# -*- coding:UTF-8 -*- import os from V1 import File, apk_utils from V1.PrintLog import printlog #生成R文件 def createRFile(apkPath,aapt2disable,pkgName=""): mergeRes(apkPath) genPath = os.path.join(apkPath, 'gen') if not os.path.exists(genPath): os.mkdir(genPath) if aapt2disable: createRFileCmd = "%s/aapt p -f -m -J %s/gen -M %s/AndroidManifest.xml -I %s/android.jar -S %s/res " % ( apk_utils.getComplieToolsHome(), apkPath, apkPath, apk_utils.getToolsJarHome(), apkPath) printlog("createRFileCmd:%s" % createRFileCmd) if os.system(createRFileCmd) != 0: return 1 else: complieResPath = os.path.join(genPath, 'resource.zip') sdkResPath = os.path.join(apkPath, 'res') complieResCmd = '%s/aapt2 compile --dir %s -o %s' % ( apk_utils.getComplieToolsHome(), sdkResPath, complieResPath) print("complieResCmd:%s" % complieResCmd) if os.system(complieResCmd) != 0: return 1 # link print('link res ...') outApk = os.path.join(genPath, 'sources.apk') linkResCmd = '%s/aapt2 link -o %s -I %s/android.jar --manifest %s/AndroidManifest.xml --java %s/ %s --custom-package %s' % ( apk_utils.getComplieToolsHome(), outApk, apk_utils.getToolsJarHome(), apkPath, genPath, complieResPath, pkgName) print('link cmd is %s' % linkResCmd) if os.system(linkResCmd) != 0: return 1 sourcePath = pkgName.replace(".", "/") createRClassCmd = "%s/javac -source 1.8 -target 1.8 %s/gen/%s/R.java" % ( apk_utils.getJavaBinPath(), apkPath, sourcePath) printlog("createRClassCmd:%s" % createRClassCmd) if os.system(createRClassCmd) != 0: return 1 createDexCmd = "%s/dx --dex --output %s/classes.dex %s/gen" % ( apk_utils.getSdkToolsPath(), apkPath, apkPath) printlog("createDexCmd:%s" % createDexCmd) if os.system(createDexCmd) != 0: return 1 dexPath = "%s/classes.dex" %apkPath dex2smaliCmd = "%s/java -jar %s/baksmali-2.1.0.jar -o %s/smali %s" % ( apk_utils.getJavaBinPath(), apk_utils.getToolsJarHome(), apkPath, dexPath) printlog("dex2smaliCmd:%s" % dex2smaliCmd) if os.system(dex2smaliCmd) != 0: return 1 File.safeFileDelete(dexPath) printlog("finished compiling resources -----------------------------------") return 0 def mergeRes(apkPath): allResDir = ['drawable', 'drawable-ldpi', 'drawable-mdpi', 'drawable-hdpi', 'drawable-xhdpi' , 'drawable-xxhdpi', 'drawable-xxxhdpi', 'drawable-xhdpi-v4', 'drawable-xxhdpi-v4', 'drawable-xxxhdpi-v4', 'mipmap-ldpi', 'mipmap-mdpi', 'mipmap-hdpi', 'mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi'] for file in allResDir: resdir ="%s/res/%s"%(apkPath,file) print ("resdir :%s" % resdir) v4dir = "%s/res/%s-v4"%(apkPath,file) print ("v4dir :%s" % v4dir) if os.path.exists(v4dir) == True: File.copyAllFile(resdir, v4dir) File.safeFileDelete(resdir) pass