Exemple #1
0
 def rebuild_apk(self, context) :
     smali_dir = os.path.join(context.apk_dir, "smali")
     dex_dir = os.path.join(context.apk_dir, "bin", "classes.dex")
     commands = [
         context.java, "-jar",
         context.baksmali,
         "-o", smali_dir,
         dex_dir,
     ]
     error = CommandUtil.run(*commands,
             stdout = self.build_stdout,
             stderr = self.build_stderr)
     if error:
         raise Exception("APKBuilder rebuild_apk baksmali ERROR:\n%s\n%s"  %(" ".join(commands), error))
     unsigned_apk_path = os.path.join(os.path.split(os.path.realpath(context.apk_path))[0], "out.unsigned.apk")
     commands = [
         context.java, "-jar",
         "-Xmx512M", "-Djava.awt.headless=true",
         context.apktool, "-f",
         "--output", unsigned_apk_path,
         "b", context.apk_dir,
     ]
     error = CommandUtil.run(*commands,
             stdout = self.build_stdout,
             stderr = self.build_stderr)
     if not os.path.isfile(unsigned_apk_path) :
         if not error:
             error = "unknow error"
         raise Exception("APKBuilder rebuild_apk rebuild ERROR:\n%s\n%s"  %(" ".join(commands), error))
     context.unsigned_apk_path = unsigned_apk_path
     return context
Exemple #2
0
    def aapt_res_assets(self, context):
        resources_apk = os.path.join(context.apk_dir, "resources._ap")
        commands = [
            context.aapt,
            "package",
            "-f",
            "--auto-add-overlay",
            "-M",
            os.path.join(context.apk_dir, "AndroidManifest.xml"),
            "-I",
            os.path.join(context.android_sdk_root, "platforms",
                         context.android_platform, "android.jar"),
            "-F",
            resources_apk,
            "-S",
            os.path.join(context.apk_dir, "res"),
            "-A",
            os.path.join(context.apk_dir, "assets"),
        ]
        for sdk in context.sdk_list:
            sdk_res_path = os.path.join(sdk.sdk_path, "res")
            if os.path.exists(sdk_res_path):
                commands.append("-S")
                commands.append(sdk_res_path)
            sdk_assets_path = os.path.join(sdk.sdk_path, "assets")
            if os.path.exists(sdk_assets_path):
                commands.append("-A")
                commands.append(sdk_assets_path)

        error = CommandUtil.run(*commands,
                                stdout=self.build_stdout,
                                stderr=self.build_stderr)
        if error:
            raise Exception("APKBuilder aapt_res_assets ERROR:\n%s\n%s" %
                            (" ".join(commands), error))
        context.resources_apk = resources_apk

        resources_out = os.path.join(context.apk_dir, "resources_out")
        commands = [
            context.java, "-jar", context.apktool, "-f", "d",
            context.resources_apk, "-o", resources_out
        ]
        error = CommandUtil.run(*commands,
                                stdout=self.build_stdout,
                                stderr=self.build_stderr)
        if error:
            raise Exception("APKBuilder aapt_res_assets ERROR:\n%s\n%s" %
                            (" ".join(commands), error))
        if os.path.exists(os.path.join(resources_out, "assets")):
            shutil.rmtree(os.path.join(context.apk_dir, "assets"))
            shutil.copytree(os.path.join(resources_out, "assets"),
                            os.path.join(context.apk_dir, "assets"))
        if os.path.exists(os.path.join(resources_out, "res")):
            shutil.rmtree(os.path.join(context.apk_dir, "res"))
            shutil.copytree(os.path.join(resources_out, "res"),
                            os.path.join(context.apk_dir, "res"))
        return context
