Skip to content

Commit

Permalink
Automatically detect android sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Jan 11, 2024
1 parent 52a91c0 commit 3eb30d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
11 changes: 3 additions & 8 deletions pysoot/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class PySootError(Exception):
pass

Expand All @@ -7,17 +6,13 @@ class ParameterError(PySootError):
pass


class JythonClientException(PySootError):
pass


class RecvException(PySootError):
class JavaNotFoundError(PySootError):
pass


class JavaNotFoundError(PySootError):
class MissingJavaRuntimeJarsError(PySootError):
pass


class MissingJavaRuntimeJarsError(PySootError):
class AndroidSDKNotFoundError(PySootError):
pass
25 changes: 22 additions & 3 deletions pysoot/lifter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import subprocess

from .errors import JavaNotFoundError, MissingJavaRuntimeJarsError, ParameterError
from .errors import AndroidSDKNotFoundError, JavaNotFoundError, MissingJavaRuntimeJarsError, ParameterError
from .soot_manager import SootManager


Expand Down Expand Up @@ -50,8 +50,7 @@ def __init__(self, input_file=None, input_format="jar", ir_format="shimple", add

elif input_format == "apk":
if android_sdk is None:
raise ParameterError("when format is apk, android_sdk should point to something like: "
"~/Android/Sdk/platforms")
android_sdk = _find_android_sdk()
if additional_jars is not None or additional_jar_roots is not None:
l.warning("when input_format is 'apk', setting additional_jars or additional_jar_roots is pointless")
self.android_sdk = android_sdk
Expand Down Expand Up @@ -106,3 +105,23 @@ def _find_rt_jars() -> set[str]:
raise MissingJavaRuntimeJarsError

return jar_paths


def _find_android_sdk() -> str:
# Use $ANDROID_SDK_ROOT if it is set
if "ANDROID_SDK_ROOT" in os.environ:
return os.path.join(os.environ["ANDROID_SDK_ROOT"], "platforms")

# Use $ANDROID_HOME if it is set
if "ANDROID_HOME" in os.environ:
return os.path.join(os.environ["ANDROID_HOME"], "platforms")

raise AndroidSDKNotFoundError


def has_android_sdk() -> bool:
try:
_find_android_sdk()
return True
except AndroidSDKNotFoundError:
return False
8 changes: 2 additions & 6 deletions tests/test_pysoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import unittest

from pysoot.lifter import Lifter
from pysoot.lifter import Lifter, has_android_sdk


class TestPySoot(unittest.TestCase):
Expand All @@ -15,9 +15,6 @@ class TestPySoot(unittest.TestCase):
os.path.dirname(__file__), "..", "..", "binaries-private", "tests", "java"
)
)
android_sdk_path = os.path.join(
os.path.expanduser("~"), "Android", "Sdk", "platforms"
)

def compare_code(self, tstr1, tstr2):
for l1, l2 in zip(tstr1.split("\n"), tstr2.split("\n")):
Expand Down Expand Up @@ -91,8 +88,7 @@ def test_exceptions1(self):
elif i in [3, 4, 5, 14, 15, 16]:
assert block in preds

# TODO consider adding Android Sdk in the CI server
@unittest.skipUnless(os.path.exists(android_sdk_path), "Android SDK not found")
@unittest.skipUnless(has_android_sdk(), "Android SDK not found")
def test_android1(self):
apk = os.path.join(self.test_samples_folder, "android1.apk")
lifter = Lifter(apk, input_format="apk", android_sdk=self.android_sdk_path)
Expand Down

0 comments on commit 3eb30d0

Please sign in to comment.