• android -------- AndroidX的迁移


    Google 2018 IO 大会推出了 Android新的扩展库 AndroidX,用于替换原来的 Android扩展库,将原来的android.*替换成androidx.*;只有包名和Maven工件名受到影响,原来的类名,方法名和字段名不会更改。接下来我们来看看使用 AndroidX的扩展库需要哪些配置。

    1. AndroidX变化

    1)常用依赖库对比:

    Old build artifactAndroidX build artifact
    com.android.support:appcompat-v7:28.0.2 androidx.appcompat:appcompat:1.0.0
    com.android.support:design:28.0.2 com.google.android.material:material:1.0.0
    com.android.support:support-v4:28.0.2 androidx.legacy:legacy-support-v4:1.0.0
    com.android.support:recyclerview-v7:28.0.2 androidx.recyclerview:recyclerview:1.0.0
    com.android.support.constraint:constraint-layout:1.1.2 androidx.constraintlayout:constraintlayout:1.1.2

     更多详细变化内容,可以下载CSV格式映射文件;

    2)常用支持库类对比:

    Support Library classAndroidX class
    android.support.v4.app.Fragment androidx.fragment.app.Fragment
    android.support.v4.app.FragmentActivity androidx.fragment.app.FragmentActivity
    android.support.v7.app.AppCompatActivity androidx.appcompat.app.AppCompatActivity
    android.support.v7.app.ActionBar androidx.appcompat.app.ActionBar
    android.support.v7.widget.RecyclerView androidx.recyclerview.widget.RecyclerView

     更多详细变化内容,可以下载CSV格式映射文件。

    2. AndroidX配置

    1)更新升级插件

    • 将AS更新至 AS 3.2及以上;
    • Gradle 插件版本改为 4.6及以上;
      项目下 gradle/wrapper/gradle-wrapper.propertie 文件中的distributionUrl改为:
    distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip
    
    • compileSdkVersion 版本升级到 28及以上;
    • buildToolsVersion 版本改为 28.0.2及以上。




    2)开启迁移AndroidX
     在项目的gradle.properties文件里添加如下配置:

    android.useAndroidX=true
    android.enableJetifier=true

     表示项目启用 AndroidX 并迁移到 AndroidX。

    3)替换依赖库
     修改项目app目录下的build.gradle依赖库:

    implementation 'com.android.support:appcompat-v7:28.0.2' → implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.android.support:design:28.0.2'  → implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2' → implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    ...

    4)修改支持库类
     将原来importandroid.**包删除,重新import新的androidx.**包;

    public abstract class BaseActivity extends AppCompatActivity {}

    导入的包名是:

    import androidx.appcompat.app.AppCompatActivity;

    参考AndroidX 官方文档,开启传送门

    https://developer.android.google.cn/jetpack/androidx/migrate

  • 相关阅读:
    python的pycharm虚拟环境问题
    Not Found: /graphql"GET /graphql HTTP/1.1" (Django)
    学习笔记257—word 第二行行首无法使用空格,是为什么?
    学习笔记256—蛋白质的合成过程及mRNA tRNA与rRNA区别
    学习笔记255—横向课题与纵向课题的区别是什么?
    1
    111
    11
    Gradle连载4依赖包打包方式 心悦君兮君不知
    [bug] MySQL错误“ERROR 2006 (HY000):MySQL server has gone away”
  • 原文地址:https://www.cnblogs.com/zhangqie/p/11321917.html
Copyright © 2020-2023  润新知