common_utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import file_utils
  2. import xml_utils
  3. import os.path
  4. def changeApplication(game, sdk, subChannel, config, targetApplication):
  5. '''
  6. 修改最顶级的application
  7. '''
  8. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  9. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  10. application = xml_utils.getApplicationAttr(manifest, 'name')
  11. if application is None:
  12. xml_utils.changeApplicationAttr(manifest, 'name', targetApplication)
  13. print('change add application %s' % targetApplication)
  14. else:
  15. smaliPath = os.path.join(decompliePath, 'smali')
  16. superApplication = findSuperApplication(smaliPath, application)
  17. print('super application is %s' % superApplication)
  18. if superApplication == targetApplication:
  19. return
  20. changeSuperApplication(smaliPath, superApplication, targetApplication.replace('.', '/'))
  21. print('change super application %s to %s' % (superApplication, targetApplication))
  22. def findSuperApplication(basePath, className):
  23. '''
  24. 获取最顶级的application
  25. '''
  26. tagSuper = '.super Landroid/app/Application;'
  27. tag = '.super L'
  28. applicationFile = file_utils.getPackagePath(basePath, className) + '.smali'
  29. topClassName = None
  30. superClass = None
  31. with openFile(applicationFile, 'r') as f:
  32. line = f.readline()
  33. while line:
  34. if tagSuper in line:
  35. topClassName = className
  36. break
  37. elif tag in line:
  38. superClass = line[8:-2]
  39. superClass = superClass.replace('/', '.')
  40. break
  41. line = f.readline()
  42. if topClassName is not None:
  43. return topClassName
  44. elif superClass is not None:
  45. return findSuperApplication(basePath, superClass)
  46. def changeSuperApplication(basePath, className, targetApplication):
  47. '''
  48. 更改application
  49. '''
  50. applicationFile = file_utils.getPackagePath(basePath, className) + '.smali'
  51. # 修改application
  52. changeSuperApplicationSmali(applicationFile, targetApplication)
  53. def changeSuperApplicationSmali(file, application):
  54. '''
  55. 修改application.smali
  56. '''
  57. contentSpuer = '.super Landroid/app/Application;'
  58. constructor = '.method public constructor <init>()V'
  59. direct = 'invoke-direct'
  60. invokeSuper = 'invoke-super'
  61. applicationTag = 'Landroid/app/Application;->'
  62. newContent = ''
  63. with openFile(file, 'r+') as f:
  64. line = f.readline()
  65. isConstructor = False
  66. while line:
  67. if contentSpuer in line:
  68. newContent += '.super L%s;\n' % application
  69. elif invokeSuper in line and applicationTag in line:
  70. newContent += line.replace('android/app/Application', application)
  71. elif isConstructor and direct in line and applicationTag in line:
  72. newContent += line.replace('android/app/Application', application)
  73. isConstructor = False
  74. else:
  75. if constructor in line:
  76. isConstructor = True
  77. newContent += line
  78. line = f.readline()
  79. f.seek(0, 0)
  80. #清空内容
  81. f.truncate()
  82. f.write(newContent)
  83. return 0
  84. def openFile(file, mode):
  85. return file_utils.openFile(file, mode)