def convertDex2JarFilesWithRelativePaths(self, dexRootDir, dexFileNames, jarFilesRootDir, overwrite=True, proceedOnError=True): ''' Converts provided dex files with relative paths into the specified folder. Returns a list containing tuples that show the correspondence between successfully converted dex files and jar files (relative paths). Args: :param dexRootDir: root dir for dex files :param dexFileNames: list of dex file paths against dexRootDir that need to be converted to corresponding jar file :param jarFilesRootDir: the path to the folder where the results of the convertion will be placed :param override: override jar file in the output folder if it exists :param proceedOnError: if True - proceed converting provided files even if an error during a file convertion occurred. The entry with dex and jar file, where an error occurred, will not be included into the final map Returns: dex2jarRelativeCorrespondence: list of tuples that reflect the correspondence between successfully converted dex and jar files ''' ensureDirExists(jarFilesRootDir) dex2jarRelativeCorrespondence = [] for dexFileRelative in dexFileNames: dexFileFull = os.path.join(dexRootDir, dexFileRelative) jarFileRelative = os.path.splitext(dexFileRelative)[0] + ".jar" jarFileFull = os.path.join(jarFilesRootDir, jarFileRelative) try: self.convertDex2Jar(dexFileFull, jarFileFull, overwrite) except Dex2JarConvertionError: if proceedOnError: continue else: raise dex2jarRelativeTuple = (dexFileRelative, jarFileRelative) dex2jarRelativeCorrespondence.append(dex2jarRelativeTuple) return dex2jarRelativeCorrespondence
def convertJar2DexFiles(self, jarFiles, outputFolder, withFiles=[], overwrite=True, proceedOnError=True): ''' Converts provided jar files into dex files to the specified folder. Returns the map that shows the correspondence between successfully converted jar files and dex files. Args: :param jarFiles: list of jar files that are required to be converted to dex files :param outputFolder: the path to the folder where to store the resulting dex files :param withFiles: additional files that need to be included into the final dex file :param override: if True - override the destination jar file if exists :param proceedOnError: if True - proceed converting provided files even if an error during a file convertion occurred. The entry with dex and jar file, where an error occurred, will not be included into the final map. Returns: jar2dexMap: map that reflects the correspondence between successfully converted jar and dex files. ''' ensureDirExists(outputFolder) jar2dexMap = {} for jarFile in jarFiles: dexFilename = os.path.splitext(os.path.basename(jarFile))[0] + ".dex" dexFile = os.path.join(outputFolder, dexFilename) try: self.convertJar2Dex(jarFile, dexFile, withFiles, overwrite) except Jar2DexConvertionError: if proceedOnError: continue else: raise jar2dexMap[jarFile] = dexFile return jar2dexMap
def instrumentJarWithEmma(self, jarFile, outputFolder, emmaMetadataFile): ''' Instruments provided jar file with Emma code coverage tool code. The resulting emmaMetadataFile is create with merge==yes, i.e., if the file exists the information about instrumentation will be merged with the existing data. Only instrumented jar files will be copied to the outputFolder. Args: :param jarFile: a jar file to instrument :param outputFolder: where to store instrumented file :param emmaMetadataFile: path to the file with emma instrumentation results ''' ensureDirExists(outputFolder) emma = EmmaInterface(javaPath = self.config.getEmmaJavaPath(), javaOpts = self.config.getEmmaJavaOpts(), pathEmma = self.config.getEmmaDir(), jarEmma = self.config.getEmmaJar(), jarEmmaDevice = self.config.getEmmaDeviceJar()) (successfulRun, cmdOutput) = emma.instr(instrpaths = [jarFile], outdir = outputFolder, emmaMetadataFile = emmaMetadataFile, merge = EMMA_MERGE.YES, outmode = EMMA_OUTMODE.FULLCOPY) if successfulRun: #emma put instrumented jar files into lib folder so we need to move #them back to the output folder and delete jarsOutDir = os.path.join(outputFolder, "lib") for filename in os.listdir(jarsOutDir): shutil.move(os.path.join(jarsOutDir, filename), os.path.join(outputFolder, filename)) shutil.rmtree(jarsOutDir) return if not successfulRun: err = "Cannot instrument jar file [%s] with Emma. %s" % (jarFile, cmdOutput) raise EmmaCannotInstrumentException(err)
def decompileApk(self, pathToApk, dirToDecompile): ''' Using Apktool tries to decompile an apk file. Currently decompile only resources (required to decompile AndroidManifest.xml). Args: :param pathToApk: path to a valid apk file that needs to be decompiled :param dirToDecompile: path to a dir where the result of the decompile process will be stored Returns: Raises: ApkCannotBeDecompiledException: if the provided apk file cannot be decompiled. ''' ensureDirExists(dirToDecompile) apktool = ApktoolInterface(javaPath = self.config.getApktoolJavaPath(), javaOpts = self.config.getApktoolJavaOpts(), pathApktool = self.config.getApktoolPath(), jarApktool = self.config.getApktoolJar()) (successfulRun, cmdOutput) = apktool.decode(apkPath = pathToApk, dirToDecompile = dirToDecompile, quiet = True, noSrc = True, noRes = False, debug = False, #maybe true to enable debugging noDebugInfo = False, #check this force = True, #directory exist so without this this process finishes frameworkTag = "", frameworkDir = "", keepBrokenRes = True) if not successfulRun: err = "Cannot decompile file: [%s] into dir [%s]. ERRSTR: %s" % (pathToApk, dirToDecompile, cmdOutput) raise ApkCannotBeDecompiledException(err)
def convertDex2JarFiles(self, dexFileNames, outputFolder, overwrite=True, proceedOnError=True): ''' Converts provided dex files into the specified folder. Returns the map that shows the correspondence between successfully converted dex files and jar files. Args: :param dexFileNames: list of files that need to be converted to corresponding jar files :param outputFolder: the path to the folder where the results of the convertion will be placed :param override: override jar file in the output folder if it exists :param proceedOnError: if True - proceed converting provided files even if an error during a file convertion occurred. The entry with dex and jar file, where an error occurred, will not be included into the final map. Returns: dex2jarCorrespondence: tuple that reflects the correspondence between successfully converted dex and jar files. ''' ensureDirExists(outputFolder) dex2jarCorrespondence = [] for dexFile in dexFileNames: jarFilename = os.path.splitext(os.path.basename(dexFile))[0] + ".jar" jarFile = os.path.join(outputFolder, jarFilename) try: self.convertDex2Jar(dexFile, jarFile, overwrite) except Dex2JarConvertionError: if proceedOnError: continue else: raise dex2jarTuple = (dexFile, jarFile) dex2jarCorrespondence.append(dex2jarTuple) return dex2jarCorrespondence