Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
Format code with Black [auto]
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudiuGeorgiu committed Dec 24, 2021
1 parent 5826d47 commit 96a3032
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
49 changes: 29 additions & 20 deletions src/obfuscapk/obfuscation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
self.key_password: str = key_password
self.ignore_packages_file: str = ignore_packages_file
self.use_aapt2 = use_aapt2
if apk_path.endswith('aab'):
if apk_path.endswith("aab"):
self.is_bundle = True
else:
self.is_bundle = False
Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__(
# If the path of the output obfuscated apk is not specified, save it in the
# working directory.
if not self.obfuscated_apk_path:
if (self.is_bundle):
if self.is_bundle:
self.obfuscated_apk_path = "{0}_obfuscated.aab".format(
os.path.join(
self.working_dir_path,
Expand Down Expand Up @@ -352,21 +352,25 @@ def decode_apk(self) -> None:
os.path.splitext(os.path.basename(self.apk_path))[0],
)
try:
if (self.is_bundle):
bundledecompiler.decode(self.apk_path, self._decoded_apk_path, force=False)
if self.is_bundle:
bundledecompiler.decode(
self.apk_path, self._decoded_apk_path, force=False
)
else:
apktool.decode(self.apk_path, self._decoded_apk_path, force=True)


# Path to the decoded manifest file.
if (self.is_bundle):
if self.is_bundle:
self._manifest_file = os.path.join(
self._decoded_apk_path, "base", "manifest", "AndroidManifest.xml",
self._decoded_apk_path,
"base",
"manifest",
"AndroidManifest.xml",
)
else:
self._manifest_file = os.path.join(
self._decoded_apk_path, "AndroidManifest.xml"
)
self._decoded_apk_path, "AndroidManifest.xml"
)

# A list containing the paths to all the smali files obtained with
# apktool or bundledecompiler.
Expand Down Expand Up @@ -412,9 +416,11 @@ def decode_apk(self) -> None:
self._smali_files.sort()

# Check if multidex.
if (self.is_bundle):
if self.is_bundle:
if os.path.isdir(
os.path.join(self._decoded_apk_path, "base", "dex", "smali_classes2")
os.path.join(
self._decoded_apk_path, "base", "dex", "smali_classes2"
)
):
self._is_multidex = True
else:
Expand All @@ -423,15 +429,19 @@ def decode_apk(self) -> None:
):
self._is_multidex = True

if (self._is_multidex):
if self._is_multidex:
smali_directories = ["smali"]
for i in range(2, 15):
smali_directories.append("smali_classes{0}".format(i))

for smali_directory in smali_directories:
if (self.is_bundle):
if self.is_bundle:
current_directory = os.path.join(
self._decoded_apk_path, "base", "dex", smali_directory, ""
self._decoded_apk_path,
"base",
"dex",
smali_directory,
"",
)
else:
current_directory = os.path.join(
Expand Down Expand Up @@ -539,7 +549,7 @@ def build_obfuscated_apk(self) -> None:
bundledecompiler: BundleDecompiler = BundleDecompiler()

try:
if (self.is_bundle):
if self.is_bundle:
bundledecompiler.build(self._decoded_apk_path, self.obfuscated_apk_path)
else:
apktool.build(self._decoded_apk_path, self.obfuscated_apk_path)
Expand Down Expand Up @@ -579,7 +589,7 @@ def sign_obfuscated_apk(self) -> None:
)

try:
if (self.is_bundle):
if self.is_bundle:
aabsigner.sign(
self.obfuscated_apk_path,
)
Expand All @@ -601,7 +611,7 @@ def align_obfuscated_apk(self) -> None:

# The obfuscated apk will be aligned with zipalign.
zipalign: Zipalign = Zipalign()
if (self.is_bundle):
if self.is_bundle:
return

try:
Expand Down Expand Up @@ -652,7 +662,7 @@ def get_assets_directory(self) -> str:
self.decode_apk()

# '.join(x, "")' is used to add a trailing slash.
if (self.is_bundle):
if self.is_bundle:
return os.path.join(self._decoded_apk_path, "base", "assets", "")
else:
return os.path.join(self._decoded_apk_path, "assets", "")
Expand All @@ -663,11 +673,10 @@ def get_resource_directory(self) -> str:
self.decode_apk()

