Android Jetpack 之AndroidX

随着Android 9.0(API级别28)的发布,有一个名为AndroidX的支持库的新版本, 它是Jetpack的一部分。AndroidX库包含现有的支持库,还包括最新的Jetpack组件。 

您可以继续使用支持库。历史工件(那些版本为27及更早版本,打包为android.support.*)将继续在Google Maven上提供。但是,所有新的库开发都将在AndroidX库中进行。 

我们建议在所有新项目中使用AndroidX库。您还应该考虑将 现有项目迁移到AndroidX。

现有项目向Androidx库迁移

1.创建一个普通项目

2.在项目的gradle.properties文件里添加如下配置

#迁移项目到Androidx
android.useAndroidX=true
android.enableJetifier=true

3.在AndroidStudio的菜单中找到 ReFactor 下的 Migrate to AndroidX

4.它会提示是否保存工作区

5.然后根据提示修改冲突,如果你是和我一样新建的项目,一般都是依赖问题。

你只需要把 com.android.support 的包替换为Androidx的包就好。

比如刚才新建的项目,替换为这个就好。


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

具体替换点击查看google官网指南。

注意:

许多第三方库并没有迁移 androidx库,所以项目在使用 androidx库时应当注意兼容问题!!!

猜你喜欢

转载自blog.csdn.net/qq_41346910/article/details/90245192