Exemple #3
0
    def aapt_res_assets(self, context) :
        resources_apk = os.path.join(context.apk_dir, "resources._ap")
        commands = [
            context.aapt,
            "package",
            "-f",
            "--auto-add-overlay",
            "-M", os.path.join(context.apk_dir, "AndroidManifest.xml"),
            "-I", os.path.join(context.android_sdk_root, "platforms", context.android_platform, "android.jar"),
            "-F", resources_apk,
            "-S", os.path.join(context.apk_dir, "res"),
            "-A", os.path.join(context.apk_dir, "assets"),
        ]
        for sdk in context.sdk_list :
            sdk_res_path = os.path.join(sdk.sdk_path, "res")
            if os.path.exists(sdk_res_path) :
                commands.append("-S")
                commands.append(sdk_res_path)
            sdk_assets_path = os.path.join(sdk.sdk_path, "assets")
            if os.path.exists(sdk_assets_path) :
                commands.append("-A")
                commands.append(sdk_assets_path)

        error = CommandUtil.run(*commands,
                stdout = self.build_stdout,
                stderr = self.build_stderr)
        if error :
            raise Exception("APKBuilder aapt_res_assets ERROR:\n%s\n%s"  %(" ".join(commands), error))
        context.resources_apk = resources_apk

        resources_out = os.path.join(context.apk_dir, "resources_out")
        commands = [
            context.java, "-jar",
            context.apktool,
            "-f",
            "d", context.resources_apk,
            "-o", resources_out
        ]
        error = CommandUtil.run(*commands,
                stdout = self.build_stdout,
                stderr = self.build_stderr)
        if error :
            raise Exception("APKBuilder aapt_res_assets ERROR:\n%s\n%s"  %(" ".join(commands), error))
        if os.path.exists(os.path.join(resources_out, "assets")) :
            shutil.rmtree(os.path.join(context.apk_dir, "assets"))
            shutil.copytree(os.path.join(resources_out, "assets"), os.path.join(context.apk_dir, "assets"))
        if os.path.exists(os.path.join(resources_out, "res")) :
            shutil.rmtree(os.path.join(context.apk_dir, "res"))
            shutil.copytree(os.path.join(resources_out, "res"), os.path.join(context.apk_dir, "res"))
        return context
Exemple #4
0
    def aapt_package_R(self, context, sdk_config = None) :
        if None == sdk_config:
            project_path = context.apk_dir
            android_manifest = os.path.join(context.apk_dir, "AndroidManifest.xml")
        else :
            project_path = sdk_config.sdk_path
            android_manifest = os.path.join(project_path, "AndroidManifest.xml")
        gen_dir = os.path.join(context.apk_dir, "gen")
        if not os.path.exists(gen_dir) :
            os.mkdir(gen_dir)
        commands = [
            context.aapt,
            "package",
            "-m",
            "--auto-add-overlay",
            "-J", gen_dir,
            "-M", android_manifest,
            "-I", os.path.join(context.android_sdk_root, "platforms", context.android_platform, "android.jar"),
            "-S", os.path.join(context.apk_dir, "res"),
        ]
        for res_dir in context.aapt_all_res :
            commands.append("-S")
            commands.append(res_dir)
        if None == project_path :
            project_path = context.apk_dir
        else:
            commands.append("--non-constant-id")

        error = CommandUtil.run(*commands,
                stdout = self.build_stdout,
                stderr = self.build_stderr)
        if error :
            raise Exception("APKBuilder aapt_package(%s) ERROR:\n%s\n%s"  %(project_path, " ".join(commands), error))
        return context
Exemple #5
0
 def decode_apk(self, context):
     apk_dir = os.path.split(os.path.realpath(context.apk_path))[0]
     out_dir = os.path.join(apk_dir, "out")
     if not os.path.exists(out_dir):
         os.makedirs(out_dir)
     commands = [
         context.java,
         "-jar",
         "-Xmx512M",
         "-Djava.awt.headless=true",
         context.apktool,
         "-f",
         "--output",
         out_dir,
         "d",
         context.apk_path,
     ]
     error = CommandUtil.run(*commands,
                             stdout=self.build_stdout,
                             stderr=self.build_stderr)
     if error != None:
         exception = "APKBuilder decode apk %s ERROR:\n%s\n%s" % (
             context.apk_path, " ".join(commands), error)
         raise Exception(exception)
     context.apk_dir = out_dir
     apk_manifest_xml = os.path.join(context.apk_dir, "AndroidManifest.xml")
     context.apk_manifest = AndroidManifest(apk_manifest_xml)
     return context
Exemple #6
0
 def sign_apk(self, context, keystore, storepass, alias, keypass = None) :
     if None == keypass :
         keypass = storepass
     signed_apk_path = os.path.join(os.path.split(context.unsigned_apk_path)[0], "out.signed.apk")
     commands = [
         context.jarsigner,
         "-digestalg", "SHA1",
         "-sigalg", "MD5withRSA",
         "-verbose",
         "-keystore", keystore,
         "-storepass", storepass,
         "-keypass", keypass,
         "-signedjar", signed_apk_path,
         context.unsigned_apk_path, alias,
     ]
     error = CommandUtil.run(*commands,
             stdout = self.build_stdout,
             stderr = self.build_stderr)
     if not os.path.isfile(signed_apk_path) :
         if not error:
             error = "unknow error"
         sys.stderr.write("APKBuilder sign_apk ERROR:\n%s\n%s\n"  %(" ".join(commands), error))
     else :
         context.signed_apk_path = signed_apk_path
     return context
