blob: da1821f01c23edd986ef1a64ec2c106e8fbf21f3 [file] [log] [blame]
Alan Viverettef5cb0e62017-11-17 15:15:23 -05001#!/usr/bin/python3
Alan Viverette95f6d362017-04-06 09:40:50 -04002
Alan Viverettecd3de262017-08-14 09:51:30 -04003# This script is used to update platform SDK prebuilts, Support Library, and a variety of other
4# prebuilt libraries used by Android's Makefile builds. For details on how to use this script,
5# visit go/update-prebuilts.
Alan Viverette95f6d362017-04-06 09:40:50 -04006import os, sys, getopt, zipfile, re
Alan Viveretted4527e62017-05-11 15:03:25 -04007import argparse
Jiyong Park1d1c9632018-05-29 17:45:27 +09008import glob
Alan Viveretted4527e62017-05-11 15:03:25 -04009import subprocess
shepshaparde4127cf2019-09-03 16:08:44 -070010from shutil import copyfile, rmtree, which, move
Alan Viveretted4527e62017-05-11 15:03:25 -040011from distutils.version import LooseVersion
Alan Viverettef5cb0e62017-11-17 15:15:23 -050012from functools import reduce
Alan Viverette95f6d362017-04-06 09:40:50 -040013
Alan Viverette45837092017-05-12 14:50:53 -040014current_path = 'current'
Andy Wickhamc3c99af2020-10-26 20:47:58 +000015framework_sdk_target = 'sdk_phone_armv7-win_sdk'
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -040016androidx_dir = os.path.join(current_path, 'androidx')
Alan Viverette3e57a4a2017-08-11 15:49:47 -040017extras_dir = os.path.join(current_path, 'extras')
Alan Viverettec1c32b62017-12-20 09:40:36 -050018buildtools_dir = 'tools'
Jeff Gaston553a4372018-03-29 18:34:22 -040019jetifier_dir = os.path.join(buildtools_dir, 'jetifier', 'jetifier-standalone')
Alan Viverette95f6d362017-04-06 09:40:50 -040020
Jeff Gastona68a8d42018-03-23 14:00:13 -040021temp_dir = os.path.join(os.getcwd(), "support_tmp")
22os.chdir(os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))))
23git_dir = os.getcwd()
24
Alan Viverettecd3de262017-08-14 09:51:30 -040025# See go/fetch_artifact for details on this script.
Alan Viveretted4527e62017-05-11 15:03:25 -040026FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
Jeff Sharkeyabe16df2021-05-25 10:09:03 -060027FETCH_ARTIFACT_BEYOND_CORP = '/usr/bin/fetch_artifact'
Alan Viveretted4527e62017-05-11 15:03:25 -040028
Alan Viverette95f6d362017-04-06 09:40:50 -040029maven_to_make = {
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -040030 # AndroidX
Dustin Lamda15cd42020-04-01 19:06:23 -070031 'androidx.benchmark:benchmark-common': {'name':'androidx.benchmark_benchmark-common', 'path':'androidx/benchmark/benchmark-common'},
32 'androidx.benchmark:benchmark-junit4': {'name':'androidx.benchmark_benchmark-junit4', 'path':'androidx/benchmark/benchmark-junit4'},
33 'androidx.tracing:tracing': {'name':'androidx.tracing_tracing', 'path':'androidx/tracing/tracing'},
34 'androidx.tracing:tracing-ktx': {'name':'androidx.tracing_tracing-ktx', 'path':'androidx/tracing/tracing-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -040035 'androidx.slice:slice-builders': {'name':'androidx.slice_slice-builders', 'path':'androidx/slice/slice-builders'},
36 'androidx.slice:slice-core': {'name':'androidx.slice_slice-core', 'path':'androidx/slice/slice-core'},
37 'androidx.slice:slice-view': {'name':'androidx.slice_slice-view', 'path':'androidx/slice/slice-view'},
Jeff Gaston07a3cf72018-08-24 14:16:34 -040038 'androidx.remotecallback:remotecallback': {'name':'androidx.remotecallback_remotecallback', 'path':'androidx/remotecallback/remotecallback'},
39 'androidx.remotecallback:remotecallback-processor': {'name':'androidx.remotecallback_remotecallback-processor', 'path':'androidx/remotecallback/remotecallback-processor', 'host':True},
Jeff Gastona0bce602018-08-24 14:05:03 -040040 'androidx.versionedparcelable:versionedparcelable': {'name':'androidx.versionedparcelable_versionedparcelable', 'path':'androidx/versionedparcelable'},
41 'androidx.vectordrawable:vectordrawable-animated': {'name':'androidx.vectordrawable_vectordrawable-animated', 'path':'androidx/vectordrawable/vectordrawable-animated'},
Aurimas Liutikasabaa49f2018-09-18 15:30:20 -070042 'androidx.activity:activity': {'name':'androidx.activity_activity', 'path':'androidx/activity/activity'},
Nate Myren1fa47162019-11-12 11:28:06 -080043 'androidx.activity:activity-ktx': {'name':'androidx.activity_activity-ktx', 'path':'androidx/activity/activity-ktx'},
Tony Mak0f658752019-07-19 11:11:05 +010044 'androidx.annotation:annotation': {'name':'androidx.annotation_annotation', 'path':'androidx/annotation/annotation', 'host_and_device' : True},
Dustin Lamda15cd42020-04-01 19:06:23 -070045 'androidx.annotation:annotation-experimental': {'name':'androidx.annotation_annotation-experimental', 'path':'androidx/annotation/annotation-experimental'},
Jeff Gastona0bce602018-08-24 14:05:03 -040046 'androidx.asynclayoutinflater:asynclayoutinflater': {'name':'androidx.asynclayoutinflater_asynclayoutinflater', 'path':'androidx/asynclayoutinflater/asynclayoutinflater'},
Jeff Gastona0bce602018-08-24 14:05:03 -040047 'androidx.collection:collection': {'name':'androidx.collection_collection', 'path':'androidx/collection/collection'},
Nate Myren1fa47162019-11-12 11:28:06 -080048 'androidx.collection:collection-ktx': {'name':'androidx.collection_collection-ktx', 'path':'androidx/collection/collection-ktx'},
Aurimas Liutikasd9124762018-09-28 15:31:35 -070049 'androidx.concurrent:concurrent-futures': {'name':'androidx.concurrent_concurrent-futures', 'path':'androidx/concurrent/concurrent-futures'},
Aurimas Liutikas8af28d32019-06-06 14:04:01 -070050 'androidx.concurrent:concurrent-listenablefuture-callback': {'name':'androidx.concurrent_concurrent-listenablefuture-callback', 'path':'androidx/concurrent/concurrent-listenablefuture-callback'},
51 'androidx.concurrent:concurrent-listenablefuture': {'name':'androidx.concurrent_concurrent-listenablefuture', 'path':'androidx/concurrent/concurrent-listenablefuture'},
Jeff Gastona0bce602018-08-24 14:05:03 -040052 'androidx.core:core': {'name':'androidx.core_core', 'path':'androidx/core/core'},
Yvan Hsueh5efba6e2020-04-30 01:17:47 +080053 'androidx.core:core-animation': {'name':'androidx.core_core-animation', 'path':'androidx/core/core-animation'},
Nate Myren1fa47162019-11-12 11:28:06 -080054 'androidx.core:core-ktx': {'name':'androidx.core_core-ktx', 'path':'androidx/core/core-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -040055 'androidx.contentpaging:contentpaging': {'name':'androidx.contentpaging_contentpaging', 'path':'androidx/contentpaging/contentpaging'},
56 'androidx.coordinatorlayout:coordinatorlayout': {'name':'androidx.coordinatorlayout_coordinatorlayout', 'path':'androidx/coordinatorlayout/coordinatorlayout'},
57 'androidx.legacy:legacy-support-core-ui': {'name':'androidx.legacy_legacy-support-core-ui', 'path':'androidx/legacy/legacy-support-core-ui'},
58 'androidx.legacy:legacy-support-core-utils': {'name':'androidx.legacy_legacy-support-core-utils', 'path':'androidx/legacy/legacy-support-core-utils'},
59 'androidx.cursoradapter:cursoradapter': {'name':'androidx.cursoradapter_cursoradapter', 'path':'androidx/cursoradapter/cursoradapter'},
60 'androidx.browser:browser': {'name':'androidx.browser_browser', 'path':'androidx/browser/browser'},
61 'androidx.customview:customview': {'name':'androidx.customview_customview', 'path':'androidx/customview/customview'},
62 'androidx.documentfile:documentfile': {'name':'androidx.documentfile_documentfile', 'path':'androidx/documentfile/documentfile'},
63 'androidx.drawerlayout:drawerlayout': {'name':'androidx.drawerlayout_drawerlayout', 'path':'androidx/drawerlayout/drawerlayout'},
64 'androidx.dynamicanimation:dynamicanimation': {'name':'androidx.dynamicanimation_dynamicanimation', 'path':'androidx/dynamicanimation/dynamicanimation'},
65 'androidx.emoji:emoji': {'name':'androidx.emoji_emoji', 'path':'androidx/emoji/emoji'},
66 'androidx.emoji:emoji-appcompat': {'name':'androidx.emoji_emoji-appcompat', 'path':'androidx/emoji/emoji-appcompat'},
67 'androidx.emoji:emoji-bundled': {'name':'androidx.emoji_emoji-bundled', 'path':'androidx/emoji/emoji-bundled'},
68 'androidx.exifinterface:exifinterface': {'name':'androidx.exifinterface_exifinterface', 'path':'androidx/exifinterface/exifinterface'},
69 'androidx.fragment:fragment': {'name':'androidx.fragment_fragment', 'path':'androidx/fragment/fragment'},
Nate Myren1fa47162019-11-12 11:28:06 -080070 'androidx.fragment:fragment-ktx': {'name':'androidx.fragment_fragment-ktx', 'path':'androidx/fragment/fragment-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -040071 'androidx.heifwriter:heifwriter': {'name':'androidx.heifwriter_heifwriter', 'path':'androidx/heifwriter/heifwriter'},
72 'androidx.interpolator:interpolator': {'name':'androidx.interpolator_interpolator', 'path':'androidx/interpolator/interpolator'},
73 'androidx.loader:loader': {'name':'androidx.loader_loader', 'path':'androidx/loader/loader'},
74 'androidx.localbroadcastmanager:localbroadcastmanager': {'name':'androidx.localbroadcastmanager_localbroadcastmanager', 'path':'androidx/localbroadcastmanager/localbroadcastmanager'},
75 'androidx.media:media': {'name':'androidx.media_media', 'path':'androidx/media/media'},
Oussama Ben Abdelbaki09fffcb2019-05-13 15:11:37 -040076 'androidx.media2:media2-player': {'name':'androidx.media2_media2-player', 'path':'androidx/media2/media2-player'},
77 'androidx.media2:media2-session': {'name':'androidx.media2_media2-session', 'path':'androidx/media2/media2-session'},
78 'androidx.media2:media2-common': {'name':'androidx.media2_media2-common', 'path':'androidx/media2/media2-common'},
Jeff Gastona0bce602018-08-24 14:05:03 -040079 'androidx.media2:media2-exoplayer': {'name':'androidx.media2_media2-exoplayer', 'path':'androidx/media2/media2-exoplayer'},
Gyumin Sim1752eca2018-12-13 11:34:10 +090080 'androidx.media2:media2-widget': {'name':'androidx.media2_media2-widget', 'path':'androidx/media2/media2-widget'},
Nate Myren1fa47162019-11-12 11:28:06 -080081 'androidx.navigation:navigation-common': {'name':'androidx.navigation_navigation-common', 'path':'androidx/navigation/navigation-common'},
82 'androidx.navigation:navigation-common-ktx': {'name':'androidx.navigation_navigation-common-ktx', 'path':'androidx/navigation/navigation-common-ktx'},
83 'androidx.navigation:navigation-fragment': {'name':'androidx.navigation_navigation-fragment', 'path':'androidx/navigation/navigation-fragment'},
84 'androidx.navigation:navigation-fragment-ktx': {'name':'androidx.navigation_navigation-fragment-ktx', 'path':'androidx/navigation/navigation-fragment-ktx'},
85 'androidx.navigation:navigation-runtime': {'name':'androidx.navigation_navigation-runtime', 'path':'androidx/navigation/navigation-runtime'},
86 'androidx.navigation:navigation-runtime-ktx': {'name':'androidx.navigation_navigation-runtime-ktx', 'path':'androidx/navigation/navigation-runtime-ktx'},
87 'androidx.navigation:navigation-ui': {'name':'androidx.navigation_navigation-ui', 'path':'androidx/navigation/navigation-ui'},
88 'androidx.navigation:navigation-ui-ktx': {'name':'androidx.navigation_navigation-ui-ktx', 'path':'androidx/navigation/navigation-ui-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -040089 'androidx.percentlayout:percentlayout': {'name':'androidx.percentlayout_percentlayout', 'path':'androidx/percentlayout/percentlayout'},
90 'androidx.print:print': {'name':'androidx.print_print', 'path':'androidx/print/print'},
91 'androidx.recommendation:recommendation': {'name':'androidx.recommendation_recommendation', 'path':'androidx/recommendation/recommendation'},
92 'androidx.recyclerview:recyclerview-selection': {'name':'androidx.recyclerview_recyclerview-selection', 'path':'androidx/recyclerview/recyclerview-selection'},
Oussama Ben Abdelbaki00f9b392019-03-18 14:45:12 -040093 'androidx.savedstate:savedstate': {'name':'androidx.savedstate_savedstate', 'path':'androidx/savedstate/savedstate'},
Chang Li0a700902020-10-07 16:03:26 +010094 'androidx.savedstate:savedstate-ktx': {'name':'androidx.savedstate_savedstate-ktx', 'path':'androidx/savedstate/savedstate-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -040095 'androidx.slidingpanelayout:slidingpanelayout': {'name':'androidx.slidingpanelayout_slidingpanelayout', 'path':'androidx/slidingpanelayout/slidingpanelayout'},
96 'androidx.swiperefreshlayout:swiperefreshlayout': {'name':'androidx.swiperefreshlayout_swiperefreshlayout', 'path':'androidx/swiperefreshlayout/swiperefreshlayout'},
97 'androidx.textclassifier:textclassifier': {'name':'androidx.textclassifier_textclassifier', 'path':'androidx/textclassifier/textclassifier'},
98 'androidx.transition:transition': {'name':'androidx.transition_transition', 'path':'androidx/transition/transition'},
99 'androidx.tvprovider:tvprovider': {'name':'androidx.tvprovider_tvprovider', 'path':'androidx/tvprovider/tvprovider'},
100 'androidx.legacy:legacy-support-v13': {'name':'androidx.legacy_legacy-support-v13', 'path':'androidx/legacy/legacy-support-v13'},
101 'androidx.legacy:legacy-preference-v14': {'name':'androidx.legacy_legacy-preference-v14', 'path':'androidx/legacy/legacy-preference-v14'},
102 'androidx.leanback:leanback': {'name':'androidx.leanback_leanback', 'path':'androidx/leanback/leanback'},
103 'androidx.leanback:leanback-preference': {'name':'androidx.leanback_leanback-preference', 'path':'androidx/leanback/leanback-preference'},
104 'androidx.legacy:legacy-support-v4': {'name':'androidx.legacy_legacy-support-v4', 'path':'androidx/legacy/legacy-support-v4'},
105 'androidx.appcompat:appcompat': {'name':'androidx.appcompat_appcompat', 'path':'androidx/appcompat/appcompat'},
Oussama Ben Abdelbaki61551b82019-02-12 15:56:27 -0500106 'androidx.appcompat:appcompat-resources': {'name':'androidx.appcompat_appcompat-resources', 'path':'androidx/appcompat/appcompat-resources'},
Jeff Gastona0bce602018-08-24 14:05:03 -0400107 'androidx.cardview:cardview': {'name':'androidx.cardview_cardview', 'path':'androidx/cardview/cardview'},
108 'androidx.gridlayout:gridlayout': {'name':'androidx.gridlayout_gridlayout', 'path':'androidx/gridlayout/gridlayout'},
109 'androidx.mediarouter:mediarouter': {'name':'androidx.mediarouter_mediarouter', 'path':'androidx/mediarouter/mediarouter'},
110 'androidx.palette:palette': {'name':'androidx.palette_palette', 'path':'androidx/palette/palette'},
111 'androidx.preference:preference': {'name':'androidx.preference_preference', 'path':'androidx/preference/preference'},
112 'androidx.recyclerview:recyclerview': {'name':'androidx.recyclerview_recyclerview', 'path':'androidx/recyclerview/recyclerview'},
113 'androidx.vectordrawable:vectordrawable': {'name':'androidx.vectordrawable_vectordrawable', 'path':'androidx/vectordrawable/vectordrawable'},
114 'androidx.viewpager:viewpager': {'name':'androidx.viewpager_viewpager', 'path':'androidx/viewpager/viewpager'},
Fabian Kozynskie514c122020-03-04 14:41:08 -0500115 'androidx.viewpager2:viewpager2': {'name':'androidx.viewpager2_viewpager2', 'path':'androidx/viewpager2/viewpager2'},
Jeff Gastona0bce602018-08-24 14:05:03 -0400116 'androidx.wear:wear': {'name':'androidx.wear_wear', 'path':'androidx/wear/wear'},
117 'androidx.webkit:webkit': {'name':'androidx.webkit_webkit', 'path':'androidx/webkit/webkit'},
Kevin Chynb6ae85d2018-10-02 12:12:20 -0700118 'androidx.biometric:biometric': {'name':'androidx.biometric_biometric', 'path':'androidx/biometric/biometric'},
Adam He7dec9ff2020-01-27 14:05:45 -0800119 'androidx.autofill:autofill': {'name':'androidx.autofill_autofill', 'path':'androidx/autofill/autofill'},
Alexander Dorokhine2cf37af2020-08-03 14:16:37 -0700120 'androidx.appsearch:appsearch': {'name':'androidx.appsearch_appsearch', 'path':'androidx/appsearch/appsearch'},
Winson Chunge7588602021-01-08 09:57:25 -0800121 'androidx.appsearch:appsearch-local-storage': {'name':'androidx.appsearch_appsearch_local_storage', 'path':'androidx/appsearch/appsearch/appsearch-local-storage'},
Alexander Dorokhineebb69062021-02-03 07:10:32 +0000122 'androidx.appsearch:appsearch-platform-storage': {'name':'androidx.appsearch_appsearch_platform_storage', 'path':'androidx/appsearch/appsearch/appsearch-platform-storage'},
Alexander Dorokhine2cf37af2020-08-03 14:16:37 -0700123 'androidx.appsearch:appsearch-compiler': {'name':'androidx.appsearch_appsearch-compiler', 'path':'androidx/appsearch/appsearch-compiler', 'host' : True},
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400124
125 # AndroidX for Multidex
Jeff Gastona0bce602018-08-24 14:05:03 -0400126 'androidx.multidex:multidex': {'name':'androidx-multidex_multidex', 'path':'androidx/multidex/multidex'},
127 'androidx.multidex:multidex-instrumentation': {'name':'androidx-multidex_multidex-instrumentation', 'path':'androidx/multidex/multidex-instrumentation'},
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400128
129 # AndroidX for Constraint Layout
Jeff Gastona0bce602018-08-24 14:05:03 -0400130 'androidx.constraintlayout:constraintlayout': {'name':'androidx-constraintlayout_constraintlayout', 'path':'androidx/constraintlayout/constraintlayout'},
131 'androidx.constraintlayout:constraintlayout-solver': {'name':'androidx-constraintlayout_constraintlayout-solver', 'path':'androidx/constraintlayout/constraintlayout-solver'},
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400132
133 # AndroidX for Architecture Components
Jeff Gastona0bce602018-08-24 14:05:03 -0400134 'androidx.arch.core:core-common': {'name':'androidx.arch.core_core-common', 'path':'androidx/arch/core/core-common'},
135 'androidx.arch.core:core-runtime': {'name':'androidx.arch.core_core-runtime', 'path':'androidx/arch/core/core-runtime'},
136 'androidx.lifecycle:lifecycle-common': {'name':'androidx.lifecycle_lifecycle-common', 'path':'androidx/lifecycle/lifecycle-common'},
137 'androidx.lifecycle:lifecycle-common-java8': {'name':'androidx.lifecycle_lifecycle-common-java8', 'path':'androidx/lifecycle/lifecycle-common-java8'},
138 'androidx.lifecycle:lifecycle-extensions': {'name':'androidx.lifecycle_lifecycle-extensions', 'path':'androidx/lifecycle/lifecycle-extensions'},
139 'androidx.lifecycle:lifecycle-livedata': {'name':'androidx.lifecycle_lifecycle-livedata', 'path':'androidx/lifecycle/lifecycle-livedata'},
Nate Myren1fa47162019-11-12 11:28:06 -0800140 'androidx.lifecycle:lifecycle-livedata-ktx': {'name':'androidx.lifecycle_lifecycle-livedata-ktx', 'path':'androidx/lifecycle/lifecycle-livedata-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -0400141 'androidx.lifecycle:lifecycle-livedata-core': {'name':'androidx.lifecycle_lifecycle-livedata-core', 'path':'androidx/lifecycle/lifecycle-livedata-core'},
Nate Myren1fa47162019-11-12 11:28:06 -0800142 'androidx.lifecycle:lifecycle-livedata-core-ktx': {'name':'androidx.lifecycle_lifecycle-livedata-core-ktx', 'path':'androidx/lifecycle/lifecycle-livedata-core-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -0400143 'androidx.lifecycle:lifecycle-process': {'name':'androidx.lifecycle_lifecycle-process', 'path':'androidx/lifecycle/lifecycle-process'},
144 'androidx.lifecycle:lifecycle-runtime': {'name':'androidx.lifecycle_lifecycle-runtime', 'path':'androidx/lifecycle/lifecycle-runtime'},
Nate Myren1fa47162019-11-12 11:28:06 -0800145 'androidx.lifecycle:lifecycle-runtime-ktx': {'name':'androidx.lifecycle_lifecycle-runtime-ktx', 'path':'androidx/lifecycle/lifecycle-runtime-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -0400146 'androidx.lifecycle:lifecycle-service': {'name':'androidx.lifecycle_lifecycle-service', 'path':'androidx/lifecycle/lifecycle-service'},
147 'androidx.lifecycle:lifecycle-viewmodel': {'name':'androidx.lifecycle_lifecycle-viewmodel', 'path':'androidx/lifecycle/lifecycle-viewmodel'},
Nate Myren1fa47162019-11-12 11:28:06 -0800148 'androidx.lifecycle:lifecycle-viewmodel-ktx': {'name':'androidx.lifecycle_lifecycle-viewmodel-ktx', 'path':'androidx/lifecycle/lifecycle-viewmodel-ktx'},
Aurimas Liutikasb5836e22019-09-06 09:00:45 -0700149 'androidx.lifecycle:lifecycle-viewmodel-savedstate': {'name':'androidx.lifecycle_lifecycle-viewmodel-savedstate', 'path':'androidx/lifecycle/lifecycle-viewmodel-savedstate'},
Jeff Gastona0bce602018-08-24 14:05:03 -0400150 'androidx.paging:paging-common': {'name':'androidx.paging_paging-common', 'path':'androidx/paging/paging-common'},
Aurimas Liutikasb5836e22019-09-06 09:00:45 -0700151 'androidx.paging:paging-common-ktx': {'name':'androidx.paging_paging-common-ktx', 'path':'androidx/paging/paging-common-ktx'},
Jeff Gastona0bce602018-08-24 14:05:03 -0400152 'androidx.paging:paging-runtime': {'name':'androidx.paging_paging-runtime', 'path':'androidx/paging/paging-runtime'},
153 'androidx.sqlite:sqlite': {'name':'androidx.sqlite_sqlite', 'path':'androidx/sqlite/sqlite'},
154 'androidx.sqlite:sqlite-framework': {'name':'androidx.sqlite_sqlite-framework', 'path':'androidx/sqlite/sqlite-framework'},
Tony Mak0f658752019-07-19 11:11:05 +0100155 'androidx.room:room-common': {'name':'androidx.room_room-common', 'path':'androidx/room/room-common', 'host_and_device' : True},
156 'androidx.room:room-compiler': {'name':'androidx.room_room-compiler', 'path':'androidx/room/room-compiler', 'host' : True},
157 'androidx.room:room-migration': {'name':'androidx.room_room-migration', 'path':'androidx/room/room-migration', 'host_and_device' : True},
Jeff Gastona0bce602018-08-24 14:05:03 -0400158 'androidx.room:room-runtime': {'name':'androidx.room_room-runtime', 'path':'androidx/room/room-runtime'},
159 'androidx.room:room-testing': {'name':'androidx.room_room-testing', 'path':'androidx/room/room-testing'},
Ming-Shin Lu97a1dc02020-09-11 21:55:21 +0800160 'androidx.room:room-compiler-processing': {'name':'androidx.room_room-compiler-processing', 'path':'androidx/room/room-compiler-processing', 'host' : True},
Chang Li0a700902020-10-07 16:03:26 +0100161 'androidx.work:work-runtime': {'name':'androidx.work_work-runtime', 'path':'androidx/work/work-runtime'},
162 'androidx.work:work-runtime-ktx': {'name':'androidx.work_work-runtime-ktx', 'path':'androidx/work/work-runtime-ktx'},
163 'androidx.work:work-testing': {'name':'androidx.work_work-testing', 'path':'androidx/work/work-testing'},
Allenab72f012018-01-29 18:10:23 -0800164
Alan Viverette19ecd502018-01-05 13:52:16 -0500165 # Lifecycle
Alan Viverettec7e88f12018-02-13 15:29:50 -0500166 # Missing dependencies:
167 # - auto-common
168 # - javapoet
Jeff Gastona0bce602018-08-24 14:05:03 -0400169 #'android.arch.lifecycle:compiler': {'name':'android-arch-lifecycle-compiler', 'path':'arch-lifecycle/compiler'},
Alan Viverettec7e88f12018-02-13 15:29:50 -0500170 # Missing dependencies:
171 # - reactive-streams
Jeff Gastona0bce602018-08-24 14:05:03 -0400172 #'android.arch.lifecycle:reactivestreams': 'android-arch-lifecycle-reactivestreams','arch-lifecycle/reactivestreams',
Allenab72f012018-01-29 18:10:23 -0800173
Alan Viverette19ecd502018-01-05 13:52:16 -0500174 # Room
Alan Viverettec7e88f12018-02-13 15:29:50 -0500175 # Missing dependencies:
176 # - auto-common
177 # - javapoet
178 # - antlr4
179 # - kotlin-metadata
180 # - commons-codec
Jeff Gastona0bce602018-08-24 14:05:03 -0400181 #'android.arch.persistence.room:compiler': {'name':'android-arch-room-compiler', 'path':'arch-room/compiler'},
Alan Viverettec7e88f12018-02-13 15:29:50 -0500182 # Missing dependencies:
183 # - rxjava
Jeff Gastona0bce602018-08-24 14:05:03 -0400184 #'android.arch.persistence.room:rxjava2': {'name':'android-arch-room-rxjava2', 'path':'arch-room/rxjava2'},
Allenab72f012018-01-29 18:10:23 -0800185
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400186 # Third-party dependencies
Jeff Gastona0bce602018-08-24 14:05:03 -0400187 'com.google.android:flexbox': {'name':'flexbox', 'path':'flexbox'},
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400188
Jeff Gastonfa7c7e82018-04-06 13:35:48 -0400189 # Androidx Material Design Components
Jeff Gastona0bce602018-08-24 14:05:03 -0400190 'com.google.android.material:material': {'name':'com.google.android.material_material', 'path':'com/google/android/material/material'},
Alan Viverette95f6d362017-04-06 09:40:50 -0400191}
192
193# Always remove these files.
194blacklist_files = [
195 'annotations.zip',
196 'public.txt',
197 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -0400198 'AndroidManifest.xml',
Alan Viverettef48d9b42017-08-17 14:45:46 -0400199 os.path.join('libs','noto-emoji-compat-java.jar')
Alan Viverette95f6d362017-04-06 09:40:50 -0400200]
201
Alan Viverette44ad4e42017-08-09 17:45:00 -0400202artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -0400203
Alan Viverettec960cfb2017-12-04 13:09:22 -0500204
205class MavenLibraryInfo:
Jeff Gastonc302bb92018-03-23 13:53:48 -0400206 def __init__(self, key, group_id, artifact_id, version, dir, repo_dir, file):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500207 self.key = key
208 self.group_id = group_id
209 self.artifact_id = artifact_id
210 self.version = version
Jeff Gastonc302bb92018-03-23 13:53:48 -0400211 self.dir = dir
Alan Viverettec960cfb2017-12-04 13:09:22 -0500212 self.repo_dir = repo_dir
213 self.file = file
214
215
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500216def print_e(*args, **kwargs):
217 print(*args, file=sys.stderr, **kwargs)
218
Alan Viverette95f6d362017-04-06 09:40:50 -0400219
220def touch(fname, times=None):
221 with open(fname, 'a'):
222 os.utime(fname, times)
223
224
Alan Viverette45837092017-05-12 14:50:53 -0400225def path(*path_parts):
226 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
227
228
Alan Viverettecd3de262017-08-14 09:51:30 -0400229def flatten(list):
230 return reduce((lambda x, y: "%s %s" % (x, y)), list)
231
232
Alan Viverette45837092017-05-12 14:50:53 -0400233def rm(path):
234 if os.path.isdir(path):
235 rmtree(path)
236 elif os.path.exists(path):
237 os.remove(path)
238
239
240def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400241 if os.path.exists(dst_path):
Alan Viverettecd3de262017-08-14 09:51:30 -0400242 rm(dst_path)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400243 if not os.path.exists(os.path.dirname(dst_path)):
244 os.makedirs(os.path.dirname(dst_path))
Jiyong Park1d1c9632018-05-29 17:45:27 +0900245 for f in (glob.glob(src_path)):
246 if '*' in dst_path:
247 dst = os.path.join(os.path.dirname(dst_path), os.path.basename(f))
248 else:
249 dst = dst_path
shepshaparde4127cf2019-09-03 16:08:44 -0700250 move(f, dst)
Alan Viverette45837092017-05-12 14:50:53 -0400251
252
Jeff Gastonc302bb92018-03-23 13:53:48 -0400253def detect_artifacts(maven_repo_dirs):
Alan Viveretted4527e62017-05-11 15:03:25 -0400254 maven_lib_info = {}
255
Dan Willemsen814152e2017-11-06 13:22:11 -0800256 # Find the latest revision for each artifact, remove others
Jeff Gastonc302bb92018-03-23 13:53:48 -0400257 for repo_dir in maven_repo_dirs:
Dan Willemsen814152e2017-11-06 13:22:11 -0800258 for root, dirs, files in os.walk(repo_dir):
259 for file in files:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500260 if file[-4:] == ".pom":
261 # Read the POM (hack hack hack).
262 group_id = ''
263 artifact_id = ''
264 version = ''
265 file = os.path.join(root, file)
266 with open(file) as pom_file:
267 for line in pom_file:
268 if line[:11] == ' <groupId>':
269 group_id = line[11:-11]
270 elif line[:14] == ' <artifactId>':
271 artifact_id = line[14:-14]
272 elif line[:11] == ' <version>':
273 version = line[11:-11]
274 if group_id == '' or artifact_id == '' or version == '':
275 print_e('Failed to find Maven artifact data in ' + file)
276 continue
Alan Viverette95f6d362017-04-06 09:40:50 -0400277
Alan Viverettec960cfb2017-12-04 13:09:22 -0500278 # Locate the artifact.
279 artifact_file = file[:-4]
280 if os.path.exists(artifact_file + '.jar'):
281 artifact_file = artifact_file + '.jar'
282 elif os.path.exists(artifact_file + '.aar'):
283 artifact_file = artifact_file + '.aar'
284 else:
285 print_e('Failed to find artifact for ' + artifact_file)
286 continue
287
288 # Make relative to root.
289 artifact_file = artifact_file[len(root) + 1:]
290
291 # Find the mapping.
292 group_artifact = group_id + ':' + artifact_id
Jeff Gaston3e622ba2018-03-26 23:24:46 -0400293 if group_artifact in maven_to_make:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500294 key = group_artifact
Jeff Gaston3e622ba2018-03-26 23:24:46 -0400295 elif artifact_id in maven_to_make:
296 key = artifact_id
Alan Viverettec960cfb2017-12-04 13:09:22 -0500297 else:
298 print_e('Failed to find artifact mapping for ' + group_artifact)
299 continue
300
301 # Store the latest version.
302 version = LooseVersion(version)
303 if key not in maven_lib_info \
304 or version > maven_lib_info[key].version:
305 maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
306 root, repo_dir, artifact_file)
Alan Viverette95f6d362017-04-06 09:40:50 -0400307
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400308 return maven_lib_info
309
310
Colin Cross59b59db2018-04-24 13:58:05 -0700311def transform_maven_repos(maven_repo_dirs, transformed_dir, extract_res=True, include_static_deps=True):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400312 cwd = os.getcwd()
313
314 # Use a temporary working directory.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400315 maven_lib_info = detect_artifacts(maven_repo_dirs)
Jeff Gastona68a8d42018-03-23 14:00:13 -0400316 working_dir = temp_dir
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400317
Alan Viverettec960cfb2017-12-04 13:09:22 -0500318 if not maven_lib_info:
319 print_e('Failed to detect artifacts')
320 return False
321
Jeff Gastonc302bb92018-03-23 13:53:48 -0400322 # extract some files (for example, AndroidManifest.xml) from any relevant artifacts
Alan Viveretted4527e62017-05-11 15:03:25 -0400323 for info in maven_lib_info.values():
Alan Viveretted8ce7222017-12-11 17:24:43 -0500324 transform_maven_lib(working_dir, info, extract_res)
Dan Willemsen814152e2017-11-06 13:22:11 -0800325
Colin Cross74683bc2018-04-11 17:36:18 -0700326 # generate a single Android.bp that specifies to use all of the above artifacts
327 makefile = os.path.join(working_dir, 'Android.bp')
Alan Viverette4ec9a172018-02-20 16:19:31 -0500328 with open(makefile, 'w') as f:
Anton Hanssonbd2021c2021-04-06 19:21:34 +0000329 args = ["pom2bp"]
Anton Hansson8e84b612021-04-06 14:24:29 +0100330 args.extend(["-sdk-version", "30"])
Anton Hanssonbd2021c2021-04-06 19:21:34 +0000331 args.extend(["-default-min-sdk-version", "24"])
Jeff Gaston1cc06092018-03-28 15:51:02 -0400332 if include_static_deps:
333 args.append("-static-deps")
Jeff Gaston54868ee2018-04-25 19:43:35 -0400334 rewriteNames = sorted([name for name in maven_to_make if ":" in name] + [name for name in maven_to_make if ":" not in name])
Jeff Gastona0bce602018-08-24 14:05:03 -0400335 args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name]['name'] for name in rewriteNames])
Colin Cross5542fc22021-04-14 11:36:46 -0700336 args.extend(["-rewrite=^auto-common$=auto_common"])
337 args.extend(["-rewrite=^auto-value-annotations$=auto_value_annotations"])
Dustin Lamda15cd42020-04-01 19:06:23 -0700338 args.extend(["-rewrite=^monitor$=androidx.test.monitor"])
339 args.extend(["-rewrite=^rules$=androidx.test.rules"])
340 args.extend(["-rewrite=^runner$=androidx.test.runner"])
Yiqun Wu9e020fe2020-11-18 21:16:08 +0000341 args.extend(["-rewrite=^com.squareup:javapoet$=javapoet"])
Aurimas Liutikasd9124762018-09-28 15:31:35 -0700342 args.extend(["-rewrite=^com.google.guava:listenablefuture$=guava-listenablefuture-prebuilt-jar"])
Tony Mak0f658752019-07-19 11:11:05 +0100343 args.extend(["-rewrite=^sqlite-jdbc$=xerial-sqlite-jdbc"])
Tony Mak0f658752019-07-19 11:11:05 +0100344 args.extend(["-rewrite=^gson$=gson-prebuilt-jar"])
Sunny Goyal6667ddb2020-03-03 15:15:54 -0800345 args.extend(["-rewrite=^com.intellij:annotations$=jetbrains-annotations"])
Sunny Goyal6667ddb2020-03-03 15:15:54 -0800346 args.extend(["-extra-static-libs=androidx.room_room-compiler=guava-21.0"])
Jeff Gaston07a3cf72018-08-24 14:16:34 -0400347 args.extend(["-host=" + name for name in maven_to_make if maven_to_make[name].get('host')])
Tony Mak0f658752019-07-19 11:11:05 +0100348 args.extend(["-host-and-device=" + name for name in maven_to_make if maven_to_make[name].get('host_and_device')])
Colin Cross21060072018-04-24 13:52:29 -0700349 # these depend on GSON which is not in AOSP
Tony Mak0f658752019-07-19 11:11:05 +0100350 args.extend(["-exclude=android-arch-room-migration",
Colin Cross21060072018-04-24 13:52:29 -0700351 "-exclude=android-arch-room-testing"])
Jeff Gaston7ec1d8f2018-03-27 00:25:12 -0400352 args.extend(["."])
Dan Willemsen814152e2017-11-06 13:22:11 -0800353 subprocess.check_call(args, stdout=f, cwd=working_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400354
355 # Replace the old directory.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400356 output_dir = os.path.join(cwd, transformed_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400357 mv(working_dir, output_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500358 return True
Alan Viverette95f6d362017-04-06 09:40:50 -0400359
Jeff Gastonc302bb92018-03-23 13:53:48 -0400360# moves <artifact_info> (of type MavenLibraryInfo) into the appropriate part of <working_dir> , and possibly unpacks any necessary included files
Alan Viveretted8ce7222017-12-11 17:24:43 -0500361def transform_maven_lib(working_dir, artifact_info, extract_res):
Dan Willemsen814152e2017-11-06 13:22:11 -0800362 # Move library into working dir
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400363 new_dir = os.path.normpath(os.path.join(working_dir, os.path.relpath(artifact_info.dir, artifact_info.repo_dir)))
Jeff Gastonc302bb92018-03-23 13:53:48 -0400364 mv(artifact_info.dir, new_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800365
Alan Viverettec960cfb2017-12-04 13:09:22 -0500366 matcher = artifact_pattern.match(artifact_info.file)
367 maven_lib_name = artifact_info.key
Alan Viveretted4527e62017-05-11 15:03:25 -0400368 maven_lib_vers = matcher.group(2)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500369 maven_lib_type = artifact_info.file[-3:]
Alan Viveretted4527e62017-05-11 15:03:25 -0400370
Jeff Gastona0bce602018-08-24 14:05:03 -0400371 make_lib_name = maven_to_make[artifact_info.key]['name']
372 make_dir_name = maven_to_make[artifact_info.key]['path']
Alan Viveretted4527e62017-05-11 15:03:25 -0400373
Alan Viverette129555e2018-01-30 09:57:57 -0500374 artifact_file = os.path.join(new_dir, artifact_info.file)
375
Colin Cross59b59db2018-04-24 13:58:05 -0700376 if maven_lib_type == "aar":
377 if extract_res:
378 target_dir = os.path.join(working_dir, make_dir_name)
379 if not os.path.exists(target_dir):
380 os.makedirs(target_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800381
Alan Viverettec960cfb2017-12-04 13:09:22 -0500382 process_aar(artifact_file, target_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400383
Colin Cross59b59db2018-04-24 13:58:05 -0700384 with zipfile.ZipFile(artifact_file) as zip:
385 manifests_dir = os.path.join(working_dir, "manifests")
386 zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
Dan Willemsend677b602017-11-08 22:13:17 -0800387
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500388 print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
Alan Viveretted4527e62017-05-11 15:03:25 -0400389
Alan Viverette95f6d362017-04-06 09:40:50 -0400390
Alan Viverettec960cfb2017-12-04 13:09:22 -0500391def process_aar(artifact_file, target_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400392 # Extract AAR file to target_dir.
393 with zipfile.ZipFile(artifact_file) as zip:
394 zip.extractall(target_dir)
395
Dan Willemsen814152e2017-11-06 13:22:11 -0800396 # Remove classes.jar
Alan Viverette95f6d362017-04-06 09:40:50 -0400397 classes_jar = os.path.join(target_dir, "classes.jar")
398 if os.path.exists(classes_jar):
Dan Willemsen814152e2017-11-06 13:22:11 -0800399 os.remove(classes_jar)
Alan Viverette95f6d362017-04-06 09:40:50 -0400400
401 # Remove or preserve empty dirs.
402 for root, dirs, files in os.walk(target_dir):
403 for dir in dirs:
404 dir_path = os.path.join(root, dir)
405 if not os.listdir(dir_path):
406 os.rmdir(dir_path)
407
408 # Remove top-level cruft.
409 for file in blacklist_files:
410 file_path = os.path.join(target_dir, file)
411 if os.path.exists(file_path):
412 os.remove(file_path)
413
Alan Viverettef17c9402017-07-19 12:57:40 -0400414
Alan Viverettecd3de262017-08-14 09:51:30 -0400415def fetch_artifact(target, build_id, artifact_path):
Jeff Sharkeyabe16df2021-05-25 10:09:03 -0600416 global args
Jiyong Park1d1c9632018-05-29 17:45:27 +0900417 download_to = os.path.join('.', os.path.dirname(artifact_path))
418 print('Fetching %s from %s ...' % (artifact_path, target))
419 if not os.path.exists(download_to):
420 os.makedirs(download_to)
Jeff Sharkeyabe16df2021-05-25 10:09:03 -0600421 if args.beyond_corp:
422 fetch_cmd = [FETCH_ARTIFACT_BEYOND_CORP, '--use_oauth2',
423 '--bid', str(build_id), '--target', target, artifact_path, download_to]
424 else:
425 fetch_cmd = [FETCH_ARTIFACT,
426 '--bid', str(build_id), '--target', target, artifact_path, download_to]
Jeff Gaston97a9b762018-10-02 15:15:12 -0400427 print("Running: " + ' '.join(fetch_cmd))
Alan Viverette45837092017-05-12 14:50:53 -0400428 try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400429 subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
Alan Viverette45837092017-05-12 14:50:53 -0400430 except subprocess.CalledProcessError:
Jeff Gaston13e38412018-02-06 14:45:36 -0500431 print_e('FAIL: Unable to retrieve %s artifact for build ID %s' % (artifact_path, build_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500432 print_e('Please make sure you are authenticated for build server access!')
Alan Viverette45837092017-05-12 14:50:53 -0400433 return None
434 return artifact_path
435
436
Anton Hansson1d01a032018-04-09 10:29:37 +0100437def fetch_artifacts(target, build_id, artifact_dict):
438 for artifact, target_path in artifact_dict.items():
439 artifact_path = fetch_artifact(target, build_id.url_id, artifact)
440 if not artifact_path:
441 return False
442 mv(artifact_path, target_path)
443 return True
444
445
Alan Viverette129555e2018-01-30 09:57:57 -0500446def extract_artifact(artifact_path):
Alan Viverette45837092017-05-12 14:50:53 -0400447 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400448 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400449 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400450 zipFile.extractall(repo_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400451 return repo_dir
452
453
Alan Viverette7e897e22018-03-09 15:24:10 -0500454def fetch_and_extract(target, build_id, file, artifact_path=None):
455 if not artifact_path:
456 artifact_path = fetch_artifact(target, build_id, file)
Alan Viverette129555e2018-01-30 09:57:57 -0500457 if not artifact_path:
458 return None
459 return extract_artifact(artifact_path)
460
461
Jeff Gaston08a2ee42018-05-07 18:08:41 -0400462def update_androidx(target, build_id, local_file):
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400463 if build_id:
Anthony Chenacbb4872018-07-02 11:22:48 -0700464 repo_file = 'top-of-tree-m2repository-all-%s.zip' % build_id.fs_id
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400465 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file, None)
466 else:
467 repo_dir = fetch_and_extract(target, None, None, local_file)
468 if not repo_dir:
469 print_e('Failed to extract AndroidX repository')
470 return False
471
Tony Mak0f658752019-07-19 11:11:05 +0100472 # Keep JavaPlugins.bp file untounched.
473 java_plugins_bp_path = os.path.join(androidx_dir, 'JavaPlugins.bp')
474 tmp_java_plugins_bp_path = os.path.join('/tmp', 'JavaPlugins.bp')
475 mv(java_plugins_bp_path, tmp_java_plugins_bp_path)
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400476
Tony Mak0f658752019-07-19 11:11:05 +0100477 # Transform the repo archive into a Makefile-compatible format.
478 if not transform_maven_repos([repo_dir], androidx_dir, extract_res=False):
479 return False
480
481 # Import JavaPlugins.bp in Android.bp.
482 makefile = os.path.join(androidx_dir, 'Android.bp')
483 with open(makefile, "a+") as f:
484 f.write('\nbuild = ["JavaPlugins.bp"]\n')
485 mv(tmp_java_plugins_bp_path, java_plugins_bp_path)
486
487 return True
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400488
Alan Viverette6dc45752020-04-16 14:56:20 +0000489
Jeff Gaston782c3e32018-02-06 14:36:17 -0500490def update_jetifier(target, build_id):
491 repo_file = 'jetifier-standalone.zip'
492 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file)
493 if not repo_dir:
494 print_e('Failed to extract Jetifier')
495 return False
496
497 rm(jetifier_dir)
Jeff Gaston553a4372018-03-29 18:34:22 -0400498 mv(os.path.join(repo_dir, 'jetifier-standalone'), jetifier_dir)
499 os.chmod(os.path.join(jetifier_dir, 'bin', 'jetifier-standalone'), 0o755)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500500 return True
501
Alan Viverette7e897e22018-03-09 15:24:10 -0500502
Alan Viverette6dc45752020-04-16 14:56:20 +0000503def update_constraint(local_file):
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400504 repo_dir = extract_artifact(local_file)
505 if not repo_dir:
Alan Viverette6dc45752020-04-16 14:56:20 +0000506 print_e('Failed to extract Constraint Layout')
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400507 return False
508 return transform_maven_repos([repo_dir], os.path.join(extras_dir, 'constraint-layout-x'), extract_res=False)
509
Alan Viverette45837092017-05-12 14:50:53 -0400510
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400511def update_material(file):
512 design_dir = extract_artifact(file)
513 if not design_dir:
Alan Viverette6dc45752020-04-16 14:56:20 +0000514 print_e('Failed to extract Material Design Components')
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400515 return False
516
517 # Don't bother extracting resources -- this should only be used with AAPT2.
518 return transform_maven_repos([design_dir],
519 os.path.join(extras_dir, 'material-design-x'), extract_res=False)
520
521
Anton Hansson1d01a032018-04-09 10:29:37 +0100522def update_framework(build_id, sdk_dir):
Anton Hansson83f92172020-03-26 11:12:03 +0000523 api_scope_list = ['public', 'system', 'test', 'module-lib', 'system-server']
Sundong Ahn66091f22018-08-29 18:54:28 +0900524 if sdk_dir == 'current':
525 api_scope_list.append('core')
526
527 for api_scope in api_scope_list:
Jiyong Park1d1c9632018-05-29 17:45:27 +0900528 target_dir = path(sdk_dir, api_scope)
Sundong Ahn66091f22018-08-29 18:54:28 +0900529 if api_scope == 'core':
530 artifact_to_path = {'core.current.stubs.jar': path(target_dir, 'android.jar')}
531 else:
532 artifact_to_path = {'apistubs/android/' + api_scope + '/*.jar': path(target_dir, '*')}
Anton Hansson1d01a032018-04-09 10:29:37 +0100533
534 if not fetch_artifacts(framework_sdk_target, build_id, artifact_to_path):
535 return False
536
Jiyong Park1d1c9632018-05-29 17:45:27 +0900537 if api_scope == 'public':
Anton Hansson1d01a032018-04-09 10:29:37 +0100538 # Fetch a few artifacts from the public sdk.
Sundong Ahn66091f22018-08-29 18:54:28 +0900539 artifact = 'sdk-repo-linux-platforms-%s.zip' % build_id.fs_id
Anton Hansson1d01a032018-04-09 10:29:37 +0100540 artifact_path = fetch_artifact(framework_sdk_target, build_id.url_id, artifact)
Anton Hansson9709fd42018-04-03 16:52:13 +0100541 if not artifact_path:
542 return False
Anton Hansson1d01a032018-04-09 10:29:37 +0100543
544 with zipfile.ZipFile(artifact_path) as zipFile:
Colin Crossd338d702020-07-10 19:17:54 -0700545 extra_files = [
546 'android.jar',
547 'framework.aidl',
548 'uiautomator.jar',
549 'core-for-system-modules.jar',
550 'data/annotations.zip',
551 'data/api-versions.xml']
552 for filename in extra_files:
Anton Hanssona26e4812020-03-25 12:54:39 +0000553 matches = list(filter(lambda path: filename in path, zipFile.namelist()))
554 if len(matches) != 1:
555 print_e('Expected 1 file named \'%s\' in zip %s, found %d' %
556 (filename, zipFile.filename, len(matches)))
557 return False
558 zip_path = matches[0]
559 src_path = zipFile.extract(zip_path)
560 dst_path = path(target_dir, filename)
561 mv(src_path, dst_path)
Anton Hansson9709fd42018-04-03 16:52:13 +0100562
Alan Viverettecd3de262017-08-14 09:51:30 -0400563 return True
Alan Viverette45837092017-05-12 14:50:53 -0400564
Sundong Ahncc34cc32018-07-11 15:18:47 +0900565def update_makefile(build_id):
566 template = '"%s",\n\
567 "current"'
568 makefile = os.path.join(git_dir, 'Android.bp')
569
570 with open(makefile, 'r+') as f:
571 contents = f.read().replace('"current"', template % build_id)
572 f.seek(0)
573 f.write(contents)
574
575 return True
Alan Viverette45837092017-05-12 14:50:53 -0400576
Anton Hansson1d01a032018-04-09 10:29:37 +0100577def finalize_sdk(build_id, sdk_version):
578 target_finalize_dir = '%d' % sdk_version
Anton Hansson9709fd42018-04-03 16:52:13 +0100579
Anton Hanssonaa554c02020-04-30 14:06:20 +0100580 for api_scope in ['public', 'system', 'test', 'module-lib', 'system-server']:
Sundong Ahn66091f22018-08-29 18:54:28 +0900581 artifact_to_path = {'apistubs/android/' + api_scope + '/api/*.txt':
582 path(target_finalize_dir, api_scope, 'api', '*')}
583
584 if not fetch_artifacts(framework_sdk_target, build_id, artifact_to_path):
585 return False
586
587 return update_framework(build_id, target_finalize_dir) \
Sundong Ahncc34cc32018-07-11 15:18:47 +0900588 and update_makefile(target_finalize_dir)
Anton Hansson9709fd42018-04-03 16:52:13 +0100589
Anton Hansson9709fd42018-04-03 16:52:13 +0100590
Anton Hansson1d01a032018-04-09 10:29:37 +0100591def update_framework_current(build_id):
592 return update_framework(build_id, current_path)
Anton Hansson9709fd42018-04-03 16:52:13 +0100593
594
Alan Viverettec1c32b62017-12-20 09:40:36 -0500595def update_buildtools(target, arch, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500596 artifact_path = fetch_and_extract(target, build_id.url_id,
597 "sdk-repo-%s-build-tools-%s.zip" % (arch, build_id.fs_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500598 if not artifact_path:
599 return False
600
601 top_level_dir = os.listdir(artifact_path)[0]
602 src_path = os.path.join(artifact_path, top_level_dir)
603 dst_path = path(buildtools_dir, arch)
Colin Cross20648d32021-02-12 13:30:51 -0800604
605 # There are a few libraries that have been manually added to the
606 # build tools, copy them from the destination back to the source
607 # before the destination is overwritten.
608 files_to_save = (
609 'lib64/libconscrypt_openjdk_jni.dylib',
610 'lib64/libconscrypt_openjdk_jni.so',
611 'bin/lib64/libwinpthread-1.dll',
612 )
613 for file in files_to_save:
614 src_file = os.path.join(dst_path, file)
615 dst_file = os.path.join(src_path, file)
616 if os.path.exists(dst_path):
617 mv(src_file, dst_file)
618
Alan Viverettec1c32b62017-12-20 09:40:36 -0500619 mv(src_path, dst_path)
620
621 # Move all top-level files to /bin and make them executable
622 bin_path = path(dst_path, 'bin')
623 top_level_files = filter(lambda e: os.path.isfile(path(dst_path, e)), os.listdir(dst_path))
624 for file in top_level_files:
625 src_file = path(dst_path, file)
626 dst_file = path(bin_path, file)
627 mv(src_file, dst_file)
628 os.chmod(dst_file, 0o755)
629
Colin Cross6673ceb2021-02-12 13:14:14 -0800630 # Make the files under lld-bin executable
631 lld_bin_files = os.listdir(os.path.join(dst_path, 'lld-bin'))
632 for file in lld_bin_files:
633 os.chmod(os.path.join(dst_path, 'lld-bin', file), 0o755)
634
Alan Viverettec1c32b62017-12-20 09:40:36 -0500635 # Remove renderscript
636 rm(path(dst_path, 'renderscript'))
637
638 return True
639
640
Alan Viverettecd3de262017-08-14 09:51:30 -0400641def append(text, more_text):
642 if text:
643 return "%s, %s" % (text, more_text)
644 return more_text
Alan Viverette45837092017-05-12 14:50:53 -0400645
Alan Viverette95f6d362017-04-06 09:40:50 -0400646
Jeff Gaston13e38412018-02-06 14:45:36 -0500647class buildId(object):
648 def __init__(self, url_id, fs_id):
649 # id when used in build server urls
650 self.url_id = url_id
651 # id when used in build commands
652 self.fs_id = fs_id
653
Jeff Gastonb9b09052018-02-05 14:16:05 -0500654def getBuildId(args):
Jeff Gaston13e38412018-02-06 14:45:36 -0500655 # must be in the format 12345 or P12345
Jeff Gastonb9b09052018-02-05 14:16:05 -0500656 source = args.source
Jeff Gaston13e38412018-02-06 14:45:36 -0500657 number_text = source[:]
658 presubmit = False
659 if number_text.startswith("P"):
660 presubmit = True
661 number_text = number_text[1:]
662 if not number_text.isnumeric():
Alan Viverette7e897e22018-03-09 15:24:10 -0500663 return None
Jeff Gaston13e38412018-02-06 14:45:36 -0500664 url_id = source
665 fs_id = url_id
666 if presubmit:
667 fs_id = "0"
668 args.file = False
669 return buildId(url_id, fs_id)
Jeff Gastonb9b09052018-02-05 14:16:05 -0500670
Colin Cross573e1212018-02-12 14:59:52 -0800671def getFile(args):
672 source = args.source
673 if not source.isnumeric():
674 return args.source
Alan Viverette7e897e22018-03-09 15:24:10 -0500675 return None
Colin Cross573e1212018-02-12 14:59:52 -0800676
Alan Viverette4ec9a172018-02-20 16:19:31 -0500677
678def script_relative(rel_path):
Jeff Gastona68a8d42018-03-23 14:00:13 -0400679 return os.path.join(script_dir, rel_path)
Alan Viverette4ec9a172018-02-20 16:19:31 -0500680
681
Jeff Gastoncc296a82018-03-23 14:33:24 -0400682def uncommittedChangesExist():
683 try:
684 # Make sure we don't overwrite any pending changes.
685 diffCommand = "cd " + git_dir + " && git diff --quiet"
686 subprocess.check_call(diffCommand, shell=True)
687 subprocess.check_call(diffCommand + " --cached", shell=True)
688 return False
689 except subprocess.CalledProcessError:
690 return True
691
692
Jeff Gastona7fba9b2018-03-26 23:48:51 -0400693rm(temp_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400694parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400695 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400696parser.add_argument(
Alan Viverette129555e2018-01-30 09:57:57 -0500697 'source',
698 help='Build server build ID or local Maven ZIP file')
699parser.add_argument(
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400700 '-m', '--material', action="store_true",
Alan Viverette6dc45752020-04-16 14:56:20 +0000701 help='If specified, updates only Material Design Components')
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400702parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400703 '-c', '--constraint', action="store_true",
704 help='If specified, updates only Constraint Layout')
705parser.add_argument(
Jeff Gaston782c3e32018-02-06 14:36:17 -0500706 '-j', '--jetifier', action="store_true",
707 help='If specified, updates only Jetifier')
708parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400709 '-p', '--platform', action="store_true",
710 help='If specified, updates only the Android Platform')
Alan Viverettec1c32b62017-12-20 09:40:36 -0500711parser.add_argument(
Anton Hansson9709fd42018-04-03 16:52:13 +0100712 '-f', '--finalize_sdk', type=int,
713 help='If specified, imports the source build as the specified finalized SDK version')
714parser.add_argument(
Alan Viverettec1c32b62017-12-20 09:40:36 -0500715 '-b', '--buildtools', action="store_true",
716 help='If specified, updates only the Build Tools')
Jeff Gastoncc296a82018-03-23 14:33:24 -0400717parser.add_argument(
Alan Viverette6dc45752020-04-16 14:56:20 +0000718 '-x', '--androidx', action="store_true",
719 help='If specified, updates only the Jetpack (androidx) libraries excluding those covered by other arguments')
Jeff Gaston41fd5e62018-04-25 17:02:02 -0400720parser.add_argument(
Jeff Gastoncc296a82018-03-23 14:33:24 -0400721 '--commit-first', action="store_true",
722 help='If specified, then if uncommited changes exist, commit before continuing')
Jeff Sharkeyabe16df2021-05-25 10:09:03 -0600723parser.add_argument(
724 '--beyond-corp', action="store_true",
725 help='If specified, then fetch artifacts with tooling that works on BeyondCorp devices')
Alan Viveretted4527e62017-05-11 15:03:25 -0400726args = parser.parse_args()
Jeff Gaston56fe2762018-02-06 14:54:37 -0500727args.file = True
Alan Viverette129555e2018-01-30 09:57:57 -0500728if not args.source:
729 parser.error("You must specify a build ID or local Maven ZIP file")
Alan Viveretted4527e62017-05-11 15:03:25 -0400730 sys.exit(1)
Alan Viverette6dc45752020-04-16 14:56:20 +0000731if not (args.platform or args.buildtools \
732 or args.jetifier or args.androidx or args.material \
733 or args.finalize_sdk or args.constraint):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500734 parser.error("You must specify at least one target to update")
Alan Viverettecd3de262017-08-14 09:51:30 -0400735 sys.exit(1)
Alan Viverette6dc45752020-04-16 14:56:20 +0000736if (args.constraint or args.material or args.androidx) \
Colin Cross74683bc2018-04-11 17:36:18 -0700737 and which('pom2bp') is None:
Louis Pullen-Freilich61bb70e2019-04-09 16:15:32 +0100738 parser.error("Cannot find pom2bp in path; please run lunch to set up build environment. You may also need to run 'm pom2bp' if it hasn't been built already.")
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500739 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400740
Jeff Gastoncc296a82018-03-23 14:33:24 -0400741if uncommittedChangesExist():
742 if args.commit_first:
743 subprocess.check_call("cd " + git_dir + " && git add -u", shell=True)
744 subprocess.check_call("cd " + git_dir + " && git commit -m 'save working state'", shell=True)
745
746if uncommittedChangesExist():
Anton Hansson10861a02018-04-06 13:57:08 +0100747 print_e('FAIL: There are uncommitted changes here. Please commit or stash before continuing, because %s will run "git reset --hard" if execution fails' % os.path.basename(__file__))
Alan Viveretted4527e62017-05-11 15:03:25 -0400748 sys.exit(1)
749
750try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400751 components = None
752 if args.constraint:
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400753 if update_constraint_x(getFile(args)):
754 components = append(components, 'Constraint Layout X')
755 else:
756 print_e('Failed to update Constraint Layout X, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400757 sys.exit(1)
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400758 if args.androidx:
Jeff Gaston17ccf8f2018-08-29 11:33:19 -0400759 if update_androidx('androidx', \
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400760 getBuildId(args), getFile(args)):
761 components = append(components, 'AndroidX')
762 else:
763 print_e('Failed to update AndroidX, aborting...')
764 sys.exit(1)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500765 if args.jetifier:
Jeff Gaston17ccf8f2018-08-29 11:33:19 -0400766 if update_jetifier('androidx', getBuildId(args)):
Jeff Gaston782c3e32018-02-06 14:36:17 -0500767 components = append(components, 'Jetifier')
768 else:
769 print_e('Failed to update Jetifier, aborting...')
770 sys.exit(1)
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100771 if args.platform or args.finalize_sdk:
Anton Hansson87654c42018-04-26 15:19:42 +0100772 if update_framework_current(getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400773 components = append(components, 'platform SDK')
774 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500775 print_e('Failed to update platform SDK, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400776 sys.exit(1)
Anton Hansson9709fd42018-04-03 16:52:13 +0100777 if args.finalize_sdk:
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100778 n = args.finalize_sdk
779 if finalize_sdk(getBuildId(args), n):
780 # We commit the finalized dir separately from the current sdk update.
781 msg = "Import final sdk version %d from build %s" % (n, getBuildId(args).url_id)
782 subprocess.check_call(['git', 'add', '%d' % n])
Sundong Ahncc34cc32018-07-11 15:18:47 +0900783 subprocess.check_call(['git', 'add', 'Android.bp'])
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100784 subprocess.check_call(['git', 'commit', '-m', msg])
Anton Hansson9709fd42018-04-03 16:52:13 +0100785 else:
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100786 print_e('Failed to finalize SDK %d, aborting...' % n)
Anton Hansson9709fd42018-04-03 16:52:13 +0100787 sys.exit(1)
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400788 if args.material:
789 if update_material(getFile(args)):
790 components = append(components, 'intermediate-AndroidX Design Library')
791 else:
792 print_e('Failed to update intermediate-AndroidX Design Library, aborting...')
Alan Viverette129555e2018-01-30 09:57:57 -0500793 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500794 if args.buildtools:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500795 if update_buildtools('sdk_phone_armv7-sdk_mac', 'darwin', getBuildId(args)) \
796 and update_buildtools('sdk_phone_x86_64-sdk', 'linux', getBuildId(args)) \
797 and update_buildtools('sdk_phone_armv7-win_sdk', 'windows', getBuildId(args)):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500798 components = append(components, 'build tools')
799 else:
800 print_e('Failed to update build tools, aborting...')
801 sys.exit(1)
Jeff Gastonb70daa92018-03-29 20:10:22 -0400802
Jeff Gastonf12f3ce2018-03-23 16:41:24 -0400803
Alan Viveretted4527e62017-05-11 15:03:25 -0400804
Colin Cross941d9802018-05-03 21:37:57 -0700805 subprocess.check_call(['git', 'add', current_path, buildtools_dir])
Colin Cross573e1212018-02-12 14:59:52 -0800806 if not args.source.isnumeric():
Alan Viverette129555e2018-01-30 09:57:57 -0500807 src_msg = "local Maven ZIP"
808 else:
Jeff Gaston13e38412018-02-06 14:45:36 -0500809 src_msg = "build %s" % (getBuildId(args).url_id)
Alan Viverette129555e2018-01-30 09:57:57 -0500810 msg = "Import %s from %s\n\n%s" % (components, src_msg, flatten(sys.argv))
Alan Viveretted4527e62017-05-11 15:03:25 -0400811 subprocess.check_call(['git', 'commit', '-m', msg])
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100812 if args.finalize_sdk:
813 print('NOTE: Created two commits:')
814 subprocess.check_call(['git', 'log', '-2', '--oneline'])
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500815 print('Remember to test this change before uploading it to Gerrit!')
Alan Viveretted4527e62017-05-11 15:03:25 -0400816
817finally:
818 # Revert all stray files, including the downloaded zip.
819 try:
820 with open(os.devnull, 'w') as bitbucket:
821 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
822 subprocess.check_call(
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400823 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!', '--allow-empty'], stdout=bitbucket)
Alan Viveretted4527e62017-05-11 15:03:25 -0400824 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
825 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500826 print_e('ERROR: Failed cleaning up, manual cleanup required!!!')