gen_test_apk_gradle_script.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import argparse
  2. import os
  3. import tarfile
  4. import xml.etree.ElementTree as etree
  5. FLAT_DIRS_REPO_TEMPLATE = 'flatDir {{ dirs {dirs} }}\n'
  6. MAVEN_REPO_TEMPLATE = 'maven {{ url "{repo}" }}\n'
  7. KEYSTORE_TEMLATE = 'signingConfigs {{ debug {{ storeFile file("{keystore}") }} }}\n'
  8. TEST_APK_TEMPLATE = """\
  9. ext.jniLibsDirs = [
  10. {jni_libs_dirs}
  11. ]
  12. ext.resDirs = [
  13. {res_dirs}
  14. ]
  15. ext.javaDirs = [
  16. {java_dirs}
  17. ]
  18. ext.bundles = [
  19. {bundles}
  20. ]
  21. buildscript {{
  22. // repositories {{
  23. // jcenter()
  24. // }}
  25. repositories {{
  26. {maven_repos}
  27. }}
  28. dependencies {{
  29. classpath 'com.android.tools.build:gradle:3.5.3'
  30. }}
  31. }}
  32. apply plugin: 'com.android.application'
  33. repositories {{
  34. // maven {{
  35. // url "http://maven.google.com/"
  36. // }}
  37. // maven {{
  38. // url "http://artifactory.yandex.net/artifactory/public/"
  39. // }}
  40. // flatDir {{
  41. // dirs System.env.PKG_ROOT + '/bundle'
  42. // }}
  43. {flat_dirs_repo}
  44. {maven_repos}
  45. }}
  46. dependencies {{
  47. for (bundle in bundles) {{
  48. compile("$bundle")
  49. }}
  50. }}
  51. android {{
  52. {keystore}
  53. compileSdkVersion 33
  54. buildToolsVersion "33.0.0"
  55. compileOptions {{
  56. sourceCompatibility 1.8
  57. targetCompatibility 1.8
  58. }}
  59. defaultConfig {{
  60. minSdkVersion 26
  61. targetSdkVersion 33
  62. applicationId "{app_id}"
  63. }}
  64. sourceSets {{
  65. main {{
  66. manifest.srcFile 'Manifest.xml'
  67. jniLibs.srcDirs = jniLibsDirs
  68. res.srcDirs = resDirs
  69. java.srcDirs = javaDirs
  70. }}
  71. }}
  72. applicationVariants.all {{ variant ->
  73. variant.outputs.each {{ output ->
  74. def fileName = "$projectDir/output/{app_id}.apk"
  75. output.outputFileName = new File(output.outputFile.parent, fileName).getName()
  76. }}
  77. }}
  78. dependencies {{
  79. implementation 'com.google.android.gms:play-services-location:21.0.1'
  80. implementation 'com.google.android.gms:play-services-gcm:17.0.0'
  81. implementation 'com.evernote:android-job:1.2.6'
  82. implementation 'androidx.annotation:annotation:1.1.0'
  83. implementation 'androidx.core:core:1.1.0'
  84. }}
  85. }}
  86. """
  87. def create_native_properties(output_dir, library_name):
  88. native_properties_file = os.path.join(output_dir, 'native_library_name.xml')
  89. resources = etree.Element('resources')
  90. name = etree.SubElement(resources, 'item', dict(name='native_library_name', type='string'))
  91. name.text = library_name
  92. etree.ElementTree(resources).write(native_properties_file, xml_declaration=True, encoding='utf-8')
  93. def gen_build_script(args):
  94. def wrap(items):
  95. return ',\n '.join('"{}"'.format(x) for x in items)
  96. bundles = []
  97. bundles_dirs = set(args.flat_repos)
  98. for bundle in args.bundles:
  99. dir_name, base_name = os.path.split(bundle)
  100. assert len(dir_name) > 0 and len(base_name) > 0
  101. name, ext = os.path.splitext(base_name)
  102. assert len(name) > 0 and ext == '.aar'
  103. bundles_dirs.add(dir_name)
  104. bundles.append('com.yandex:{}@aar'.format(name))
  105. if len(bundles_dirs) > 0:
  106. flat_dirs_repo = FLAT_DIRS_REPO_TEMPLATE.format(dirs=wrap(bundles_dirs))
  107. else:
  108. flat_dirs_repo = ''
  109. maven_repos = ''.join(MAVEN_REPO_TEMPLATE.format(repo=repo) for repo in args.maven_repos)
  110. if args.keystore:
  111. keystore = KEYSTORE_TEMLATE.format(keystore=args.keystore)
  112. else:
  113. keystore = ''
  114. return TEST_APK_TEMPLATE.format(
  115. app_id=args.app_id,
  116. jni_libs_dirs=wrap(args.jni_libs_dirs),
  117. res_dirs=wrap(args.res_dirs),
  118. java_dirs=wrap(args.java_dirs),
  119. maven_repos=maven_repos,
  120. bundles=wrap(bundles),
  121. flat_dirs_repo=flat_dirs_repo,
  122. keystore=keystore,
  123. )
  124. if __name__ == '__main__':
  125. parser = argparse.ArgumentParser()
  126. parser.add_argument('--aars', nargs='*', default=[])
  127. parser.add_argument('--app-id', required=True)
  128. parser.add_argument('--assets-dirs', nargs='*', default=[])
  129. parser.add_argument('--bundles', nargs='*', default=[])
  130. parser.add_argument('--bundle-name', nargs='?', default=None)
  131. parser.add_argument('--java-dirs', nargs='*', default=[])
  132. parser.add_argument('--jni-libs-dirs', nargs='*', default=[])
  133. parser.add_argument('--library-name', required=True)
  134. parser.add_argument('--manifest', required=True)
  135. parser.add_argument('--flat-repos', nargs='*', default=[])
  136. parser.add_argument('--maven-repos', nargs='*', default=[])
  137. parser.add_argument('--output-dir', required=True)
  138. parser.add_argument('--peers', nargs='*', default=[])
  139. parser.add_argument('--keystore', default=None)
  140. parser.add_argument('--res-dirs', nargs='*', default=[])
  141. args = parser.parse_args()
  142. for index, jsrc in enumerate(filter(lambda x: x.endswith('.jsrc'), args.peers)):
  143. jsrc_dir = os.path.join(args.output_dir, 'jsrc_{}'.format(str(index)))
  144. os.makedirs(jsrc_dir)
  145. with tarfile.open(jsrc, 'r') as tar:
  146. tar.extractall(path=jsrc_dir)
  147. args.java_dirs.append(jsrc_dir)
  148. args.build_gradle = os.path.join(args.output_dir, 'build.gradle')
  149. args.settings_gradle = os.path.join(args.output_dir, 'settings.gradle')
  150. args.gradle_properties = os.path.join(args.output_dir, 'gradle.properties')
  151. content = gen_build_script(args)
  152. with open(args.build_gradle, 'w') as f:
  153. f.write(content)
  154. with open(args.gradle_properties, 'w') as f:
  155. f.write(
  156. '''android.enableJetifier=true
  157. android.useAndroidX=true
  158. org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=512m'''
  159. )
  160. if args.bundle_name:
  161. with open(args.settings_gradle, 'w') as f:
  162. f.write('rootProject.name = "{}"'.format(args.bundle_name))
  163. values_dir = os.path.join(args.output_dir, 'res', 'values')
  164. os.makedirs(values_dir)
  165. create_native_properties(values_dir, args.library_name)