summaryrefslogtreecommitdiff
path: root/common/androidx-media3/update-from-gmaven.py
blob: 5c6b37991ca476e07e996048fda10dd89af208cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python3

# Helper script for updating androidx.media3 prebuilts from maven
#
# Usage:
#   a. Initialize android environment: $ . ./build/envsetup.sh; lunch <target>
#   b. Build pom2bp (needed by this script): $ m pom2bp
#       * If this fails with 'fatal error: thread exhaustion'
#         (and then an *enormous* thread dump), retry the command.
#   b. Update the version numbers in this file (and ensure any new modules are added below)
#   c. Run the script from the Android source root:
#      $ ./prebuilts/misc/common/androidx-media3/update-from-gmaven.py
#
# The script will then:
#   1. Remove the previous artifacts
#   2. Download the aars and poms into a file structure mirroring their maven
#      path
#   3. Extract the AndroidManifest from the aars into the manifests folder
#   4. Run pom2bp to generate the Android.bp
#
# The visibility restrictions in Android.bp must be manually restored.
#
# Manual verification steps:
#   1. Build the 'leaf' imported modules (i.e. the set that ends up depending
#      on *everything* transitively), e.g.
#      $ m androidx.media3.media3-exoplayer-dash androidx.media3.media3-exoplayer androidx.media3.media3-session androidx.media3.media3-transformer androidx.media3.media3-ui

import os
import subprocess
import sys

media3Version="1.1.1"

mavenToBpPatternMap = {
    "androidx.media3:" : "androidx.media3.",
    }

def cmd(args):
   print(args)
   out = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
   if (out.returncode != 0):
      print(out.stderr.decode("utf-8"))
      sys.exit(out.returncode)
   out_string = out.stdout.decode("utf-8")
   print(out_string)
   return out_string

def chdir(path):
   print("cd %s" % path)
   os.chdir(path)

def getAndroidRoot():
   if os.path.isdir(".repo/projects"):
      return os.getcwd()
   elif 'TOP' in os.environ:
      return os.environ['TOP']
   else:
      print("Error: Run from android source root or set TOP envvar")
      sys.exit(-1)

def downloadArtifact(groupId, artifactId, version):
   """Downloads an aar, sources.jar and pom from google maven"""
   groupPath = groupId.replace('.', '/')
   artifactDirPath = os.path.join(groupPath, artifactId, version)
   artifactPath = os.path.join(artifactDirPath, "%s-%s" % (artifactId, version))
   cmd("mkdir -p " + artifactDirPath)
   # download aar
   cmd("wget -O %s.aar https://dl.google.com/dl/android/maven2/%s.aar" % (artifactPath, artifactPath))

   # extract AndroidManifest.xml from aar, into path expected by pom2bp
   manifestDir = getManifestPath("%s:%s" % (groupId,artifactId))
   cmd("mkdir -p " + manifestDir)
   cmd("unzip -o %s.aar AndroidManifest.xml -d %s" % (artifactPath, manifestDir))

   # download pom
   cmd("wget -O %s.pom https://dl.google.com/dl/android/maven2/%s.pom" % (artifactPath, artifactPath))

   # download sources.jar
   cmd("wget -O %s-sources.jar https://dl.google.com/dl/android/maven2/%s-sources.jar" % (artifactPath, artifactPath))

def downloadApk(groupId, artifactId, version):
   """Downloads an apk from google maven"""
   groupPath = groupId.replace('.', '/')
   artifactDirPath = os.path.join(groupPath, artifactId, version)
   artifactPath = os.path.join(artifactDirPath, "%s-%s" % (artifactId, version))
   cmd("mkdir -p " + artifactDirPath)
   # download apk
   cmd("wget -O %s.apk https://dl.google.com/dl/android/maven2/%s.apk" % (artifactPath, artifactPath))
   # download pom
   cmd("wget -O %s.pom https://dl.google.com/dl/android/maven2/%s.pom" % (artifactPath, artifactPath))

def getManifestPath(mavenArtifactName):
  """Get the path to the aar's manifest as generated by pom2bp."""
  manifestPath = mavenArtifactName
  for searchPattern in mavenToBpPatternMap:
    manifestPath = manifestPath.replace(searchPattern, mavenToBpPatternMap[searchPattern])
  return "manifests/%s" % manifestPath

prebuiltDir = os.path.join(getAndroidRoot(), "prebuilts/misc/common/androidx-media3")
chdir(prebuiltDir)

cmd("rm -rf androidx/media3")
cmd("rm -rf manifests")

downloadArtifact("androidx.media3", "media3-common", media3Version)
downloadArtifact("androidx.media3", "media3-container", media3Version)
downloadArtifact("androidx.media3", "media3-database", media3Version)
downloadArtifact("androidx.media3", "media3-datasource", media3Version)
downloadArtifact("androidx.media3", "media3-decoder", media3Version)
downloadArtifact("androidx.media3", "media3-effect", media3Version)
downloadArtifact("androidx.media3", "media3-exoplayer", media3Version)
downloadArtifact("androidx.media3", "media3-exoplayer-dash", media3Version)
downloadArtifact("androidx.media3", "media3-extractor", media3Version)
downloadArtifact("androidx.media3", "media3-muxer", media3Version)
downloadArtifact("androidx.media3", "media3-session", media3Version)
downloadArtifact("androidx.media3", "media3-transformer", media3Version)
downloadArtifact("androidx.media3", "media3-ui", media3Version)

atxRewriteStr = ""
for name in mavenToBpPatternMap:
  atxRewriteStr += "-rewrite %s=%s " % (name, mavenToBpPatternMap[name])

cmd("pom2bp " + atxRewriteStr +
    # map external maven dependencies to Android module names
    "-rewrite androidx.annotation:annotation=androidx.annotation_annotation " +
    "-rewrite androidx.annotation:annotation-experimental=androidx.annotation_annotation-experimental " +
    "-rewrite androidx.collection:collection=androidx.collection_collection " +
    "-rewrite androidx.core:core=androidx.core_core " +
    "-rewrite androidx.media:media=androidx.media_media " +
    "-rewrite androidx.recyclerview:recyclerview=androidx.recyclerview_recyclerview " +
    "-rewrite com.google.guava:guava=guava " +
    "-sdk-version current " +
    "-static-deps " +
    "-prepend prepend-license.txt " +
    ". > Android.bp")