Exemple #7
0
 def sign_apk(self, context, keystore, storepass, alias, keypass=None):
     if None == keypass:
         keypass = storepass
     signed_apk_path = os.path.join(
         os.path.split(context.unsigned_apk_path)[0], "out.signed.apk")
     commands = [
         context.jarsigner,
         "-digestalg",
         "SHA1",
         "-sigalg",
         "MD5withRSA",
         "-verbose",
         "-keystore",
         keystore,
         "-storepass",
         storepass,
         "-keypass",
         keypass,
         "-signedjar",
         signed_apk_path,
         context.unsigned_apk_path,
         alias,
     ]
     error = CommandUtil.run(*commands,
                             stdout=self.build_stdout,
                             stderr=self.build_stderr)
     if not os.path.isfile(signed_apk_path):
         if not error:
             error = "unknow error"
         sys.stderr.write("APKBuilder sign_apk ERROR:\n%s\n%s\n" %
                          (" ".join(commands), error))
     else:
         context.signed_apk_path = signed_apk_path
     return context
Exemple #8
0
    def javac_class(self, context, sdk_config=None):
        gen_dir = os.path.join(context.apk_dir, "gen")
        bin_dir = os.path.join(context.apk_dir, "bin")
        if not os.path.exists(bin_dir):
            os.mkdir(bin_dir)
        commands = [
            context.javac, "-target", "1.7", "-source", "1.7",
            "-bootclasspath",
            os.path.join(context.android_sdk_root, "platforms",
                         context.android_platform, "android.jar"), "-d",
            bin_dir
        ]

        classpath = [gen_dir]
        if sdk_config:
            sdk_classpath_list = sdk_config.get_config("classpath")
            if (not isinstance(sdk_classpath_list, list)
                    or not isinstance(sdk_classpath_list, tuple)):
                sdk_classpath_list = ("libs", )
            for sdk_classpath in sdk_classpath_list:
                sdk_classpath_path = os.path.join(sdk_config.sdk_path,
                                                  sdk_classpath)
                if os.path.exists(sdk_classpath_path):
                    for rootpath, dirs, files in os.walk(sdk_classpath_path):
                        for jarfile in files:
                            classpath.append(os.path.join(rootpath, jarfile))

        commands.append("-classpath")
        commands.append(":".join(classpath))

        src_path_list = None
        project_path = None
        if sdk_config:
            project_path = sdk_config.sdk_path
            src_path_list = sdk_config.get_config("src")
            if (not isinstance(src_path_list, list)) or (not isinstance(
                    src_path_list, tuple)):
                src_path_list = ["src"]
        else:
            project_path = context.apk_dir
            src_path_list = ["gen"]

        has_src_file = False
        for src_path in src_path_list:
            src_dir = os.path.join(project_path, src_path)
            if os.path.exists(src_dir):
                for path, dirs, files in os.walk(src_dir):
                    for java_file in files:
                        if ".java" == java_file[-5:]:
                            has_src_file = True
                            commands.append(os.path.join(path, java_file))

        if has_src_file:
            error = CommandUtil.run(*commands,
                                    stdout=self.build_stdout,
                                    stderr=self.build_stderr)
            if error:
                raise Exception("APKBuilder javac_class(%s) ERROR:\n%s\n%s" %
                                (src_dir, " ".join(commands), error))
        return context
