当前位置: 移动技术网 > IT编程>移动开发>Android > Android Studio项目适配AndroidX(Android 9.0)的方法步骤

Android Studio项目适配AndroidX(Android 9.0)的方法步骤

2020年03月09日  | 移动技术网IT编程  | 我要评论

佛山 招聘,神鬼传奇大西洋探宝,鸦爪祭司的秘密

说在前面:

老项目、大项目适配android x 注意了,一定要谨慎、谨慎、再谨慎。项目中用到的第三方库多的话会很麻烦,有些第三方库还没有适配android x。

适配android x的两种情况:一种是老项目适配android x ,另外一种是新项目要求适配android 9.0

硬核要求

android studio 版本升级到3.2.0 以上的版本,
distributionurl的版本升级为 4.10.1以上
targetsdkversion 28
gradle 插件的版本升级为 3.2.0以上

classpath 'com.android.tools.build:gradle:3.2.0'

这里以as 3.4为例

在这里插入图片描述

第一步:

新项目,这步可以跳过。

compilesdkversion 28
  defaultconfig {
    applicationid "gangqing.pu.xmxidaq"
    minsdkversion 19
    targetsdkversion 28
    versioncode 1
    versionname "1.0"
    testinstrumentationrunner "androidx.test.runner.androidjunitrunner"
  }

第二步:

// top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
  repositories {
    google()
    jcenter()
    
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.4.0'
    classpath 'com.jakewharton:butterknife-gradle-plugin:10.1.0'
    // note: do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}

allprojects {
  repositories {
    google()
    jcenter()
    maven { url "https://jitpack.io" }
  }
}

task clean(type: delete) {
  delete rootproject.builddir
}

第三步:

在gradle.properties 中加入如下代码,表示支持android x

android.useandroidx=true
android.enablejetifier=true

在这里插入图片描述

第四步:

android studio ----> refactor----> migrate to androidx,,一键转为 androidx

在这里插入图片描述

第五步:

支持java 1.8

compileoptions {
    sourcecompatibility javaversion.version_1_8
    targetcompatibility javaversion.version_1_8
  }

第六步:

修改布局,比如:constraintlayout

变化之前 androidx
<android.support.constraint.constraintlayout <androidx.constraintlayout.widget.constraintlayout

注意:如果是老项目适配androidx,没有找到constraintlayout 的话,那么还需要在在这里插入图片描述中添加如下代码

dependencies {
   implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}

第四步之后,
<android.support.constraint.constraintlayout
变为<androidx.constraintlayout.constraintlayout 这时你需要修改为
<androidx.constraintlayout.widget.constraintlayout |

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网