1#!/usr/bin/python3 2 3# Helper script for updating androidx.test prebuilts from maven 4# 5# Usage: 6# a. Initialize android environment eg . build/envsetup.sh; lunch <target> 7# b. Update the version numbers in this file 8# c. ./prebuilts/misc/androidx-test/update-from-gmaven.py 9# 10# The script will then: 11# 1. Remove the previous artifacts 12# 2. Download the aars and poms into a file structure mirroring their maven 13# path 14# 3. Extract the AndroidManifest from the aars into the manifests folder 15# 4. Run pom2bp to generate the Android.bp 16 17import os 18import subprocess 19import sys 20 21monitorVersion="1.7.0" 22runnerVersion="1.6.0" 23rulesVersion="1.6.0" 24espressoVersion="3.6.0" 25coreVersion="1.6.0" 26extJUnitVersion="1.2.0" 27extTruthVersion="1.6.0" 28orchestratorVersion="1.5.0" 29servicesVersion="1.5.0" 30jankTestHelperVersion="1.0.1" 31 32mavenToBpPatternMap = { 33 "androidx.test:" : "androidx.test.", 34 "androidx.test.ext:": "androidx.test.ext.", 35 "androidx.test.espresso:espresso-":"androidx.test.espresso.", 36 "androidx.test.janktesthelper:janktesthelper":"androidx.test.janktesthelper", 37 "androidx.test.services:storage":"androidx.test.services.storage", 38 "androidx.test.services:test-services":"androidx.test.services.test-services", 39 "androidx.tracing:tracing":"androidx.tracing_tracing", 40 "androidx.concurrent:concurrent-futures":"androidx.concurrent_concurrent-futures", 41 "com.google.guava:listenablefuture":"guava-listenablefuture-prebuilt-jar", 42 } 43 44extraLibs = { 45 "androidx.test.rules" : "android.test.base", 46 } 47 48prependLicenseTemplate = """ 49package {{ 50 default_applicable_licenses: ["Android-Apache-2.0"], 51}} 52 53filegroup {{ 54 name: "test-services.apk", 55 srcs: 56 ["androidx/test/services/test-services/{servicesVersion}/test-services-{servicesVersion}.apk",], 57 path: "androidx/test/services/test-services/{servicesVersion}", 58 visibility: [ 59 "//tools/tradefederation/core:__pkg__", 60 ], 61}} 62""" 63 64def cmd(args): 65 print(args) 66 out = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) 67 if (out.returncode != 0): 68 print(out.stderr.decode("utf-8")) 69 sys.exit(out.returncode) 70 out_string = out.stdout.decode("utf-8") 71 print(out_string) 72 return out_string 73 74def chdir(path): 75 print("cd %s" % path) 76 os.chdir(path) 77 78def getAndroidRoot(): 79 if os.path.isdir(".repo/projects"): 80 return os.getcwd() 81 elif 'TOP' in os.environ: 82 return os.environ['TOP'] 83 else: 84 print("Error: Run from android source root or set TOP envvar") 85 sys.exit(-1) 86 87def downloadArtifact(groupId, artifactId, version): 88 """Downloads an aar, sources.jar and pom from google maven""" 89 groupPath = groupId.replace('.', '/') 90 artifactDirPath = os.path.join(groupPath, artifactId, version) 91 artifactPath = os.path.join(artifactDirPath, "%s-%s" % (artifactId, version)) 92 cmd("mkdir -p " + artifactDirPath) 93 # download aar 94 cmd("wget -O %s.aar https://dl.google.com/dl/android/maven2/%s.aar" % (artifactPath, artifactPath)) 95 96 # extract AndroidManifest.xml from aar, into path expected by pom2bp 97 manifestDir = getManifestPath("%s:%s" % (groupId,artifactId)) 98 cmd("mkdir -p " + manifestDir) 99 cmd("unzip -o %s.aar AndroidManifest.xml -d %s" % (artifactPath, manifestDir)) 100 101 # download pom 102 cmd("wget -O %s.pom https://dl.google.com/dl/android/maven2/%s.pom" % (artifactPath, artifactPath)) 103 104 # download sources.jar 105 cmd("wget -O %s-sources.jar https://dl.google.com/dl/android/maven2/%s-sources.jar" % (artifactPath, artifactPath)) 106 107def downloadApk(groupId, artifactId, version): 108 """Downloads an apk from google maven""" 109 groupPath = groupId.replace('.', '/') 110 artifactDirPath = os.path.join(groupPath, artifactId, version) 111 artifactPath = os.path.join(artifactDirPath, "%s-%s" % (artifactId, version)) 112 cmd("mkdir -p " + artifactDirPath) 113 # download apk 114 cmd("wget -O %s.apk https://dl.google.com/dl/android/maven2/%s.apk" % (artifactPath, artifactPath)) 115 # download pom 116 cmd("wget -O %s.pom https://dl.google.com/dl/android/maven2/%s.pom" % (artifactPath, artifactPath)) 117 118def getManifestPath(mavenArtifactName): 119 """Get the path to the aar's manifest as generated by pom2bp.""" 120 manifestPath = mavenArtifactName 121 for searchPattern in mavenToBpPatternMap: 122 manifestPath = manifestPath.replace(searchPattern, mavenToBpPatternMap[searchPattern]) 123 return "manifests/%s" % manifestPath 124 125def updatePrependLicense(): 126 with open("prepend-license.txt", "w") as f: 127 f.write(prependLicenseTemplate.format(servicesVersion = servicesVersion)) 128 129 130prebuiltDir = os.path.join(getAndroidRoot(), "prebuilts/misc/common/androidx-test") 131chdir(prebuiltDir) 132 133cmd("rm -rf androidx/test") 134cmd("rm -rf manifests") 135 136downloadArtifact("androidx.test", "core", coreVersion) 137downloadArtifact("androidx.test.espresso", "espresso-accessibility", espressoVersion) 138downloadArtifact("androidx.test.espresso", "espresso-core", espressoVersion) 139downloadArtifact("androidx.test.espresso", "espresso-contrib", espressoVersion) 140downloadArtifact("androidx.test.espresso", "espresso-idling-resource", espressoVersion) 141downloadArtifact("androidx.test.espresso", "espresso-intents", espressoVersion) 142downloadArtifact("androidx.test.espresso", "espresso-idling-resource", espressoVersion) 143downloadArtifact("androidx.test.espresso", "espresso-web", espressoVersion) 144downloadArtifact("androidx.test", "monitor", monitorVersion) 145downloadArtifact("androidx.test", "rules", rulesVersion) 146downloadArtifact("androidx.test", "runner", runnerVersion) 147downloadArtifact("androidx.test.ext", "junit", extJUnitVersion) 148downloadArtifact("androidx.test.ext", "truth", extTruthVersion) 149downloadArtifact("androidx.test.janktesthelper", "janktesthelper", jankTestHelperVersion) 150downloadArtifact("androidx.test.services", "storage", servicesVersion) 151downloadApk("androidx.test.services", "test-services", servicesVersion) 152downloadApk("androidx.test", "orchestrator", orchestratorVersion) 153 154 155atxRewriteStr = "" 156for name in mavenToBpPatternMap: 157 atxRewriteStr += "-rewrite %s=%s " % (name, mavenToBpPatternMap[name]) 158for name in extraLibs: 159 atxRewriteStr += "-extra-libs %s=%s " % (name, extraLibs[name]) 160 161updatePrependLicense() 162 163cmd("pom2bp " + atxRewriteStr + 164 # map external maven dependencies to Android module names 165 "-rewrite com.google.truth:truth=truth " + 166 "-rewrite net.sf.kxml:kxml2=kxml2-android " + 167 "-rewrite androidx.lifecycle:lifecycle-common=androidx.lifecycle_lifecycle-common " + 168 "-rewrite androidx.annotation:annotation=androidx.annotation_annotation " + 169 "-rewrite org.hamcrest:hamcrest-integration=hamcrest " + 170 "-rewrite org.hamcrest:hamcrest-core=hamcrest " + 171 "-rewrite javax.inject:javax.inject=jsr330 " + 172 "-rewrite com.google.android.material:material=com.google.android.material_material " + 173 "-rewrite androidx.drawerlayout:drawerlayout=androidx.drawerlayout_drawerlayout " + 174 "-rewrite androidx.viewpager:viewpager=androidx.viewpager_viewpager " + 175 "-rewrite androidx.recyclerview:recyclerview=androidx.recyclerview_recyclerview " + 176 "-rewrite androidx.core:core=androidx.core_core " + 177 "-rewrite androidx.legacy:legacy-support-core-utils=androidx.legacy_legacy-support-core-utils " + 178 "-rewrite androidx.appcompat:appcompat=androidx.appcompat_appcompat " + 179 "-sdk-version current " + 180 "-static-deps " + 181 "-prepend prepend-license.txt " + 182 ". > Android.bp") 183 184