Exemple #9
0
    def javac_class(self, context, sdk_config = None) :
        gen_dir = os.path.join(context.apk_dir, "gen")
        bin_dir = os.path.join(context.apk_dir, "bin")
        if not os.path.exists(bin_dir) :
            os.mkdir(bin_dir)
        commands = [
            context.javac,
            "-target", "1.7",
            "-source", "1.7",
            "-bootclasspath", os.path.join(context.android_sdk_root, "platforms", context.android_platform, "android.jar"),
            "-d", bin_dir
        ]

        classpath = [gen_dir]
        if sdk_config :
            sdk_classpath_list = sdk_config.get_config("classpath")
            if (not isinstance(sdk_classpath_list, list) or not isinstance(sdk_classpath_list, tuple)) :
                sdk_classpath_list = ("libs",)
            for sdk_classpath in sdk_classpath_list :
                sdk_classpath_path = os.path.join(sdk_config.sdk_path, sdk_classpath)
                if os.path.exists(sdk_classpath_path) :
                    for rootpath, dirs, files in os.walk(sdk_classpath_path) :
                        for jarfile in files :
                             classpath.append(os.path.join(rootpath, jarfile))

        commands.append("-classpath")
        commands.append(":".join(classpath))

        src_path_list = None
        project_path = None
        if sdk_config :
            project_path = sdk_config.sdk_path
            src_path_list = sdk_config.get_config("src")
            if (not isinstance(src_path_list, list)) or (not isinstance(src_path_list, tuple)):
                src_path_list = ["src"]
        else :
            project_path = context.apk_dir
            src_path_list = ["gen"]

        has_src_file = False
        for src_path in src_path_list :
            src_dir = os.path.join(project_path, src_path)
            if os.path.exists(src_dir) :
                for path, dirs, files in os.walk(src_dir) :
                    for java_file in files :
                        if ".java" == java_file[-5:] :
                            has_src_file = True
                            commands.append(os.path.join(path, java_file))

        if has_src_file :
            error = CommandUtil.run(*commands,
                    stdout = self.build_stdout,
                    stderr = self.build_stderr)
            if error :
                raise Exception("APKBuilder javac_class(%s) ERROR:\n%s\n%s"  %(src_dir, " ".join(commands), error))
        return context
Exemple #10
0
 def rebuild_apk(self, context):
     smali_dir = os.path.join(context.apk_dir, "smali")
     dex_dir = os.path.join(context.apk_dir, "bin", "classes.dex")
     commands = [
         context.java,
         "-jar",
         context.baksmali,
         "-o",
         smali_dir,
         dex_dir,
     ]
     error = CommandUtil.run(*commands,
                             stdout=self.build_stdout,
                             stderr=self.build_stderr)
     if error:
         raise Exception("APKBuilder rebuild_apk baksmali ERROR:\n%s\n%s" %
                         (" ".join(commands), error))
     unsigned_apk_path = os.path.join(
         os.path.split(os.path.realpath(context.apk_path))[0],
         "out.unsigned.apk")
     commands = [
         context.java,
         "-jar",
         "-Xmx512M",
         "-Djava.awt.headless=true",
         context.apktool,
         "-f",
         "--output",
         unsigned_apk_path,
         "b",
         context.apk_dir,
     ]
     error = CommandUtil.run(*commands,
                             stdout=self.build_stdout,
                             stderr=self.build_stderr)
     if not os.path.isfile(unsigned_apk_path):
         if not error:
             error = "unknow error"
         raise Exception("APKBuilder rebuild_apk rebuild ERROR:\n%s\n%s" %
                         (" ".join(commands), error))
     context.unsigned_apk_path = unsigned_apk_path
     return context
Exemple #11
0
 def dex_jar(self, context, project_path) :
     commands = [
         context.dx,
         "--dex",
         "--output", os.path.join(project_path, "bin", "classes.dex"),
         os.path.join(project_path, "bin", "classes.jar")
     ]
     error = CommandUtil.run(*commands,
             stdout = self.build_stdout,
             stderr = self.build_stderr)
     if error :
         raise Exception("APKBuilder dex_jar(%s) ERROR:\n%s\n%s"  %(project_path, " ".join(commands), error))
     return context
Exemple #12
0
 def jar_class(self, context) :
     commands = [
         context.jar,
         "cvf",
         os.path.join(context.apk_dir, "bin", "classes.jar"),
         "-C", os.path.join(context.apk_dir, "bin"), "."
     ]
     error = CommandUtil.run(*commands,
             stdout = self.build_stdout,
             stderr = self.build_stderr)
     if error :
         raise Exception("APKBuilder jar_class(%s) ERROR:\n%s\n%s"  %(context.apk_dir, " ".join(commands), error))
     return context
Exemple #13
0
 def dex_jar(self, context, project_path):
     commands = [
         context.dx, "--dex", "--output",
         os.path.join(project_path, "bin", "classes.dex"),
         os.path.join(project_path, "bin", "classes.jar")
     ]
     error = CommandUtil.run(*commands,
                             stdout=self.build_stdout,
                             stderr=self.build_stderr)
     if error:
         raise Exception("APKBuilder dex_jar(%s) ERROR:\n%s\n%s" %
                         (project_path, " ".join(commands), error))
     return context
