Aapt_Util.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding:UTF-8 -*-
  2. import os
  3. from V1 import File, apk_utils
  4. from V1.PrintLog import printlog
  5. #生成R文件
  6. def createRFile(apkPath,aapt2disable,pkgName=""):
  7. mergeRes(apkPath)
  8. genPath = os.path.join(apkPath, 'gen')
  9. if not os.path.exists(genPath):
  10. os.mkdir(genPath)
  11. if aapt2disable:
  12. createRFileCmd = "%s/aapt p -f -m -J %s/gen -M %s/AndroidManifest.xml -I %s/android.jar -S %s/res " % (
  13. apk_utils.getComplieToolsHome(), apkPath, apkPath, apk_utils.getToolsJarHome(), apkPath)
  14. printlog("createRFileCmd:%s" % createRFileCmd)
  15. if os.system(createRFileCmd) != 0:
  16. return 1
  17. else:
  18. complieResPath = os.path.join(genPath, 'resource.zip')
  19. sdkResPath = os.path.join(apkPath, 'res')
  20. complieResCmd = '%s/aapt2 compile --dir %s -o %s' % (
  21. apk_utils.getComplieToolsHome(), sdkResPath, complieResPath)
  22. print("complieResCmd:%s" % complieResCmd)
  23. if os.system(complieResCmd) != 0:
  24. return 1
  25. # link
  26. print('link res ...')
  27. outApk = os.path.join(genPath, 'sources.apk')
  28. linkResCmd = '%s/aapt2 link -o %s -I %s/android.jar --manifest %s/AndroidManifest.xml --java %s/ %s --custom-package %s' % (
  29. apk_utils.getComplieToolsHome(), outApk, apk_utils.getToolsJarHome(), apkPath, genPath, complieResPath, pkgName)
  30. print('link cmd is %s' % linkResCmd)
  31. if os.system(linkResCmd) != 0:
  32. return 1
  33. sourcePath = pkgName.replace(".", "/")
  34. createRClassCmd = "%s/javac -source 1.8 -target 1.8 %s/gen/%s/R.java" % (
  35. apk_utils.getJavaBinPath(), apkPath, sourcePath)
  36. printlog("createRClassCmd:%s" % createRClassCmd)
  37. if os.system(createRClassCmd) != 0:
  38. return 1
  39. createDexCmd = "%s/dx --dex --output %s/classes.dex %s/gen" % (
  40. apk_utils.getSdkToolsPath(), apkPath, apkPath)
  41. printlog("createDexCmd:%s" % createDexCmd)
  42. if os.system(createDexCmd) != 0:
  43. return 1
  44. dexPath = "%s/classes.dex" %apkPath
  45. dex2smaliCmd = "%s/java -jar %s/baksmali-2.1.0.jar -o %s/smali %s" % (
  46. apk_utils.getJavaBinPath(), apk_utils.getToolsJarHome(), apkPath, dexPath)
  47. printlog("dex2smaliCmd:%s" % dex2smaliCmd)
  48. if os.system(dex2smaliCmd) != 0:
  49. return 1
  50. File.safeFileDelete(dexPath)
  51. printlog("finished compiling resources -----------------------------------")
  52. return 0
  53. def mergeRes(apkPath):
  54. allResDir = ['drawable', 'drawable-ldpi', 'drawable-mdpi', 'drawable-hdpi',
  55. 'drawable-xhdpi'
  56. , 'drawable-xxhdpi', 'drawable-xxxhdpi',
  57. 'drawable-xhdpi-v4', 'drawable-xxhdpi-v4', 'drawable-xxxhdpi-v4',
  58. 'mipmap-ldpi', 'mipmap-mdpi', 'mipmap-hdpi', 'mipmap-xhdpi',
  59. 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  60. for file in allResDir:
  61. resdir ="%s/res/%s"%(apkPath,file)
  62. print ("resdir :%s" % resdir)
  63. v4dir = "%s/res/%s-v4"%(apkPath,file)
  64. print ("v4dir :%s" % v4dir)
  65. if os.path.exists(v4dir) == True:
  66. File.copyAllFile(resdir, v4dir)
  67. File.safeFileDelete(resdir)
  68. pass