Reprint please indicate the source: juejin.cn/post/684490…

This article is from Rong Hua Xie Hou’s blog

1. Introduction

Everyone is familiar with Android Support Libray. With the rapid iteration of Android version, Google launched Support Libray to be compatible with lower versions of Android devices. However, as more and more versions have been released, the maintenance and use of the support library has become more and more complex, so AndroidX came into being. Let’s learn how to migrate to AndroidX.

AndroidX was developed iteratively based on Android Support Libray version 28.0.0.

Environmental requirements:

  • Android Studio 3.2.0 or later

  • CompileSdkVersion 28 or later

2. Migrate to AndroidX

Click Refactor — >Migrate to AndroidX in the AS toolbar and the following prompt box will appear:

You are advised to Backup your current project before migrating. If you select [Backup project as Zip File] and click [MIGRATE], you will be prompted to select a Backup path.

Click on the “DO REFACTOR” button to continue:

There is a warning saying that the code to be modified is not found, it does not matter, click the search, click the “DO REFACTOR” button again, after a period of compilation, migration is completed, at this time to compile the whole project, it may prompt some class can not find the problem, re-import is ok.

Note: as the support library is upgraded to AndroidX, some third party libraries using the support library will also need to be upgraded to the latest version, such as rxlifecycle etc. Most of the major third party libraries currently support AndroidX.

3. The comparison

Take a look at the differences from the original project:

dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com. Android. Support: appcompat - v7:28.0.0'
    implementation 'com. Android. Support. The constraint, the constraint - layout: 1.1.3'
    testImplementation 'junit: junit: 4.12'
    androidTestImplementation 'com. Android. Support. Test: runner: 1.0.2'
    androidTestImplementation 'com. Android. Support. Test. Espresso: espresso - core: 3.0.2'
}
Copy the code

Was amended to read:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx. Appcompat: appcompat: 1.0.2'
    implementation 'androidx. Constraintlayout: constraintlayout: 2.0.0 - alpha2'
    testImplementation 'junit: junit: 4.12'
    androidTestImplementation 'androidx. Test: runner: 1.1.0'
    androidTestImplementation 'androidx. Test. Espresso: espresso - core: 3.1.0'
}
Copy the code

You can see that com.android.support has all been changed to AndroidX, the version number has been changed, and the package name of the dependency reference in the project has also been changed.

gradle.properties

The following two configurations are added:

  • android.useAndroidX=true

    When set to true, the Android plugin uses the corresponding AndroidX library instead of the support library, which is false by default.

  • android.enableJetifier=true

    When set to true, the Android plugin automatically migrates existing third-party libraries and uses AndroidX by overwriting their binaries, which defaults to false.

4 support library migration AndroidX quick lookup table