Exemple #14
0
 def jar_class(self, context):
     commands = [
         context.jar, "cvf",
         os.path.join(context.apk_dir, "bin", "classes.jar"), "-C",
         os.path.join(context.apk_dir, "bin"), "."
     ]
     error = CommandUtil.run(*commands,
                             stdout=self.build_stdout,
                             stderr=self.build_stderr)
     if error:
         raise Exception("APKBuilder jar_class(%s) ERROR:\n%s\n%s" %
                         (context.apk_dir, " ".join(commands), error))
     return context
Exemple #15
0
 def align_apk(self, context) :
     align_apk_path = os.path.join(os.path.split(context.signed_apk_path)[0], "out.signed.align.apk")
     commands = [
         context.zipalign,
         "-f",
         "4",
         context.signed_apk_path,
         align_apk_path,
     ]
     error = CommandUtil.run(*commands,
             stdout = self.build_stdout,
             stderr = self.build_stderr)
     if not os.path.isfile(align_apk_path) :
         if not error :
             error = "unknow error"
         sys.stderr.write("APKBuilder zipalign ERROR:\n%s\n%s\n"  %(" ".join(commands), error))
     else :
         context.align_apk_path = align_apk_path
     return context
Exemple #16
0
    def aapt_package_R(self, context, sdk_config=None):
        if None == sdk_config:
            project_path = context.apk_dir
            android_manifest = os.path.join(context.apk_dir,
                                            "AndroidManifest.xml")
        else:
            project_path = sdk_config.sdk_path
            android_manifest = os.path.join(project_path,
                                            "AndroidManifest.xml")
        gen_dir = os.path.join(context.apk_dir, "gen")
        if not os.path.exists(gen_dir):
            os.mkdir(gen_dir)
        commands = [
            context.aapt,
            "package",
            "-m",
            "--auto-add-overlay",
            "-J",
            gen_dir,
            "-M",
            android_manifest,
            "-I",
            os.path.join(context.android_sdk_root, "platforms",
                         context.android_platform, "android.jar"),
            "-S",
            os.path.join(context.apk_dir, "res"),
        ]
        for res_dir in context.aapt_all_res:
            commands.append("-S")
            commands.append(res_dir)
        if None == project_path:
            project_path = context.apk_dir
        else:
            commands.append("--non-constant-id")

        error = CommandUtil.run(*commands,
                                stdout=self.build_stdout,
                                stderr=self.build_stderr)
        if error:
            raise Exception("APKBuilder aapt_package(%s) ERROR:\n%s\n%s" %
                            (project_path, " ".join(commands), error))
        return context
Exemple #17
0
 def align_apk(self, context):
     align_apk_path = os.path.join(
         os.path.split(context.signed_apk_path)[0], "out.signed.align.apk")
     commands = [
         context.zipalign,
         "-f",
         "4",
         context.signed_apk_path,
         align_apk_path,
     ]
     error = CommandUtil.run(*commands,
                             stdout=self.build_stdout,
                             stderr=self.build_stderr)
     if not os.path.isfile(align_apk_path):
         if not error:
             error = "unknow error"
         sys.stderr.write("APKBuilder zipalign ERROR:\n%s\n%s\n" %
                          (" ".join(commands), error))
     else:
         context.align_apk_path = align_apk_path
     return context
Exemple #18
0
 def decode_apk(self, context) :
     apk_dir = os.path.split(os.path.realpath(context.apk_path))[0]
     out_dir = os.path.join(apk_dir, "out")
     if not os.path.exists(out_dir) :
         os.makedirs(out_dir)
     commands = [
         context.java, "-jar",
         "-Xmx512M", "-Djava.awt.headless=true",
         context.apktool, "-f",
         "--output", out_dir,
         "d", context.apk_path,
     ]
     error = CommandUtil.run(*commands,
             stdout = self.build_stdout,
             stderr = self.build_stderr)
     if error != None :
         exception = "APKBuilder decode apk %s ERROR:\n%s\n%s"  %(context.apk_path, " ".join(commands), error)
         raise Exception(exception)
     context.apk_dir = out_dir
     apk_manifest_xml = os.path.join(context.apk_dir, "AndroidManifest.xml")
     context.apk_manifest = AndroidManifest(apk_manifest_xml)
     return context