# '.join(x, "")' is used to add a trailing slash.
if (self.is_bundle):
if self.is_bundle:
return os.path.join(self._decoded_apk_path, "base", "res", "")
else:
return os.path.join(self._decoded_apk_path, "res", "")


def get_ignore_package_names(self) -> List[str]:
ignore_package_list = []
Expand Down
36 changes: 24 additions & 12 deletions src/obfuscapk/toolbundledecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def __init__(self):
)

if platform.system() == "Windows":
self.logger.warning("BundleDecompiler is not yet available on Windows platform")
self.logger.warning(
"BundleDecompiler is not yet available on Windows platform"
)
return

if "BUNDLE_DECOMPILER_PATH" in os.environ:
Expand All @@ -28,14 +30,18 @@ def __init__(self):
# Make sure bundle decompiler is available
if not os.path.isfile(full_bundledecompiler_path):
raise RuntimeError(
'Cannot find BundleDecompiler with executable "{0}"'.format(full_bundledecompiler_path)
'Cannot find BundleDecompiler with executable "{0}"'.format(
full_bundledecompiler_path
)
)

# Make sure to use the full path of the executable (needed for cross-platform
# compatibility).
if full_bundledecompiler_path is None:
raise RuntimeError(
'Something is wrong with executable "{0}"'.format(self.bundledecompiler_path)
'Something is wrong with executable "{0}"'.format(
self.bundledecompiler_path
)
)
else:
self.bundledecompiler_path = full_bundledecompiler_path
Expand All @@ -44,7 +50,9 @@ def decode(
self, aab_path: str, output_dir_path: str = None, force: bool = False
) -> str:
if platform.system() == "Windows":
raise NotImplementedError("BundleDecompiler is not yet available on Windows platform")
raise NotImplementedError(
"BundleDecompiler is not yet available on Windows platform"
)

# Check if the aab file to decode is a valid file.
if not os.path.isfile(aab_path):
Expand Down Expand Up @@ -102,9 +110,7 @@ def decode(
]

if force:
self.logger.warning(
'Bundle Decompiler does not support force'
)
self.logger.warning("Bundle Decompiler does not support force")

try:
self.logger.info(
Expand Down Expand Up @@ -132,7 +138,9 @@ def decode(

def build(self, source_dir_path: str, output_aab_path: str = None) -> str:
if platform.system() == "Windows":
raise NotImplementedError("BundleDecompiler is not yet available on Windows platform")
raise NotImplementedError(
"BundleDecompiler is not yet available on Windows platform"
)

# Check if the input directory exists.
if not os.path.isdir(source_dir_path):
Expand Down Expand Up @@ -206,7 +214,9 @@ def __init__(self):
)

if platform.system() == "Windows":
self.logger.warning("BundleDecompiler is not yet available on Windows platform")
self.logger.warning(
"BundleDecompiler is not yet available on Windows platform"
)
return

if "BUNDLE_DECOMPILER_PATH" in os.environ:
Expand All @@ -230,7 +240,9 @@ def sign(
aab_path: str,
) -> str:
if platform.system() == "Windows":
raise NotImplementedError("BundleDecompiler is not yet available on Windows platform")
raise NotImplementedError(
"BundleDecompiler is not yet available on Windows platform"
)

# Check if the aab file to sign is a valid file.
if not os.path.isfile(aab_path):
Expand All @@ -243,12 +255,12 @@ def sign(
self.aabsigner_path,
"sign-bundle",
"--in=" + aab_path,
"--out=" + aab_path.replace(".aab","_signed.aab")
"--out=" + aab_path.replace(".aab", "_signed.aab"),
]

try:
self.logger.info('Running sign command "{0}"'.format(" ".join(sign_cmd)))
output = subprocess.check_output(sign_cmd, stderr=subprocess.STDOUT).strip()
output = subprocess.check_output(sign_cmd, stderr=subprocess.STDOUT).strip()
return output.decode(errors="replace")
except subprocess.CalledProcessError as e:
self.logger.error(
Expand Down

0 comments on commit 96a3032

Please sign in to comment.