Old build artifact AndroidX build artifact
android.arch.core:common Androidx. Arch. The core: the core – common: 2.0.0 – rc01
android.arch.core:core Androidx. Arch. The core: the core: 2.0.0 – rc01
android.arch.core:core-testing Androidx. Arch. The core: the core – testing: 2.0.0 – rc01
android.arch.core:runtime Androidx. Arch. The core: the core – the runtime: 2.0.0 – rc01
android.arch.lifecycle:common Androidx. Lifecycle: lifecycle – common: 2.0.0 – rc01
android.arch.lifecycle:common-java8 Androidx. Lifecycle: lifecycle – common – java8:2.0.0 – rc01
android.arch.lifecycle:compiler Androidx. Lifecycle: lifecycle – compiler: 2.0.0 – rc01
android.arch.lifecycle:extensions Androidx. Lifecycle: lifecycle – extensions: 2.0.0 – rc01
android.arch.lifecycle:livedata Androidx. Lifecycle: lifecycle – livedata: 2.0.0 – rc01
android.arch.lifecycle:livedata-core Androidx. Lifecycle: lifecycle – livedata – core: 2.0.0 – rc01
android.arch.lifecycle:reactivestreams Androidx. Lifecycle: lifecycle – reactivestreams: 2.0.0 – rc01
android.arch.lifecycle:runtime Androidx. Lifecycle: lifecycle – runtime: 2.0.0 – rc01
android.arch.lifecycle:viewmodel Androidx. Lifecycle: lifecycle – viewmodel: 2.0.0 – rc01
android.arch.paging:common Androidx. The paging: the paging – common: 2.0.0 – rc01
android.arch.paging:runtime Androidx. The paging: the paging – runtime: 2.0.0 – rc01
android.arch.paging:rxjava2 Androidx. The paging: the paging – rxjava2:2.0.0 – rc01
android.arch.persistence.room:common Androidx. Room: room – common: 2.0.0 – rc01
android.arch.persistence.room:compiler Androidx. Room: room – the compiler: 2.0.0 – rc01
android.arch.persistence.room:guava Androidx. Room: room – guava: 2.0.0 – rc01
android.arch.persistence.room:migration Androidx. Room: room – migration: 2.0.0 – rc01
android.arch.persistence.room:runtime Androidx. Room: room – the runtime: 2.0.0 – rc01
android.arch.persistence.room:rxjava2 Androidx. Room: room – rxjava2:2.0.0 – rc01
android.arch.persistence.room:testing Androidx. Room: room – testing: 2.0.0 – rc01
android.arch.persistence:db Androidx. Sqlite: sqlite: 2.0.0 – rc01
android.arch.persistence:db-framework Androidx. Sqlite: sqlite – framework: 2.0.0 – rc01
com.android.support.constraint:constraint-layout Androidx. Constraintlayout: constraintlayout: 1.1.2
com.android.support.constraint:constraint-layout-solver Androidx. Constraintlayout: constraintlayout – solver: 1.1.2
com.android.support.test.espresso.idling:idling-concurrent Androidx. Test. Espresso. Idling: idling – concurrent: 3.1.0
com.android.support.test.espresso.idling:idling-net Androidx. Test. Espresso. Idling: idling – net: 3.1.0
com.android.support.test.espresso:espresso-accessibility Androidx. Test. Espresso: espresso – the org.eclipse.swt.accessibility: 3.1.0
com.android.support.test.espresso:espresso-contrib Androidx. Test. Espresso: espresso – contrib: 3.1.0
com.android.support.test.espresso:espresso-core Androidx. Test. Espresso: espresso – core: 3.1.0
com.android.support.test.espresso:espresso-idling-resource Androidx. Test. Espresso: espresso – idling – resource: 3.1.0
com.android.support.test.espresso:espresso-intents Androidx. Test. Espresso: espresso – the intents: 3.1.0
com.android.support.test.espresso:espresso-remote Androidx. Test. Espresso: espresso – remote: 3.1.0
com.android.support.test.espresso:espresso-web Androidx. Test. Espresso: espresso – web: 3.1.0
com.android.support.test.janktesthelper:janktesthelper Androidx. Test. Jank: janktesthelper: 1.0.1
com.android.support.test.services:test-services Androidx. Test: test – services: 1.1.0
com.android.support.test.uiautomator:uiautomator Androidx. Test. Uiautomator: uiautomator: 2.2.0
com.android.support.test:monitor Androidx. Test: the monitor: 1.1.0
com.android.support.test:orchestrator The: androidx orchestrator: 1.1.0
com.android.support.test:rules Androidx. Test: rules: 1.1.0
com.android.support.test:runner Androidx. Test: runner: 1.1.0
com.android.support:animated-vector-drawable Androidx. Vectordrawable: vectordrawable – animated: 1.0.0
com.android.support:appcompat-v7 Androidx. Appcompat: appcompat: 1.0.0
com.android.support:asynclayoutinflater Androidx. Asynclayoutinflater: asynclayoutinflater: 1.0.0
com.android.support:car Androidx. Car: car: 1.0.0 – alpha5
com.android.support:cardview-v7 Androidx. Cardview: cardview: 1.0.0
com.android.support:collections Androidx. Collection: collection: 1.0.0
com.android.support:coordinatorlayout Androidx. Coordinatorlayout: coordinatorlayout: 1.0.0
com.android.support:cursoradapter Androidx. Cursoradapter: cursoradapter: 1.0.0
com.android.support:customtabs Androidx. Browser: the browser: 1.0.0
com.android.support:customview Androidx. Customview: customview: 1.0.0
com.android.support:design Com. Google. Android. Material: material: 1.0.0 – rc01
com.android.support:documentfile Androidx. Documentfile: documentfile: 1.0.0
com.android.support:drawerlayout Androidx. Drawerlayout: drawerlayout: 1.0.0
com.android.support:exifinterface Androidx. Exifinterface: exifinterface: 1.0.0
com.android.support:gridlayout-v7 Androidx. Gridlayout: gridlayout: 1.0.0
com.android.support:heifwriter Androidx. Heifwriter: heifwriter: 1.0.0
com.android.support:interpolator Androidx. Interpolator: interpolator: 1.0.0
com.android.support:leanback-v17 Androidx. Leanback: leanback: 1.0.0
com.android.support:loader Androidx. Loader, loader: 1.0.0
com.android.support:localbroadcastmanager Androidx. Localbroadcastmanager: localbroadcastmanager: 1.0.0
com.android.support:media2 Androidx. Media2: media2:1.0.0 – alpha03
com.android.support:media2-exoplayer Androidx. Media2: media2 – exoplayer: 1.0.0 – alpha01
com.android.support:mediarouter-v7 Androidx. Mediarouter: mediarouter: 1.0.0
com.android.support:multidex Androidx. Multidex: multidex: 2.0.0
com.android.support:multidex-instrumentation Androidx. Multidex: multidex – instrumentation: 2.0.0
com.android.support:palette-v7 Androidx. The palette: palette: 1.0.0
com.android.support:percent Androidx. Percentlayout: percentlayout: 1.0.0
com.android.support:preference-leanback-v17 Androidx. Leanback: leanback – preference: 1.0.0
com.android.support:preference-v14 Androidx. Legacy: legacy – preference – v14:1.0.0
com.android.support:preference-v7 Androidx. Preference: preference: 1.0.0
com.android.support:print Androidx. Print: print: 1.0.0
com.android.support:recommendation Androidx. Recommendation: recommendation: 1.0.0
com.android.support:recyclerview-selection Androidx. Recyclerview: recyclerview – selection: 1.0.0
com.android.support:recyclerview-v7 Androidx. Recyclerview: recyclerview: 1.0.0
com.android.support:slices-builders Androidx. Slice, slice – builders: 1.0.0
com.android.support:slices-core Androidx. Slice, slice – core: 1.0.0
com.android.support:slices-view Androidx. Slice, slice – view: 1.0.0
com.android.support:slidingpanelayout Androidx. Slidingpanelayout: slidingpanelayout: 1.0.0
com.android.support:support-annotations Androidx. Annotation: the annotation: 1.0.0
com.android.support:support-compat Androidx. Core: the core: 1.0.0
com.android.support:support-content Androidx. Contentpager: contentpager: 1.0.0
com.android.support:support-core-ui Androidx. Legacy: legacy – support – core – UI: 1.0.0
com.android.support:support-core-utils Androidx. Legacy: legacy – support – core – utils: 1.0.0
com.android.support:support-dynamic-animation Androidx. Dynamicanimation: dynamicanimation: 1.0.0
com.android.support:support-emoji Androidx. Emoji: emoji: 1.0.0
com.android.support:support-emoji-appcompat Androidx. Emoji: emoji – appcompat: 1.0.0
com.android.support:support-emoji-bundled Androidx. Emoji: emoji – bundled: 1.0.0
com.android.support:support-fragment Androidx. Fragments: fragments: 1.0.0
com.android.support:support-media-compat Androidx. Media: media: 1.0.0
com.android.support:support-tv-provider Androidx. Tvprovider: tvprovider: 1.0.0
com.android.support:support-v13 Androidx. Legacy: legacy support – v13:1.0.0
com.android.support:support-v4 Androidx. Legacy: legacy support – v4:1.0.0
com.android.support:support-vector-drawable Androidx. Vectordrawable: vectordrawable: 1.0.0
com.android.support:swiperefreshlayout Androidx. Swiperefreshlayout: swiperefreshlayout: 1.0.0
com.android.support:textclassifier Androidx. Textclassifier: textclassifier: 1.0.0
com.android.support:transition Androidx. The transition: the transition: 1.0.0
com.android.support:versionedparcelable Androidx. Versionedparcelable: versionedparcelable: 1.0.0
com.android.support:viewpager Androidx. Viewpager: viewpager: 1.0.0
com.android.support:wear Androidx. Wear: wear: 1.0.0
com.android.support:webkit Androidx. Its: its: 1.0.0

5. Some questions

After upgrading to AS 3.2, Android Lint added a number of code checks, the most obvious of which is the warning in Androidmanifest.xml:

App is not indexable by Google Search; consider adding at least one Activity with an ACTION-VIEW intent filter. See issue explanation for more details.
Copy the code

The APP does not support Google search, so you need to add an Intent filter to at least one Activity.

<action android:name="android.intent.action.VIEW" />
Copy the code

However, because of the Great Wall, it is useless to add this filter. Otherwise, the yellow waves on the page will look annoying. We can also do the following:

Add a warning ignore in your app’s build.gradle:

android {
    ...

    lintOptions {
        disable 'GoogleAppIndexingWarning'}}Copy the code

You can also add ignore to the application TAB in androidmanifest.xml:

tools:ignore="GoogleAppIndexingWarning"
Copy the code

6. Write at the end

To here, AndroidX upgrade related content is finished, I hope to help you, if you have some other problems in the process of upgrading AndroidX, welcome to leave a message to me.