use androguard if aapt isn't found

This commit is contained in:
thez3ro 2017-04-13 14:18:48 +02:00
parent 9607cdb621
commit 06598ae406
No known key found for this signature in database
GPG key ID: F628F9E41DD7C073
8 changed files with 490 additions and 111 deletions

View file

@ -483,15 +483,23 @@ def capitalize_intact(string):
return string[0].upper() + string[1:]
def get_metadata_from_apk(app, build, apkfile):
"""get the required metadata from the built APK"""
def has_native_code(apkobj):
"""aapt checks if there are architecture folders under the lib/ folder
so we are simulating the same behaviour"""
arch_re = re.compile("^lib/(.*)/.*$")
arch = [file for file in apkobj.get_files() if arch_re.match(file)]
return False if not arch else True
p = SdkToolsPopen(['aapt', 'dump', 'badging', apkfile], output=False)
def get_apk_metadata_aapt(apkfile):
"""aapt function to extract versionCode, versionName, packageName and nativecode"""
vercode = None
version = None
foundid = None
nativecode = None
p = SdkToolsPopen(['aapt', 'dump', 'badging', apkfile], output=False)
for line in p.output.splitlines():
if line.startswith("package:"):
pat = re.compile(".*name='([a-zA-Z0-9._]*)'.*")
@ -509,6 +517,38 @@ def get_metadata_from_apk(app, build, apkfile):
elif line.startswith("native-code:"):
nativecode = line[12:]
return vercode, version, foundid, nativecode
def get_apk_metadata_androguard(apkfile):
"""androguard function to extract versionCode, versionName, packageName and nativecode"""
try:
from androguard.core.bytecodes.apk import APK
apkobject = APK(apkfile)
except ImportError:
raise BuildException("androguard library is not installed and aapt binary not found")
except FileNotFoundError:
raise BuildException("Could not open apk file for metadata analysis")
if not apkobject.is_valid_APK():
raise BuildException("Invalid APK provided")
foundid = apkobject.get_package()
vercode = apkobject.get_androidversion_code()
version = apkobject.get_androidversion_name()
nativecode = has_native_code(apkobject)
return vercode, version, foundid, nativecode
def get_metadata_from_apk(app, build, apkfile):
"""get the required metadata from the built APK"""
if common.set_command_in_config('aapt'):
vercode, version, foundid, nativecode = get_apk_metadata_aapt(apkfile)
else:
vercode, version, foundid, nativecode = get_apk_metadata_androguard(apkfile)
# Ignore empty strings or any kind of space/newline chars that we don't
# care about
if nativecode is not None:
@ -533,7 +573,6 @@ def get_metadata_from_apk(app, build, apkfile):
def build_local(app, build, vcs, build_dir, output_dir, log_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver, refresh):
"""Do a build locally."""
ndk_path = build.ndk_path()
if build.ndk or (build.buildjni and build.buildjni != ['no']):
if not ndk_path: