将项目从 support 库手动升级到 androidx

将项目从 support 库手动升级到 androidx

原来的 support 库转换到 androidx 都有相对于的库,官方地址如下:

需要科学上网

google 原 support 库对应的 androidx 库

例如原来的 com.android.support:appcompat-v7 库,转换到 androidx 对应的 库为:androidx.appcompat:appcompat

也就是将 app 中的 dependencies

implementation 'com.android.support:appcompat-v7:28.0.0'

改成

implementation 'androidx.appcompat:appcompat:1.0.0-beta01'

升级到 androidx

升级前,确保 android studio 版本在 3.2 及以上

Android studio 支持一键升级, 点击 Refactor -> Migrate to Androidx 即可

下面是手动升级

修改工程的 gradle.properties

在其中添加最后两项

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# 升级到 androidx 添加两行
android.useAndroidX=true
android.enableJetifier=true

修改 App 的 build.gradle 文件

修改项目依赖,

//    升级到 androidx 不能再使用 com.android.support
//    而要使用下方 androidx.appcompat 
//    implementation 'com.android.support:appcompat-v7:28.0.0'
//    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

如果项目还有其它依赖,需要逐个替换,原支持库对应的 androidx 支持库查看官方地址:

该链接需要科学上网,也可查看末尾的对应库

google 原 support 库对应的 androidx 库

需要注意的是,升级后,如果项目中有使用混淆等,同样需要逐个修改

原 support 库对应的 androix 类映射列表

修改项目中使用到原support 库的代码

例如原来项目中使用了 AppCompatActivity

导包时是 import android.support.v7.app.AppCompatActivity;

需要修改为 import androidx.appcompat.app.AppCompatActivity;

总的来说,库依赖的修改比较简单,需要逐个修改导包很麻烦

google 推荐的方式是开辟新分支,在新分支中升级

原support 库对应的 androidx 库

