当前位置: 首页 > 工具软件 > androidx > 使用案例 >

安卓升级AndroidX手把手教程

乜烨霖
2023-12-01

突然发现好多安卓项目已经改为了AndroidX,那么自己的项目也该升级一下了。下面是AndroidX的官方文档的概述。

 

AndroidX概述

 

AndroidX是Android团队用于在Jetpack中开发,测试,打包,版本和发布库的开源项目 。

AndroidX是对原始Android 支持库的重大改进 。与支持库一样,AndroidX与Android操作系统分开提供,并提供跨Android版本的向后兼容性。AndroidX通过提供功能奇偶校验和新库完全取代了支持库。此外,AndroidX还包括以下功能:

AndroidX中的所有软件包都以字符串开头,位于一致的命名空间中androidx。支持库包已映射到相应的androidx.*包中。
与支持库不同,AndroidX软件包是单独维护和更新的。这些androidx包使用 从版本1.0.0开始的严格语义版本控制。您可以单独更新项目中的AndroidX库。
所有新的支持库开发都将在AndroidX库中进行。这包括维护原始支持库工件和引入新的Jetpack组件。


1、修改project目录下 gradle.properties,在里面添加两行代码:

# 表示使用 androidx
android.useAndroidX=true
# 表示将第三方库迁移到 androidx
android.enableJetifier=true

2、点击菜单项中的Refactor中的Migratet to AndroidX。然后点击窗口右上角的sync Now。(adnroid studio版本必须在3.3及以上)

3、按理说这样就可以了,但是我改了之后发现项目一片报红,上网搜了下发现需要修改引用包,都需要改为AndroidX的包,下面是我遇到的几个错误:

     a、AndroidManifest.xml中的application中的activity全部报红,解决方法:修改activity引用的包为:

import androidx.appcompat.app.AppCompatActivity

         然后再app的build.gradle中的android中添加以下代码:

    lintOptions {
        disable 'GoogleAppIndexingWarning'
    }

    b、剩下的还是各种包导入错误,之前的v4、v7包全部不能用了,都需要改。

    c、布局文件也一定要改,比如ViewPager由android.support.v4.view.ViewPager改为了androidx.viewpager.widget.ViewPager;CardView由android.support.v7.widget.CardView改为了androidx.cardview.widget.CardView;RecycleView由android.support.v7.widget.RecycleView改为了com.scwang.smartrefresh.layout.SmartRefreshLayout;FloatingActionButton也改为了com.google.android.material.floatingactionbutton.FloatingActionButton。

这只是一部分,下面的是官网上的介绍:

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

 

最后贴上官方迁移AndroidX的文档。

 类似资料: