How to adapt Android studio project to Android x (Android 9.0)

Time:2021-2-22

Say ahead:

Attention should be paid to the adaptation of old projects and big projects to Android X. we must be cautious, cautious and more cautious. If there are many third-party libraries used in the project, it will be very troublesome. Some third-party libraries have not been adapted to Android X.

There are two cases of adapting to Android X: one is that the old project adapts to Android x, the other is that the new project requires adapting to Android 9.0

Hard core requirements

Upgrade Android studio to version 3.2.0 or above,
The version of distributionurl is upgraded to 4.10.1 or above
targetSdkVersion 28
Upgrade the version of gradle plug-in to 3.2.0 or above


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

Take as 3.4 as an example

在这里插入图片描述

Step one:

New project, this step can be skipped.


compileSdkVersion 28
  defaultConfig {
    applicationId "gangqing.pu.xmxidaq"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

Step 2:


// 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
}

Step 3:

stay gradle.properties Add the following code to support Android X


android.useAndroidX=true
android.enableJetifier=true

在这里插入图片描述

Step 4:

Android studio — > refactor — > migrate to Android

在这里插入图片描述

Step 5:

Support Java 1.8


compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

Step 6:

Modify the layout, such as: constraintlayout

Before the change AndroidX
<android.support.constraint.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout

Note: if the old project adapts to Android X and the constraintlayout is not found, you need to在这里插入图片描述Add the following code to the


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

After the fourth step,
<android.support.constraint.ConstraintLayout
Become< androidx.constraintlayout.ConstraintLayout You need to change it to
<androidx.constraintlayout.widget.ConstraintLayout |

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.