原support 库androix库
android.arch.core:commonandroidx.arch.core:core-common
android.arch.core:coreandroidx.arch.core:core
android.arch.core:core-testingandroidx.arch.core:core-testing
android.arch.core:runtimeandroidx.arch.core:core-runtime
android.arch.lifecycle:commonandroidx.lifecycle:lifecycle-common
android.arch.lifecycle:common-java8androidx.lifecycle:lifecycle-common-java8
android.arch.lifecycle:compilerandroidx.lifecycle:lifecycle-compiler
android.arch.lifecycle:extensionsandroidx.lifecycle:lifecycle-extensions
android.arch.lifecycle:livedataandroidx.lifecycle:lifecycle-livedata
android.arch.lifecycle:livedata-coreandroidx.lifecycle:lifecycle-livedata-core
android.arch.lifecycle:reactivestreamsandroidx.lifecycle:lifecycle-reactivestreams
android.arch.lifecycle:runtimeandroidx.lifecycle:lifecycle-runtime
android.arch.lifecycle:viewmodelandroidx.lifecycle:lifecycle-viewmodel
android.arch.paging:commonandroidx.paging:paging-common
android.arch.paging:runtimeandroidx.paging:paging-runtime
android.arch.paging:rxjava2androidx.paging:paging-rxjava2
android.arch.persistence.room:commonandroidx.room:room-common
android.arch.persistence.room:compilerandroidx.room:room-compiler
android.arch.persistence.room:guavaandroidx.room:room-guava
android.arch.persistence.room:migrationandroidx.room:room-migration
android.arch.persistence.room:runtimeandroidx.room:room-runtime
android.arch.persistence.room:rxjava2androidx.room:room-rxjava2
android.arch.persistence.room:testingandroidx.room:room-testing
android.arch.persistence:dbandroidx.sqlite:sqlite
android.arch.persistence:db-frameworkandroidx.sqlite:sqlite-framework
com.android.support.constraint:constraint-layoutandroidx.constraintlayout:constraintlayout
com.android.support.constraint:constraint-layout-solverandroidx.constraintlayout:constraintlayout-solver
com.android.support.test.espresso.idling:idling-concurrentandroidx.test.espresso.idling:idling-concurrent
com.android.support.test.espresso.idling:idling-netandroidx.test.espresso.idling:idling-net
com.android.support.test.espresso:espresso-accessibilityandroidx.test.espresso:espresso-accessibility
com.android.support.test.espresso:espresso-contribandroidx.test.espresso:espresso-contrib
com.android.support.test.espresso:espresso-coreandroidx.test.espresso:espresso-core
com.android.support.test.espresso:espresso-idling-resourceandroidx.test.espresso:espresso-idling-resource
com.android.support.test.espresso:espresso-intentsandroidx.test.espresso:espresso-intents
com.android.support.test.espresso:espresso-remoteandroidx.test.espresso:espresso-remote
com.android.support.test.espresso:espresso-webandroidx.test.espresso:espresso-web
com.android.support.test.janktesthelper:janktesthelperandroidx.test.jank:janktesthelper
com.android.support.test.services:test-servicesandroidx.test:test-services
com.android.support.test.uiautomator:uiautomatorandroidx.test.uiautomator:uiautomator
com.android.support.test:monitorandroidx.test:monitor
com.android.support.test:orchestratorandroidx.test:orchestrator
com.android.support.test:rulesandroidx.test:rules
com.android.support.test:runnerandroidx.test:runner
com.android.support:animated-vector-drawableandroidx.vectordrawable:vectordrawable-animated
com.android.support:appcompat-v7androidx.appcompat:appcompat
com.android.support:asynclayoutinflaterandroidx.asynclayoutinflater:asynclayoutinflater
com.android.support:carandroidx.car:car-alpha5
com.android.support:cardview-v7androidx.cardview:cardview
com.android.support:collectionsandroidx.collection:collection
com.android.support:coordinatorlayoutandroidx.coordinatorlayout:coordinatorlayout
com.android.support:cursoradapterandroidx.cursoradapter:cursoradapter
com.android.support:customtabsandroidx.browser:browser
com.android.support:customviewandroidx.customview:customview
com.android.support:designcom.google.android.material:material-rc01
com.android.support:documentfileandroidx.documentfile:documentfile
com.android.support:drawerlayoutandroidx.drawerlayout:drawerlayout
com.android.support:exifinterfaceandroidx.exifinterface:exifinterface
com.android.support:gridlayout-v7androidx.gridlayout:gridlayout
com.android.support:heifwriterandroidx.heifwriter:heifwriter
com.android.support:interpolatorandroidx.interpolator:interpolator
com.android.support:leanback-v17androidx.leanback:leanback
com.android.support:loaderandroidx.loader:loader
com.android.support:localbroadcastmanagerandroidx.localbroadcastmanager:localbroadcastmanager
com.android.support:media2androidx.media2:media2-alpha03
com.android.support:media2-exoplayerandroidx.media2:media2-exoplayer-alpha01
com.android.support:mediarouter-v7androidx.mediarouter:mediarouter
com.android.support:multidexandroidx.multidex:multidex
com.android.support:multidex-instrumentationandroidx.multidex:multidex-instrumentation
com.android.support:palette-v7androidx.palette:palette
com.android.support:percentandroidx.percentlayout:percentlayout
com.android.support:preference-leanback-v17androidx.leanback:leanback-preference
com.android.support:preference-v14androidx.legacy:legacy-preference-v14
com.android.support:preference-v7androidx.preference:preference
com.android.support:printandroidx.print:print
com.android.support:recommendationandroidx.recommendation:recommendation
com.android.support:recyclerview-selectionandroidx.recyclerview:recyclerview-selection
com.android.support:recyclerview-v7androidx.recyclerview:recyclerview
com.android.support:slices-buildersandroidx.slice:slice-builders
com.android.support:slices-coreandroidx.slice:slice-core
com.android.support:slices-viewandroidx.slice:slice-view
com.android.support:slidingpanelayoutandroidx.slidingpanelayout:slidingpanelayout
com.android.support:support-annotationsandroidx.annotation:annotation
com.android.support:support-compatandroidx.core:core
com.android.support:support-contentandroidx.contentpager:contentpager
com.android.support:support-core-uiandroidx.legacy:legacy-support-core-ui
com.android.support:support-core-utilsandroidx.legacy:legacy-support-core-utils
com.android.support:support-dynamic-animationandroidx.dynamicanimation:dynamicanimation
com.android.support:support-emojiandroidx.emoji:emoji
com.android.support:support-emoji-appcompatandroidx.emoji:emoji-appcompat
com.android.support:support-emoji-bundledandroidx.emoji:emoji-bundled
com.android.support:support-fragmentandroidx.fragment:fragment
com.android.support:support-media-compatandroidx.media:media
com.android.support:support-tv-providerandroidx.tvprovider:tvprovider
com.android.support:support-v13androidx.legacy:legacy-support-v13
com.android.support:support-v4androidx.legacy:legacy-support-v4
com.android.support:support-vector-drawableandroidx.vectordrawable:vectordrawable
com.android.support:swiperefreshlayoutandroidx.swiperefreshlayout:swiperefreshlayout
com.android.support:textclassifierandroidx.textclassifier:textclassifier
com.android.support:transitionandroidx.transition:transition
com.android.support:versionedparcelableandroidx.versionedparcelable:versionedparcelable
com.android.support:viewpagerandroidx.viewpager:viewpager
com.android.support:wearandroidx.wear:wear
com.android.support:webkitandroidx.webkit:webkit

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://zwc365.com/2020/02/09/android-support-to-androidx