android studio Android9(28)升级到Android10(29)Androidx小计

环境:Windows10,Android studio 3.6.2

1、参考Android官方推荐方式:https://developer.android.com/jetpack/androidx/migrate

2、由官方文档,第一步于gradle.properties文件添加

android.useAndroidX=true
android.enableJetifier=true

3、Android studio选择迁移到Androidx

                                      

Android studio会全局搜索不符合Androidx的导包等

                                     

替换前的依赖替换后的Androidx依赖
com.android.support:appcompat-v7:${version["androidSupportSdkVersion"]}
androidx.appcompat:appcompat:1.2.0
com.android.support:support-v4:${version["androidSupportSdkVersion"]}
androidx.legacy:legacy-support-v4:${version["androidXSdkVersion"]}
com.android.support:support-annotations:${version["androidSupportSdkVersion"]}
androidx.annotation:annotation:${version["androidXSdkVersion"]}
com.android.support:recyclerview-v7:${version["androidSupportSdkVersion"]}
androidx.recyclerview:recyclerview:${version["androidXSdkVersion"]}
com.android.support.constraint:constraint-layout:${version["constraintLayout"]}
androidx.constraintlayout:constraintlayout:2.0.2

com.android.support:cardview-v7:${version["androidSupportSdkVersion"]}

com.android.support:recyclerview-v7:${version["androidSupportSdkVersion"]}
androidx.core:core-ktx:1.3.2

配置文件:

ext {

    android = [
            compileSdkVersion: 29,
            buildToolsVersion: "29.0.3",
            minSdkVersion    : 21,
            targetSdkVersion : 29,
            versionCode      : VERSION_CODE as int,
            versionName      : APP_VERSION
    ]
    version = [
            androidSupportSdkVersion: "28.0.0",
            androidXSdkVersion      : "1.1.0",
            retrofitSdkVersion      : "2.2.0",
            butterknifeSdkVersion   : "8.8.1",
            constraintLayout        : "1.1.2",

            espressoSdkVersion      : "2.2.2",
            canarySdkVersion        : "1.5.1",
            dagger2SdkVersion       : "2.10",
            rxlifecycleSdkVersion   : "1.0",
            rxlifecycle2SdkVersion  : "2.0.1"

    ]


    dependencies = [
            //androidx
            "appcompat"                : "androidx.appcompat:appcompat:1.2.0",
            "core-ktx"                 : "androidx.core:core-ktx:1.3.2",
            "constraintlayout"         : "androidx.constraintlayout:constraintlayout:2.0.2",
            "legacy-support-v4"        : "androidx.legacy:legacy-support-v4:${version["androidXSdkVersion"]}",
            "annotationsX"             : "androidx.annotation:annotation:${version["androidXSdkVersion"]}",
            "recyclerview"             : "androidx.recyclerview:recyclerview:${version["androidXSdkVersion"]}",
            "designX"                  : "com.google.android.material:material:1.2.1",

            //support
            "appcompat-v7"             : "com.android.support:appcompat-v7:${version["androidSupportSdkVersion"]}",
            "exifinterface"            : "com.android.support:exifinterface:${version["androidSupportSdkVersion"]}",

            "design"                   : "com.android.support:design:${version["androidSupportSdkVersion"]}",
            "support-v4"               : "com.android.support:support-v4:${version["androidSupportSdkVersion"]}",
            "cardview-v7"              : "com.android.support:cardview-v7:${version["androidSupportSdkVersion"]}",
            "annotations"              : "com.android.support:support-annotations:${version["androidSupportSdkVersion"]}",
            "recyclerview-v7"          : "com.android.support:recyclerview-v7:${version["androidSupportSdkVersion"]}",
            "constraint-layout"        : "com.android.support.constraint:constraint-layout:${version["constraintLayout"]}",
...
    ]


}

使用:

implementation rootProject.ext.dependencies["appcompat"]

Ps:其中依赖:com.google.android.material:material:1.2.1 可能会报冲突错误:

error: duplicate value for resource 'attr/backgroundColor' with config '' com.google.android.material:material

解决办法用之前的依赖:com.android.support:design:${version["androidSupportSdkVersion"]}

 

4、需要手动修改的地方

替换方式(全局搜索替换快捷键->Ctrl+shift+r)

 

   1>xml文件

       

修改前android.support   修改后androidx

<android.support.v4.widget.NestedScrollView/>  

 <androidx.core.widget.NestedScrollView/>
<android.support.v4.widget.Space/>   <Space/>
<android.support.v7.widget.AppCompatTextView/>   <TextView/>

<android.support.v7.widget.CardView/>

<androidx.cardview.widget.CardView/>

<android.support.constraint.ConstraintLayout/>

<androidx.constraintlayout.widget.ConstraintLayout/>

<android.support.constraint.ConstraintLayout/>

<androidx.constraintlayout.widget.ConstraintLayout/>

<android.support.constraint.Group/>

<androidx.constraintlayout.widget.Group/>

<android.support.constraint.Guideline/><androidx.constraintlayout.widget.Guideline/>
......

 

 2>java文件

      

修改前android.support   修改后androidx

import android.support.v7.widget.RecyclerView

androidx.recyclerview.widget.RecyclerView
import android.support.annotation.NonNullimport androidx.annotation.NonNull
import android.support.v4.view.PagerAdapterimport androidx.viewpager.widget.PagerAdapter

import android.support.v4.view.ViewPager

import androidx.viewpager.widget.ViewPager

import android.support.annotation.AttrRes

import androidx.annotation.AttrRes

import android.support.annotation.Keep

import androidx.annotation.Keep

import android.support.v4.content.LocalBroadcastManager

import androidx.localbroadcastmanager.content.LocalBroadcastManager

import android.support.annotation.StringRes

import androidx.annotation.StringRes

......

 

 

分割线


end

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