Home | History | Annotate | Download | only in sdk
      1 #!/usr/bin/python3
      2 
      3 # 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.
      6 import os, sys, getopt, zipfile, re
      7 import argparse
      8 import subprocess
      9 from shutil import copyfile, rmtree, which
     10 from distutils.version import LooseVersion
     11 from functools import reduce
     12 
     13 current_path = 'current'
     14 system_path = 'system_current'
     15 api_path = 'api'
     16 system_api_path = 'system-api'
     17 support_dir = os.path.join(current_path, 'support')
     18 androidx_dir = os.path.join(current_path, 'androidx')
     19 extras_dir = os.path.join(current_path, 'extras')
     20 buildtools_dir = 'tools'
     21 jetifier_dir = os.path.join(buildtools_dir, 'jetifier', 'jetifier-standalone')
     22 
     23 temp_dir = os.path.join(os.getcwd(), "support_tmp")
     24 os.chdir(os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))))
     25 git_dir = os.getcwd()
     26 
     27 # See go/fetch_artifact for details on this script.
     28 FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
     29 
     30 maven_to_make = {
     31     # Support Library
     32     'com.android.support:animated-vector-drawable': ['android-support-animatedvectordrawable', 'graphics/drawable'],
     33     'com.android.support:appcompat-v7': ['android-support-v7-appcompat', 'v7/appcompat'],
     34     'com.android.support:cardview-v7': ['android-support-v7-cardview', 'v7/cardview'],
     35     'com.android.support:collections': ['android-support-collections', 'collections', 'jar'],
     36     'com.android.support:customtabs': ['android-support-customtabs', 'customtabs'],
     37     'com.android.support:exifinterface': ['android-support-exifinterface', 'exifinterface'],
     38     'com.android.support:gridlayout-v7': ['android-support-v7-gridlayout', 'v7/gridlayout'],
     39     'com.android.support:leanback-v17': ['android-support-v17-leanback', 'v17/leanback'],
     40     'com.android.support:mediarouter-v7': ['android-support-v7-mediarouter', 'v7/mediarouter'],
     41     'com.android.support:palette-v7': ['android-support-v7-palette', 'v7/palette'],
     42     'com.android.support:percent': ['android-support-percent', 'percent'],
     43     'com.android.support:preference-leanback-v17': ['android-support-v17-preference-leanback', 'v17/preference-leanback'],
     44     'com.android.support:preference-v14': ['android-support-v14-preference', 'v14/preference'],
     45     'com.android.support:preference-v7': ['android-support-v7-preference', 'v7/preference'],
     46     'com.android.support:recommendation': ['android-support-recommendation', 'recommendation'],
     47     'com.android.support:recyclerview-v7': ['android-support-v7-recyclerview', 'v7/recyclerview'],
     48     'com.android.support:support-annotations': ['android-support-annotations', 'annotations', 'jar'],
     49     'com.android.support:support-compat': ['android-support-compat', 'compat'],
     50     'com.android.support:support-core-ui': ['android-support-core-ui', 'core-ui'],
     51     'com.android.support:support-core-utils': ['android-support-core-utils', 'core-utils'],
     52     'com.android.support:support-dynamic-animation': ['android-support-dynamic-animation', 'dynamic-animation'],
     53     'com.android.support:support-emoji-appcompat': ['android-support-emoji-appcompat', 'emoji-appcompat'],
     54     'com.android.support:support-emoji-bundled': ['android-support-emoji-bundled', 'emoji-bundled'],
     55     'com.android.support:support-emoji': ['android-support-emoji', 'emoji'],
     56     'com.android.support:support-fragment': ['android-support-fragment', 'fragment'],
     57     'com.android.support:support-media-compat': ['android-support-media-compat', 'media-compat'],
     58     'com.android.support:support-tv-provider': ['android-support-tv-provider', 'tv-provider'],
     59     'com.android.support:support-v13': ['android-support-v13', 'v13'],
     60     'com.android.support:support-v4': ['android-support-v4', 'v4'],
     61     'com.android.support:support-vector-drawable': ['android-support-vectordrawable', 'graphics/drawable'],
     62     'com.android.support:transition': ['android-support-transition', 'transition'],
     63     'com.android.support:wear': ['android-support-wear', 'wear'],
     64 
     65     # Support Library (28.0.0 splits + new modules)
     66     'com.android.support:heifwriter': ['android-support-heifwriter', 'heifwriter'],
     67     'com.android.support:webkit': ['android-support-webkit', 'webkit'],
     68     'com.android.support:customview': ['android-support-customview', 'customview'],
     69     'com.android.support:textclassifier': ['android-support-textclassifier', 'textclassifier'],
     70     'com.android.support:swiperefreshlayout': ['android-support-swiperefreshlayout', 'swiperefreshlayout'],
     71     'com.android.support:viewpager': ['android-support-viewpager', 'viewpager'],
     72     'com.android.support:coordinatorlayout': ['android-support-coordinatorlayout', 'coordinatorlayout'],
     73     'com.android.support:asynclayoutinflater': ['android-support-asynclayoutinflater', 'asynclayoutinflater'],
     74     'com.android.support:support-content': ['android-support-support-content', 'support-content'],
     75     'com.android.support:documentfile': ['android-support-documentfile', 'documentfile'],
     76     'com.android.support:drawerlayout': ['android-support-drawerlayout', 'drawerlayout'],
     77     'com.android.support:localbroadcastmanager': ['android-support-localbroadcastmanager', 'localbroadcastmanager'],
     78     'com.android.support:print': ['android-support-print', 'print'],
     79     'com.android.support:slidingpanelayout': ['android-support-slidingpanelayout', 'slidingpanelayout'],
     80     'com.android.support:interpolator': ['android-support-interpolator', 'interpolator'],
     81     'com.android.support:cursoradapter': ['android-support-cursoradapter', 'cursoradapter'],
     82     'com.android.support:loader': ['android-support-loader', 'loader'],
     83     'com.android.support:contentpaging': ['android-support-contentpaging', 'contentpaging'],
     84     'com.android.support:recyclerview-selection': ['android-support-recyclerview-selection', 'recyclerview-selection'],
     85     'com.android.support:car': ['android-support-car', 'car'],
     86     'com.android.support:slices-core': ['android-slices-core', 'slices-core'],
     87     'com.android.support:slices-view': ['android-slices-view', 'slices-view'],
     88     'com.android.support:slices-builders': ['android-slices-builders', 'slices-builders'],
     89     'com.android.support:versionedparcelable': ['android-versionedparcelable', 'versionedparcelable'],
     90 
     91     # Multidex
     92     'com.android.support:multidex': ['android-support-multidex', 'multidex/library'],
     93     'com.android.support:multidex-instrumentation': ['android-support-multidex-instrumentation', 'multidex/instrumentation'],
     94 
     95     # Constraint Layout
     96     'com.android.support.constraint:constraint-layout': ['android-support-constraint-layout', 'constraint-layout'],
     97     'com.android.support.constraint:constraint-layout-solver': ['android-support-constraint-layout-solver', 'constraint-layout-solver'],
     98 
     99     # Architecture Components
    100     'android.arch.core:runtime': ['android-arch-core-runtime', 'arch-core/runtime'],
    101     'android.arch.core:common': ['android-arch-core-common', 'arch-core/common'],
    102     'android.arch.lifecycle:common': ['android-arch-lifecycle-common', 'arch-lifecycle/common'],
    103     'android.arch.lifecycle:common-java8': ['android-arch-lifecycle-common-java8', 'arch-lifecycle/common-java8'],
    104     'android.arch.lifecycle:extensions': ['android-arch-lifecycle-extensions', 'arch-lifecycle/extensions'],
    105     'android.arch.lifecycle:livedata': ['android-arch-lifecycle-livedata', 'arch-lifecycle/livedata'],
    106     'android.arch.lifecycle:livedata-core': ['android-arch-lifecycle-livedata-core', 'arch-lifecycle/livedata-core'],
    107     'android.arch.lifecycle:process': ['android-arch-lifecycle-process', 'arch-lifecycle/process'],
    108     'android.arch.lifecycle:runtime': ['android-arch-lifecycle-runtime', 'arch-lifecycle/runtime'],
    109     'android.arch.lifecycle:service': ['android-arch-lifecycle-service', 'arch-lifecycle/service'],
    110     'android.arch.lifecycle:viewmodel': ['android-arch-lifecycle-viewmodel', 'arch-lifecycle/viewmodel'],
    111     'android.arch.paging:common': ['android-arch-paging-common', 'arch-paging/common'],
    112     'android.arch.paging:runtime': ['android-arch-paging-runtime', 'arch-paging/runtime'],
    113     'android.arch.persistence:db': ['android-arch-persistence-db', 'arch-persistence/db'],
    114     'android.arch.persistence:db-framework': ['android-arch-persistence-db-framework', 'arch-persistence/db-framework'],
    115     'android.arch.persistence.room:common': ['android-arch-room-common', 'arch-room/common'],
    116     'android.arch.persistence.room:migration': ['android-arch-room-migration', 'arch-room/migration'],
    117     'android.arch.persistence.room:runtime': ['android-arch-room-runtime', 'arch-room/runtime'],
    118     'android.arch.persistence.room:testing': ['android-arch-room-testing', 'arch-room/testing'],
    119 
    120     # AndroidX
    121     'androidx.slice:slice-builders': ['androidx.slice_slice-builders', 'androidx/slice/slice-builders'],
    122     'androidx.slice:slice-core': ['androidx.slice_slice-core', 'androidx/slice/slice-core'],
    123     'androidx.slice:slice-view': ['androidx.slice_slice-view', 'androidx/slice/slice-view'],
    124     'androidx.versionedparcelable:versionedparcelable': ['androidx.versionedparcelable_versionedparcelable', 'androidx/versionedparcelable'],
    125     'androidx.vectordrawable:vectordrawable-animated': ['androidx.vectordrawable_vectordrawable-animated', 'androidx/vectordrawable/vectordrawable-animated'],
    126     'androidx.annotation:annotation': ['androidx.annotation_annotation', 'androidx/annotation/annotation', 'jar'],
    127     'androidx.asynclayoutinflater:asynclayoutinflater': ['androidx.asynclayoutinflater_asynclayoutinflater', 'androidx/asynclayoutinflater/asynclayoutinflater'],
    128     'androidx.car:car': ['androidx.car_car', 'androidx/car/car'],
    129     'androidx.collection:collection': ['androidx.collection_collection', 'androidx/collection/collection', 'jar'],
    130     'androidx.core:core': ['androidx.core_core', 'androidx/core/core'],
    131     'androidx.contentpaging:contentpaging': ['androidx.contentpaging_contentpaging', 'androidx/contentpaging/contentpaging'],
    132     'androidx.coordinatorlayout:coordinatorlayout': ['androidx.coordinatorlayout_coordinatorlayout', 'androidx/coordinatorlayout/coordinatorlayout'],
    133     'androidx.legacy:legacy-support-core-ui': ['androidx.legacy_legacy-support-core-ui', 'androidx/legacy/legacy-support-core-ui'],
    134     'androidx.legacy:legacy-support-core-utils': ['androidx.legacy_legacy-support-core-utils', 'androidx/legacy/legacy-support-core-utils'],
    135     'androidx.cursoradapter:cursoradapter': ['androidx.cursoradapter_cursoradapter', 'androidx/cursoradapter/cursoradapter'],
    136     'androidx.browser:browser': ['androidx.browser_browser', 'androidx/browser/browser'],
    137     'androidx.customview:customview': ['androidx.customview_customview', 'androidx/customview/customview'],
    138     'androidx.documentfile:documentfile': ['androidx.documentfile_documentfile', 'androidx/documentfile/documentfile'],
    139     'androidx.drawerlayout:drawerlayout': ['androidx.drawerlayout_drawerlayout', 'androidx/drawerlayout/drawerlayout'],
    140     'androidx.dynamicanimation:dynamicanimation': ['androidx.dynamicanimation_dynamicanimation', 'androidx/dynamicanimation/dynamicanimation'],
    141     'androidx.emoji:emoji': ['androidx.emoji_emoji', 'androidx/emoji/emoji'],
    142     'androidx.emoji:emoji-appcompat': ['androidx.emoji_emoji-appcompat', 'androidx/emoji/emoji-appcompat'],
    143     'androidx.emoji:emoji-bundled': ['androidx.emoji_emoji-bundled', 'androidx/emoji/emoji-bundled'],
    144     'androidx.exifinterface:exifinterface': ['androidx.exifinterface_exifinterface', 'androidx/exifinterface/exifinterface'],
    145     'androidx.fragment:fragment': ['androidx.fragment_fragment', 'androidx/fragment/fragment'],
    146     'androidx.heifwriter:heifwriter': ['androidx.heifwriter_heifwriter', 'androidx/heifwriter/heifwriter'],
    147     'androidx.interpolator:interpolator': ['androidx.interpolator_interpolator', 'androidx/interpolator/interpolator'],
    148     'androidx.loader:loader': ['androidx.loader_loader', 'androidx/loader/loader'],
    149     'androidx.localbroadcastmanager:localbroadcastmanager': ['androidx.localbroadcastmanager_localbroadcastmanager', 'androidx/localbroadcastmanager/localbroadcastmanager'],
    150     'androidx.media:media': ['androidx.media_media', 'androidx/media/media'],
    151     'androidx.percentlayout:percentlayout': ['androidx.percentlayout_percentlayout', 'androidx/percentlayout/percentlayout'],
    152     'androidx.print:print': ['androidx.print_print', 'androidx/print/print'],
    153     'androidx.recommendation:recommendation': ['androidx.recommendation_recommendation', 'androidx/recommendation/recommendation'],
    154     'androidx.recyclerview:recyclerview-selection': ['androidx.recyclerview_recyclerview-selection', 'androidx/recyclerview/recyclerview-selection'],
    155     'androidx.slidingpanelayout:slidingpanelayout': ['androidx.slidingpanelayout_slidingpanelayout', 'androidx/slidingpanelayout/slidingpanelayout'],
    156     'androidx.swiperefreshlayout:swiperefreshlayout': ['androidx.swiperefreshlayout_swiperefreshlayout', 'androidx/swiperefreshlayout/swiperefreshlayout'],
    157     'androidx.textclassifier:textclassifier': ['androidx.textclassifier_textclassifier', 'androidx/textclassifier/textclassifier'],
    158     'androidx.transition:transition': ['androidx.transition_transition', 'androidx/transition/transition'],
    159     'androidx.tvprovider:tvprovider': ['androidx.tvprovider_tvprovider', 'androidx/tvprovider/tvprovider'],
    160     'androidx.legacy:legacy-support-v13': ['androidx.legacy_legacy-support-v13', 'androidx/legacy/legacy-support-v13'],
    161     'androidx.legacy:legacy-preference-v14': ['androidx.legacy_legacy-preference-v14', 'androidx/legacy/legacy-preference-v14'],
    162     'androidx.leanback:leanback': ['androidx.leanback_leanback', 'androidx/leanback/leanback'],
    163     'androidx.leanback:leanback-preference': ['androidx.leanback_leanback-preference', 'androidx/leanback/leanback-preference'],
    164     'androidx.legacy:legacy-support-v4': ['androidx.legacy_legacy-support-v4', 'androidx/legacy/legacy-support-v4'],
    165     'androidx.appcompat:appcompat': ['androidx.appcompat_appcompat', 'androidx/appcompat/appcompat'],
    166     'androidx.cardview:cardview': ['androidx.cardview_cardview', 'androidx/cardview/cardview'],
    167     'androidx.gridlayout:gridlayout': ['androidx.gridlayout_gridlayout', 'androidx/gridlayout/gridlayout'],
    168     'androidx.mediarouter:mediarouter': ['androidx.mediarouter_mediarouter', 'androidx/mediarouter/mediarouter'],
    169     'androidx.palette:palette': ['androidx.palette_palette', 'androidx/palette/palette'],
    170     'androidx.preference:preference': ['androidx.preference_preference', 'androidx/preference/preference'],
    171     'androidx.recyclerview:recyclerview': ['androidx.recyclerview_recyclerview', 'androidx/recyclerview/recyclerview'],
    172     'androidx.vectordrawable:vectordrawable': ['androidx.vectordrawable_vectordrawable', 'androidx/vectordrawable/vectordrawable'],
    173     'androidx.viewpager:viewpager': ['androidx.viewpager_viewpager', 'androidx/viewpager/viewpager'],
    174     'androidx.wear:wear': ['androidx.wear_wear', 'androidx/wear/wear'],
    175     'androidx.webkit:webkit': ['androidx.webkit_webkit', 'androidx/webkit/webkit'],
    176 
    177     # AndroidX for Multidex
    178     'androidx.multidex:multidex': ['androidx-multidex_multidex', 'androidx/multidex/multidex'],
    179     'androidx.multidex:multidex-instrumentation': ['androidx-multidex_multidex-instrumentation', 'androidx/multidex/multidex-instrumentation'],
    180 
    181     # AndroidX for Constraint Layout
    182     'androidx.constraintlayout:constraintlayout': ['androidx-constraintlayout_constraintlayout', 'androidx/constraintlayout/constraintlayout'],
    183     'androidx.constraintlayout:constraintlayout-solver': ['androidx-constraintlayout_constraintlayout-solver', 'androidx/constraintlayout/constraintlayout-solver'],
    184 
    185     # AndroidX for Architecture Components
    186     'androidx.arch.core:core-common': ['androidx.arch.core_core-common', 'androidx/arch/core/core-common'],
    187     'androidx.arch.core:core-runtime': ['androidx.arch.core_core-runtime', 'androidx/arch/core/core-runtime'],
    188     'androidx.lifecycle:lifecycle-common': ['androidx.lifecycle_lifecycle-common', 'androidx/lifecycle/lifecycle-common'],
    189     'androidx.lifecycle:lifecycle-common-java8': ['androidx.lifecycle_lifecycle-common-java8', 'androidx/lifecycle/lifecycle-common-java8'],
    190     'androidx.lifecycle:lifecycle-extensions': ['androidx.lifecycle_lifecycle-extensions', 'androidx/lifecycle/lifecycle-extensions'],
    191     'androidx.lifecycle:lifecycle-livedata': ['androidx.lifecycle_lifecycle-livedata', 'androidx/lifecycle/lifecycle-livedata'],
    192     'androidx.lifecycle:lifecycle-livedata-core': ['androidx.lifecycle_lifecycle-livedata-core', 'androidx/lifecycle/lifecycle-livedata-core'],
    193     'androidx.lifecycle:lifecycle-process': ['androidx.lifecycle_lifecycle-process', 'androidx/lifecycle/lifecycle-process'],
    194     'androidx.lifecycle:lifecycle-runtime': ['androidx.lifecycle_lifecycle-runtime', 'androidx/lifecycle/lifecycle-runtime'],
    195     'androidx.lifecycle:lifecycle-service': ['androidx.lifecycle_lifecycle-service', 'androidx/lifecycle/lifecycle-service'],
    196     'androidx.lifecycle:lifecycle-viewmodel': ['androidx.lifecycle_lifecycle-viewmodel', 'androidx/lifecycle/lifecycle-viewmodel'],
    197     'androidx.paging:paging-common': ['androidx.paging_paging-common', 'androidx/paging/paging-common'],
    198     'androidx.paging:paging-runtime': ['androidx.paging_paging-runtime', 'androidx/paging/paging-runtime'],
    199     'androidx.sqlite:sqlite': ['androidx.sqlite_sqlite', 'androidx/sqlite/sqlite'],
    200     'androidx.sqlite:sqlite-framework': ['androidx.sqlite_sqlite-framework', 'androidx/sqlite/sqlite-framework'],
    201     'androidx.room:room-common': ['androidx.room_room-common', 'androidx/room/room-common'],
    202     'androidx.room:room-migration': ['androidx.room_room-migration', 'androidx/room/room-migration'],
    203     'androidx.room:room-runtime': ['androidx.room_room-runtime', 'androidx/room/room-runtime'],
    204     'androidx.room:room-testing': ['androidx.room_room-testing', 'androidx/room/room-testing'],
    205 
    206     # Lifecycle
    207     # Missing dependencies:
    208     # - auto-common
    209     # - javapoet
    210     #'android.arch.lifecycle:compiler': ['android-arch-lifecycle-compiler', 'arch-lifecycle/compiler'],
    211     # Missing dependencies:
    212     # - reactive-streams
    213     #'android.arch.lifecycle:reactivestreams': ['android-arch-lifecycle-reactivestreams','arch-lifecycle/reactivestreams'],
    214 
    215     # Room
    216     # Missing dependencies:
    217     # - auto-common
    218     # - javapoet
    219     # - antlr4
    220     # - kotlin-metadata
    221     # - commons-codec
    222     #'android.arch.persistence.room:compiler': ['android-arch-room-compiler', 'arch-room/compiler'],
    223     # Missing dependencies:
    224     # - rxjava
    225     #'android.arch.persistence.room:rxjava2': ['android-arch-room-rxjava2', 'arch-room/rxjava2'],
    226 
    227     # Third-party dependencies
    228     'com.google.android:flexbox': ['flexbox', 'flexbox'],
    229 
    230     # Support Library Material Design Components
    231     'com.android.support:design': ['android-support-design', 'design'],
    232     'com.android.support:design-animation': ['android-support-design-animation', 'design-animation'],
    233     'com.android.support:design-bottomnavigation': ['android-support-design-bottomnavigation', 'design-bottomnavigation'],
    234     'com.android.support:design-bottomsheet': ['android-support-design-bottomsheet', 'design-bottomsheet'],
    235     'com.android.support:design-button': ['android-support-design-button', 'design-button'],
    236     'com.android.support:design-canvas': ['android-support-design-canvas', 'design-canvas'],
    237     'com.android.support:design-card': ['android-support-design-card', 'design-card'],
    238     'com.android.support:design-chip': ['android-support-design-chip', 'design-chip'],
    239     'com.android.support:design-circularreveal': ['android-support-design-circularreveal', 'design-circularreveal'],
    240     'com.android.support:design-circularreveal-cardview': ['android-support-design-circularreveal-cardview', 'design-circularreveal-cardview'],
    241     'com.android.support:design-circularreveal-coordinatorlayout': ['android-support-design-circularreveal-coordinatorlayout', 'design-circularreveal-coordinatorlayout'],
    242     'com.android.support:design-color': ['android-support-design-color', 'design-color'],
    243     'com.android.support:design-dialog': ['android-support-design-dialog', 'design-dialog'],
    244     'com.android.support:design-drawable': ['android-support-design-drawable', 'design-drawable'],
    245     'com.android.support:design-expandable': ['android-support-design-expandable', 'design-expandable'],
    246     'com.android.support:design-floatingactionbutton': ['android-support-design-floatingactionbutton', 'design-floatingactionbutton'],
    247     'com.android.support:design-internal': ['android-support-design-internal', 'design-internal'],
    248     'com.android.support:design-math': ['android-support-design-math', 'design-math'],
    249     'com.android.support:design-resources': ['android-support-design-resources', 'design-resources'],
    250     'com.android.support:design-ripple': ['android-support-design-ripple', 'design-ripple'],
    251     'com.android.support:design-snackbar': ['android-support-design-snackbar', 'design-snackbar'],
    252     'com.android.support:design-stateful': ['android-support-design-stateful', 'design-stateful'],
    253     'com.android.support:design-textfield': ['android-support-design-textfield', 'design-textfield'],
    254     'com.android.support:design-theme': ['android-support-design-theme', 'design-theme'],
    255     'com.android.support:design-transformation': ['android-support-design-transformation', 'design-transformation'],
    256     'com.android.support:design-typography': ['android-support-design-typography', 'design-typography'],
    257     'com.android.support:design-widget': ['android-support-design-widget', 'design-widget'],
    258     'com.android.support:design-navigation': ['android-support-design-navigation', 'design-navigation'],
    259     'com.android.support:design-tabs': ['android-support-design-tabs', 'design-tabs'],
    260     'com.android.support:design-bottomappbar': ['android-support-design-bottomappbar', 'design-bottomappbar'],
    261     'com.android.support:design-shape': ['android-support-design-shape', 'design-shape'],
    262 
    263     # Androidx Material Design Components
    264     'com.google.android.material:material': ['com.google.android.material_material', 'com/google/android/material/material'],
    265 
    266     # Intermediate-AndroidX Material Design Components
    267     'com.android.temp.support:design': ['androidx.design_design', 'com/android/temp/support/design/design'],
    268     'com.android.temp.support:design-animation': ['androidx.design_design-animation', 'com/android/temp/support/design/design-animation'],
    269     'com.android.temp.support:design-bottomnavigation': ['androidx.design_design-bottomnavigation', 'com/android/temp/support/design/design-bottomnavigation'],
    270     'com.android.temp.support:design-bottomsheet': ['androidx.design_design-bottomsheet', 'com/android/temp/support/design/design-bottomsheet'],
    271     'com.android.temp.support:design-button': ['androidx.design_design-button', 'com/android/temp/support/design/design-button'],
    272     'com.android.temp.support:design-canvas': ['androidx.design_design-canvas', 'com/android/temp/support/design/design-canvas'],
    273     'com.android.temp.support:design-card': ['androidx.design_design-card', 'com/android/temp/support/design/design-card'],
    274     'com.android.temp.support:design-chip': ['androidx.design_design-chip', 'com/android/temp/support/design/design-chip'],
    275     'com.android.temp.support:design-circularreveal': ['androidx.design_design-circularreveal', 'com/android/temp/support/design/design-circularreveal'],
    276     'com.android.temp.support:design-circularreveal-cardview': ['androidx.design_design-circularreveal-cardview', 'com/android/temp/support/design/design-circularreveal-cardview'],
    277     'com.android.temp.support:design-circularreveal-coordinatorlayout': ['androidx.design_design-circularreveal-coordinatorlayout', 'com/android/temp/support/design/design-circularreveal-coordinatorlayout'],
    278     'com.android.temp.support:design-color': ['androidx.design_design-color', 'com/android/temp/support/design/design-color'],
    279     'com.android.temp.support:design-dialog': ['androidx.design_design-dialog', 'com/android/temp/support/design/design-dialog'],
    280     'com.android.temp.support:design-drawable': ['androidx.design_design-drawable', 'com/android/temp/support/design/design-drawable'],
    281     'com.android.temp.support:design-expandable': ['androidx.design_design-expandable', 'com/android/temp/support/design/design-expandable'],
    282     'com.android.temp.support:design-floatingactionbutton': ['androidx.design_design-floatingactionbutton', 'com/android/temp/support/design/design-floatingactionbutton'],
    283     'com.android.temp.support:design-internal': ['androidx.design_design-internal', 'com/android/temp/support/design/design-internal'],
    284     'com.android.temp.support:design-math': ['androidx.design_design-math', 'com/android/temp/support/design/design-math'],
    285     'com.android.temp.support:design-resources': ['androidx.design_design-resources', 'com/android/temp/support/design/design-resources'],
    286     'com.android.temp.support:design-ripple': ['androidx.design_design-ripple', 'com/android/temp/support/design/design-ripple'],
    287     'com.android.temp.support:design-snackbar': ['androidx.design_design-snackbar', 'com/android/temp/support/design/design-snackbar'],
    288     'com.android.temp.support:design-stateful': ['androidx.design_design-stateful', 'com/android/temp/support/design/design-stateful'],
    289     'com.android.temp.support:design-textfield': ['androidx.design_design-textfield', 'com/android/temp/support/design/design-textfield'],
    290     'com.android.temp.support:design-theme': ['androidx.design_design-theme', 'com/android/temp/support/design/design-theme'],
    291     'com.android.temp.support:design-transformation': ['androidx.design_design-transformation', 'com/android/temp/support/design/design-transformation'],
    292     'com.android.temp.support:design-typography': ['androidx.design_design-typography', 'com/android/temp/support/design/design-typography'],
    293     'com.android.temp.support:design-widget': ['androidx.design_design-widget', 'com/android/temp/support/design/design-widget'],
    294     'com.android.temp.support:design-navigation': ['androidx.design_design-navigation', 'com/android/temp/support/design/design-navigation'],
    295     'com.android.temp.support:design-tabs': ['androidx.design_design-tabs', 'com/android/temp/support/design/design-tabs'],
    296     'com.android.temp.support:design-bottomappbar': ['androidx.design_design-bottomappbar', 'com/android/temp/support/design/design-bottomappbar'],
    297     'com.android.temp.support:design-shape': ['androidx.design_design-shape', 'com/android/temp/support/design/design-shape'],
    298 
    299 }
    300 
    301 # Always remove these files.
    302 blacklist_files = [
    303     'annotations.zip',
    304     'public.txt',
    305     'R.txt',
    306     'AndroidManifest.xml',
    307     os.path.join('libs','noto-emoji-compat-java.jar')
    308 ]
    309 
    310 artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
    311 
    312 
    313 class MavenLibraryInfo:
    314     def __init__(self, key, group_id, artifact_id, version, dir, repo_dir, file):
    315         self.key = key
    316         self.group_id = group_id
    317         self.artifact_id = artifact_id
    318         self.version = version
    319         self.dir = dir
    320         self.repo_dir = repo_dir
    321         self.file = file
    322 
    323 
    324 def print_e(*args, **kwargs):
    325     print(*args, file=sys.stderr, **kwargs)
    326 
    327 
    328 def touch(fname, times=None):
    329     with open(fname, 'a'):
    330         os.utime(fname, times)
    331 
    332 
    333 def path(*path_parts):
    334     return reduce((lambda x, y: os.path.join(x, y)), path_parts)
    335 
    336 
    337 def flatten(list):
    338     return reduce((lambda x, y: "%s %s" % (x, y)), list)
    339 
    340 
    341 def rm(path):
    342     if os.path.isdir(path):
    343         rmtree(path)
    344     elif os.path.exists(path):
    345         os.remove(path)
    346 
    347 
    348 def mv(src_path, dst_path):
    349     if os.path.exists(dst_path):
    350         rm(dst_path)
    351     if not os.path.exists(os.path.dirname(dst_path)):
    352         os.makedirs(os.path.dirname(dst_path))
    353     os.rename(src_path, dst_path)
    354 
    355 
    356 def detect_artifacts(maven_repo_dirs):
    357     maven_lib_info = {}
    358 
    359     # Find the latest revision for each artifact, remove others
    360     for repo_dir in maven_repo_dirs:
    361         for root, dirs, files in os.walk(repo_dir):
    362             for file in files:
    363                 if file[-4:] == ".pom":
    364                     # Read the POM (hack hack hack).
    365                     group_id = ''
    366                     artifact_id = ''
    367                     version = ''
    368                     file = os.path.join(root, file)
    369                     with open(file) as pom_file:
    370                         for line in pom_file:
    371                             if line[:11] == '  <groupId>':
    372                                 group_id = line[11:-11]
    373                             elif line[:14] == '  <artifactId>':
    374                                 artifact_id = line[14:-14]
    375                             elif line[:11] == '  <version>':
    376                                 version = line[11:-11]
    377                     if group_id == '' or artifact_id == '' or version == '':
    378                         print_e('Failed to find Maven artifact data in ' + file)
    379                         continue
    380 
    381                     # Locate the artifact.
    382                     artifact_file = file[:-4]
    383                     if os.path.exists(artifact_file + '.jar'):
    384                         artifact_file = artifact_file + '.jar'
    385                     elif os.path.exists(artifact_file + '.aar'):
    386                         artifact_file = artifact_file + '.aar'
    387                     else:
    388                         print_e('Failed to find artifact for ' + artifact_file)
    389                         continue
    390 
    391                     # Make relative to root.
    392                     artifact_file = artifact_file[len(root) + 1:]
    393 
    394                     # Find the mapping.
    395                     group_artifact = group_id + ':' + artifact_id
    396                     if group_artifact in maven_to_make:
    397                         key = group_artifact
    398                     elif artifact_id in maven_to_make:
    399                         key = artifact_id
    400                     else:
    401                         print_e('Failed to find artifact mapping for ' + group_artifact)
    402                         continue
    403 
    404                     # Store the latest version.
    405                     version = LooseVersion(version)
    406                     if key not in maven_lib_info \
    407                             or version > maven_lib_info[key].version:
    408                         maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
    409                                                                root, repo_dir, artifact_file)
    410 
    411     return maven_lib_info
    412 
    413 
    414 def transform_maven_repos(maven_repo_dirs, transformed_dir, extract_res=True, include_static_deps=True):
    415     cwd = os.getcwd()
    416 
    417     # Use a temporary working directory.
    418     maven_lib_info = detect_artifacts(maven_repo_dirs)
    419     working_dir = temp_dir
    420 
    421     if not maven_lib_info:
    422         print_e('Failed to detect artifacts')
    423         return False
    424 
    425     # extract some files (for example, AndroidManifest.xml) from any relevant artifacts
    426     for info in maven_lib_info.values():
    427         transform_maven_lib(working_dir, info, extract_res)
    428 
    429     # generate a single Android.bp that specifies to use all of the above artifacts
    430     makefile = os.path.join(working_dir, 'Android.bp')
    431     with open(makefile, 'w') as f:
    432         args = ["pom2bp", "-sdk-version", "current"]
    433         if include_static_deps:
    434             args.append("-static-deps")
    435         rewriteNames = sorted([name for name in maven_to_make if ":" in name] + [name for name in maven_to_make if ":" not in name])
    436         args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name][0] for name in rewriteNames])
    437         args.extend(["-extra-deps=android-support-car=prebuilt-android.car-stubs"])
    438         # these depend on GSON which is not in AOSP
    439         args.extend(["-exclude=androidx.room_room-migration",
    440                      "-exclude=androidx.room_room-testing",
    441                      "-exclude=android-arch-room-migration",
    442                      "-exclude=android-arch-room-testing"])
    443         args.extend(["."])
    444         subprocess.check_call(args, stdout=f, cwd=working_dir)
    445 
    446     # Replace the old directory.
    447     output_dir = os.path.join(cwd, transformed_dir)
    448     mv(working_dir, output_dir)
    449     return True
    450 
    451 # moves <artifact_info> (of type MavenLibraryInfo) into the appropriate part of <working_dir> , and possibly unpacks any necessary included files
    452 def transform_maven_lib(working_dir, artifact_info, extract_res):
    453     # Move library into working dir
    454     new_dir = os.path.normpath(os.path.join(working_dir, os.path.relpath(artifact_info.dir, artifact_info.repo_dir)))
    455     mv(artifact_info.dir, new_dir)
    456 
    457     for dirpath, dirs, files in os.walk(new_dir):
    458         for f in files:
    459             if '-sources.jar' in f:
    460                 os.remove(os.path.join(dirpath, f))
    461 
    462     matcher = artifact_pattern.match(artifact_info.file)
    463     maven_lib_name = artifact_info.key
    464     maven_lib_vers = matcher.group(2)
    465     maven_lib_type = artifact_info.file[-3:]
    466 
    467     make_lib_name = maven_to_make[artifact_info.key][0]
    468     make_dir_name = maven_to_make[artifact_info.key][1]
    469 
    470     artifact_file = os.path.join(new_dir, artifact_info.file)
    471 
    472     if maven_lib_type == "aar":
    473         if extract_res:
    474             target_dir = os.path.join(working_dir, make_dir_name)
    475             if not os.path.exists(target_dir):
    476                 os.makedirs(target_dir)
    477 
    478             process_aar(artifact_file, target_dir)
    479 
    480         with zipfile.ZipFile(artifact_file) as zip:
    481             manifests_dir = os.path.join(working_dir, "manifests")
    482             zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
    483 
    484     print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
    485 
    486 
    487 def process_aar(artifact_file, target_dir):
    488     # Extract AAR file to target_dir.
    489     with zipfile.ZipFile(artifact_file) as zip:
    490         zip.extractall(target_dir)
    491 
    492     # Remove classes.jar
    493     classes_jar = os.path.join(target_dir, "classes.jar")
    494     if os.path.exists(classes_jar):
    495         os.remove(classes_jar)
    496 
    497     # Remove or preserve empty dirs.
    498     for root, dirs, files in os.walk(target_dir):
    499         for dir in dirs:
    500             dir_path = os.path.join(root, dir)
    501             if not os.listdir(dir_path):
    502                 os.rmdir(dir_path)
    503 
    504     # Remove top-level cruft.
    505     for file in blacklist_files:
    506         file_path = os.path.join(target_dir, file)
    507         if os.path.exists(file_path):
    508             os.remove(file_path)
    509 
    510 
    511 def fetch_artifact(target, build_id, artifact_path):
    512     print('Fetching %s from %s...' % (artifact_path, target))
    513     fetch_cmd = [FETCH_ARTIFACT, '--bid', str(build_id), '--target', target, artifact_path]
    514     try:
    515         subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
    516     except subprocess.CalledProcessError:
    517         print_e('FAIL: Unable to retrieve %s artifact for build ID %s' % (artifact_path, build_id))
    518         print_e('Please make sure you are authenticated for build server access!')
    519         return None
    520     return artifact_path
    521 
    522 
    523 def extract_artifact(artifact_path):
    524     # Unzip the repo archive into a separate directory.
    525     repo_dir = os.path.basename(artifact_path)[:-4]
    526     with zipfile.ZipFile(artifact_path) as zipFile:
    527         zipFile.extractall(repo_dir)
    528     return repo_dir
    529 
    530 
    531 def fetch_and_extract(target, build_id, file, artifact_path=None):
    532     if not artifact_path:
    533         artifact_path = fetch_artifact(target, build_id, file)
    534     if not artifact_path:
    535         return None
    536     return extract_artifact(artifact_path)
    537 
    538 
    539 def update_support(target, build_id, local_file):
    540     if build_id:
    541         repo_file = 'top-of-tree-m2repository-dejetified-%s.zip' % build_id.fs_id
    542         repo_dir = fetch_and_extract(target, build_id.url_id, repo_file, None)
    543     else:
    544         repo_dir = fetch_and_extract(target, None, None, local_file)
    545     if not repo_dir:
    546         print_e('Failed to extract Support Library repository')
    547         return False
    548 
    549     # Transform the repo archive into a Makefile-compatible format.
    550     return transform_maven_repos([repo_dir], support_dir, extract_res=True)
    551 
    552 
    553 def update_androidx(target, build_id, local_file):
    554     if build_id:
    555         repo_file = 'top-of-tree-m2repository-%s.zip' % build_id.fs_id
    556         repo_dir = fetch_and_extract(target, build_id.url_id, repo_file, None)
    557     else:
    558         repo_dir = fetch_and_extract(target, None, None, local_file)
    559     if not repo_dir:
    560         print_e('Failed to extract AndroidX repository')
    561         return False
    562 
    563     # Transform the repo archive into a Makefile-compatible format.
    564     return transform_maven_repos([repo_dir], androidx_dir, extract_res=False)
    565 
    566 
    567 def update_jetifier(target, build_id):
    568     repo_file = 'jetifier-standalone.zip'
    569     repo_dir = fetch_and_extract(target, build_id.url_id, repo_file)
    570     if not repo_dir:
    571         print_e('Failed to extract Jetifier')
    572         return False
    573 
    574     rm(jetifier_dir)
    575     mv(os.path.join(repo_dir, 'jetifier-standalone'), jetifier_dir)
    576     os.chmod(os.path.join(jetifier_dir, 'bin', 'jetifier-standalone'), 0o755)
    577     return True
    578 
    579 
    580 def update_constraint(target, build_id):
    581     layout_dir = fetch_and_extract(target, build_id.url_id,
    582                                    'com.android.support.constraint-constraint-layout-%s.zip' % build_id.fs_id)
    583     solver_dir = fetch_and_extract(target, build_id.url_id,
    584                                    'com.android.support.constraint-constraint-layout-solver-%s.zip' % build_id.fs_id)
    585     if not layout_dir or not solver_dir:
    586         print_e('Failed to extract Constraint Layout repositories')
    587         return False
    588 
    589     # Passing False here is an inelegant solution, but it means we can replace
    590     # the top-level directory without worrying about other child directories.
    591     return transform_maven_repos([layout_dir, solver_dir],
    592                                 os.path.join(extras_dir, 'constraint-layout'), extract_res=False)
    593 
    594 def update_constraint_x(local_file):
    595     repo_dir = extract_artifact(local_file)
    596     if not repo_dir:
    597         print_e('Failed to extract Constraint Layout X')
    598         return False
    599     return transform_maven_repos([repo_dir], os.path.join(extras_dir, 'constraint-layout-x'), extract_res=False)
    600 
    601 
    602 def update_design(file):
    603     design_dir = extract_artifact(file)
    604     if not design_dir:
    605         print_e('Failed to extract Design Library repositories')
    606         return False
    607 
    608     # Don't bother extracting resources -- this should only be used with AAPT2.
    609     return transform_maven_repos([design_dir],
    610                                 os.path.join(extras_dir, 'material-design'), extract_res=False)
    611 
    612 
    613 def update_material(file):
    614     design_dir = extract_artifact(file)
    615     if not design_dir:
    616         print_e('Failed to extract intermediate-AndroidX Design Library repositories')
    617         return False
    618 
    619     # Don't bother extracting resources -- this should only be used with AAPT2.
    620     return transform_maven_repos([design_dir],
    621                                  os.path.join(extras_dir, 'material-design-x'), extract_res=False)
    622 
    623 
    624 def extract_to(zip_file, paths, filename, parent_path):
    625     zip_path = next(filter(lambda path: filename in path, paths))
    626     src_path = zip_file.extract(zip_path)
    627     dst_path = path(parent_path, filename)
    628     mv(src_path, dst_path)
    629 
    630 
    631 def fetch_framework_artifacts(target, build_id, target_path, is_current_sdk):
    632     platform = 'darwin' if 'mac' in target else 'linux'
    633     artifact_path = fetch_artifact(
    634         target, build_id.url_id, 'sdk-repo-%s-platforms-%s.zip' % (platform, build_id.fs_id))
    635     if not artifact_path:
    636         return False
    637 
    638     with zipfile.ZipFile(artifact_path) as zipFile:
    639         paths = zipFile.namelist()
    640 
    641         filenames = ['android.jar', 'uiautomator.jar',  'framework.aidl',
    642             'optional/android.test.base.jar', 'optional/android.test.mock.jar',
    643             'optional/android.test.runner.jar']
    644 
    645         for filename in filenames:
    646             extract_to(zipFile, paths, filename, target_path)
    647 
    648         if is_current_sdk:
    649             # There's no system version of framework.aidl, so use the public one.
    650             extract_to(zipFile, paths, 'framework.aidl', system_path)
    651 
    652             # We don't keep historical artifacts for these.
    653             artifact_path = fetch_artifact(target, build_id.fs_id, 'core.current.stubs.jar')
    654             if not artifact_path:
    655                 return False
    656             mv(artifact_path, path(current_path, 'core.jar'))
    657 
    658     return True
    659 
    660 
    661 def update_sdk_repo(target, build_id):
    662     return fetch_framework_artifacts(target, build_id, current_path, is_current_sdk = True)
    663 
    664 
    665 def update_system(target, build_id):
    666     artifact_path = fetch_artifact(target, build_id.url_id, 'android_system.jar')
    667     if not artifact_path:
    668         return False
    669 
    670     mv(artifact_path, path(system_path, 'android.jar'))
    671 
    672     artifact_path = fetch_artifact(target, build_id.url_id, 'android.test.mock.stubs_system.jar')
    673     if not artifact_path:
    674         return False
    675 
    676     mv(artifact_path, path(system_path, 'optional/android.test.mock.jar'))
    677 
    678     return True
    679 
    680 
    681 
    682 def finalize_sdk(target, build_id, sdk_version):
    683     target_finalize_dir = "%d" % sdk_version
    684     target_finalize_system_dir = "system_%d" % sdk_version
    685 
    686     artifact_to_path = {
    687       'android_system.jar': path(target_finalize_system_dir, 'android.jar'),
    688       'public_api.txt': path(api_path, "%d.txt" % sdk_version),
    689       'system-api.txt': path(system_api_path, "%d.txt" % sdk_version),
    690     }
    691 
    692     for artifact, target_path in artifact_to_path.items():
    693         artifact_path = fetch_artifact(target, build_id.url_id, artifact)
    694         if not artifact_path:
    695             return False
    696         mv(artifact_path, target_path)
    697 
    698     if not fetch_framework_artifacts(target, build_id, target_finalize_dir, is_current_sdk = False):
    699       return False
    700 
    701     copyfile(path(target_finalize_dir, 'framework.aidl'),
    702              path(target_finalize_system_dir, 'framework.aidl'))
    703     return True
    704 
    705 
    706 def update_buildtools(target, arch, build_id):
    707     artifact_path = fetch_and_extract(target, build_id.url_id,
    708                                    "sdk-repo-%s-build-tools-%s.zip" % (arch, build_id.fs_id))
    709     if not artifact_path:
    710         return False
    711 
    712     top_level_dir = os.listdir(artifact_path)[0]
    713     src_path = os.path.join(artifact_path, top_level_dir)
    714     dst_path = path(buildtools_dir, arch)
    715     mv(src_path, dst_path)
    716 
    717     # Move all top-level files to /bin and make them executable
    718     bin_path = path(dst_path, 'bin')
    719     top_level_files = filter(lambda e: os.path.isfile(path(dst_path, e)), os.listdir(dst_path))
    720     for file in top_level_files:
    721         src_file = path(dst_path, file)
    722         dst_file = path(bin_path, file)
    723         mv(src_file, dst_file)
    724         os.chmod(dst_file, 0o755)
    725 
    726     # Remove renderscript
    727     rm(path(dst_path, 'renderscript'))
    728 
    729     return True
    730 
    731 
    732 def append(text, more_text):
    733     if text:
    734         return "%s, %s" % (text, more_text)
    735     return more_text
    736 
    737 
    738 class buildId(object):
    739   def __init__(self, url_id, fs_id):
    740     # id when used in build server urls
    741     self.url_id = url_id
    742     # id when used in build commands
    743     self.fs_id = fs_id
    744 
    745 def getBuildId(args):
    746   # must be in the format 12345 or P12345
    747   source = args.source
    748   number_text = source[:]
    749   presubmit = False
    750   if number_text.startswith("P"):
    751     presubmit = True
    752     number_text = number_text[1:]
    753   if not number_text.isnumeric():
    754     return None
    755   url_id = source
    756   fs_id = url_id
    757   if presubmit:
    758     fs_id = "0"
    759   args.file = False
    760   return buildId(url_id, fs_id)
    761 
    762 def getFile(args):
    763   source = args.source
    764   if not source.isnumeric():
    765     return args.source
    766   return None
    767 
    768 
    769 def script_relative(rel_path):
    770     return os.path.join(script_dir, rel_path)
    771 
    772 
    773 def uncommittedChangesExist():
    774     try:
    775         # Make sure we don't overwrite any pending changes.
    776         diffCommand = "cd " + git_dir + " && git diff --quiet"
    777         subprocess.check_call(diffCommand, shell=True)
    778         subprocess.check_call(diffCommand + " --cached", shell=True)
    779         return False
    780     except subprocess.CalledProcessError:
    781         return True
    782 
    783 
    784 rm(temp_dir)
    785 parser = argparse.ArgumentParser(
    786     description=('Update current prebuilts'))
    787 parser.add_argument(
    788     'source',
    789     help='Build server build ID or local Maven ZIP file')
    790 parser.add_argument(
    791     '-d', '--design', action="store_true",
    792     help='If specified, updates only the Design Library')
    793 parser.add_argument(
    794     '-m', '--material', action="store_true",
    795     help='If specified, updates only the intermediate-AndroidX Design Library')
    796 parser.add_argument(
    797     '-c', '--constraint', action="store_true",
    798     help='If specified, updates only Constraint Layout')
    799 parser.add_argument(
    800     '--constraint_x', action="store_true",
    801     help='If specified, updates Constraint Layout X')
    802 parser.add_argument(
    803     '-j', '--jetifier', action="store_true",
    804     help='If specified, updates only Jetifier')
    805 parser.add_argument(
    806     '-p', '--platform', action="store_true",
    807     help='If specified, updates only the Android Platform')
    808 parser.add_argument(
    809     '-f', '--finalize_sdk', type=int,
    810     help='If specified, imports the source build as the specified finalized SDK version')
    811 parser.add_argument(
    812     '-b', '--buildtools', action="store_true",
    813     help='If specified, updates only the Build Tools')
    814 parser.add_argument(
    815     '--stx', action="store_true",
    816     help='If specified, updates Support Library and Androidx (that is, all artifacts built from frameworks/support)')
    817 parser.add_argument(
    818     '--commit-first', action="store_true",
    819     help='If specified, then if uncommited changes exist, commit before continuing')
    820 args = parser.parse_args()
    821 if args.stx:
    822     args.support = args.androidx = True
    823 else:
    824     args.support = args.androidx = False
    825 args.file = True
    826 if not args.source:
    827     parser.error("You must specify a build ID or local Maven ZIP file")
    828     sys.exit(1)
    829 if not (args.support or args.platform or args.constraint or args.buildtools \
    830                 or args.design or args.jetifier or args.androidx or args.material \
    831                 or args.finalize_sdk or args.constraint_x):
    832     parser.error("You must specify at least one target to update")
    833     sys.exit(1)
    834 if (args.support or args.constraint or args.constraint_x or args.design or args.material or args.androidx) \
    835         and which('pom2bp') is None:
    836     parser.error("Cannot find pom2bp in path; please run lunch to set up build environment")
    837     sys.exit(1)
    838 
    839 if uncommittedChangesExist():
    840     if args.commit_first:
    841         subprocess.check_call("cd " + git_dir + " && git add -u", shell=True)
    842         subprocess.check_call("cd " + git_dir + " && git commit -m 'save working state'", shell=True)
    843 
    844 if uncommittedChangesExist():
    845     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__))
    846     sys.exit(1)
    847 
    848 try:
    849     components = None
    850     if args.constraint:
    851         if update_constraint('studio', getBuildId(args)):
    852             components = append(components, 'Constraint Layout')
    853         else:
    854             print_e('Failed to update Constraint Layout, aborting...')
    855             sys.exit(1)
    856     if args.constraint_x:
    857         if update_constraint_x(getFile(args)):
    858             components = append(components, 'Constraint Layout X')
    859         else:
    860             print_e('Failed to update Constraint Layout X, aborting...')
    861             sys.exit(1)
    862     if args.support:
    863         if update_support('support_library', getBuildId(args), getFile(args)):
    864             components = append(components, 'Support Library')
    865         else:
    866             print_e('Failed to update Support Library, aborting...')
    867             sys.exit(1)
    868     if args.androidx:
    869         if update_androidx('support_library', \
    870                            getBuildId(args), getFile(args)):
    871             components = append(components, 'AndroidX')
    872         else:
    873             print_e('Failed to update AndroidX, aborting...')
    874             sys.exit(1)
    875     if args.jetifier:
    876         if update_jetifier('support_library', getBuildId(args)):
    877             components = append(components, 'Jetifier')
    878         else:
    879             print_e('Failed to update Jetifier, aborting...')
    880             sys.exit(1)
    881     if args.platform  or args.finalize_sdk:
    882         if update_sdk_repo('sdk_phone_armv7-sdk_mac', getBuildId(args)) \
    883                 and update_system('sdk_phone_armv7-sdk_mac', getBuildId(args)):
    884             components = append(components, 'platform SDK')
    885         else:
    886             print_e('Failed to update platform SDK, aborting...')
    887             sys.exit(1)
    888     if args.finalize_sdk:
    889         n = args.finalize_sdk
    890         if finalize_sdk('sdk_phone_armv7-sdk_mac', getBuildId(args), n):
    891             # We commit the finalized dir separately from the current sdk update.
    892             msg = "Import final sdk version %d from build %s" % (n, getBuildId(args).url_id)
    893             dirs = ["%d" % n, 'system_%d' % n, api_path, system_api_path]
    894             subprocess.check_call(['git', 'add'] + dirs)
    895             subprocess.check_call(['git', 'commit', '-m', msg])
    896         else:
    897             print_e('Failed to finalize SDK %d, aborting...' % n)
    898             sys.exit(1)
    899     if args.design:
    900         if update_design(getFile(args)):
    901             components = append(components, 'Design Library')
    902         else:
    903             print_e('Failed to update Design Library, aborting...')
    904             sys.exit(1)
    905     if args.material:
    906         if update_material(getFile(args)):
    907             components = append(components, 'intermediate-AndroidX Design Library')
    908         else:
    909             print_e('Failed to update intermediate-AndroidX Design Library, aborting...')
    910             sys.exit(1)
    911     if args.buildtools:
    912         if update_buildtools('sdk_phone_armv7-sdk_mac', 'darwin', getBuildId(args)) \
    913                 and update_buildtools('sdk_phone_x86_64-sdk', 'linux', getBuildId(args)) \
    914                 and update_buildtools('sdk_phone_armv7-win_sdk', 'windows', getBuildId(args)):
    915             components = append(components, 'build tools')
    916         else:
    917             print_e('Failed to update build tools, aborting...')
    918             sys.exit(1)
    919 
    920 
    921 
    922     subprocess.check_call(['git', 'add', current_path, system_path, buildtools_dir])
    923     if not args.source.isnumeric():
    924         src_msg = "local Maven ZIP"
    925     else:
    926         src_msg = "build %s" % (getBuildId(args).url_id)
    927     msg = "Import %s from %s\n\n%s" % (components, src_msg, flatten(sys.argv))
    928     subprocess.check_call(['git', 'commit', '-m', msg])
    929     if args.finalize_sdk:
    930         print('NOTE: Created two commits:')
    931         subprocess.check_call(['git', 'log', '-2', '--oneline'])
    932     print('Remember to test this change before uploading it to Gerrit!')
    933 
    934 finally:
    935     # Revert all stray files, including the downloaded zip.
    936     try:
    937         with open(os.devnull, 'w') as bitbucket:
    938             subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
    939             subprocess.check_call(
    940                 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!', '--allow-empty'], stdout=bitbucket)
    941             subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
    942     except subprocess.CalledProcessError:
    943         print_e('ERROR: Failed cleaning up, manual cleanup required!!!')
    944