What is AndroidX

In view of the confusion of Android support package version dependence, the actual development is not very friendly, often appear to rely on the package duplication or the project itself and third-party library support package version is inconsistent, etc., Google 2018 IO conference launched a new Android extension library AndroidX, Used to replace the original Android extension. After the replacement, only package names and Maven artifact names will be affected. The original class names, method names and field names will not be changed. The original functionality will not be changed, and some new features will be supported that are not in the original Support library.

Second, why do we need to do migration

Yingcai APP planned to access the Flutter Kit tool library developed by the Flutter kit team. However, an error occurred after the Flutter Kit tool library was recompiled and run. The error message is as follows: By viewing the detailed error log, it is found that the same attribute exists in the AndroidManifest of the merged dependent component, but different attribute values are caused. According to the log tip add inside the tag: tools: after the replace = “android: appComponentFactory” recompile operation is still an error and the error information is as follows:

By looking at the detailed build logs, The value of this attribute is <Application in the Androidman file in support-compat-28.0.0.aar android:appComponentFactory=”android.support.v4.app.CoreComponentFactory” >; In the core – 1.1.0. Aar Androidman file in this attribute value of < Application android: appComponentFactory = “androidx. Core. App. CoreComponentFactory” >. Check out the Zcm_Flutter_kit SDK plugin and see that AndroidX dependencies have been introduced into the Android support code.

Since AndroidX libraries and Android Support libraries cannot coexist, in order to introduce ZCM flutter Kit to improve the efficiency of flutter hybrid development, and more and more triparty libraries may Support AndroidX in the future, Therefore, it is necessary to change the support dependency of the Hero project to AndroidX dependency.

Three, how to migrate

1. Environment preparation

AndroidX migration requirements are performed in AndroidStudio 3.2 or later, targetSdkVersion 28 is required (Android9.0), The classpath ‘com. Android. Tools. Build: gradle: 3.2.0’ 3.2.0 or higher.

2. Modify gradle.properties

Modify the gradle.properties file in the root directory of the project, UseAndroidX =true // Indicates that androidx is enabled. EnableJetifier =true // indicates that dependent packages are migrated to AndroidX. If the value is false, the dependency packages will not be migrated to AndroidX, but problems may occur when using the contents of the dependency packages. If the project does not use any third-party dependencies, you can set it to false.

3. Start one-click migration

Implement Refactor->Migrate to AndroidX in AS

After migration, perform Flie -> Invalidate Caches /Restart to ensure that the cache files in dependencies are updated.

4. Modify the project code

The one-click migration plug-in tool is provided in AS, but the replacement effect is not good. Therefore, after the one-click migration is complete, you need to manually modify what went wrong in the project.

1. Library dependency

Since the talent project is a componentialized development structure, when importing dependencies, a config.gradle file is created in the root directory and the dependency library address is placed separately in config.gradle. The advantage of this method is that when each module needs to reference the related library, it directly selects the name of the corresponding library in config.gradle, thus avoiding the problem of inconsistent dependency library versions in each module.

However, AndroidX one-click migration does not recognize this dependency, so the automatic modification is not done. You need to manually rely on support as follows

"support-v4"         : "com.android.support:support-v4:${dependVersion.support}"."appcompat-v7"        : "com.android.support:appcompat-v7:${dependVersion.support}"."design"              : "com.android.support:design:${dependVersion.support}"."recyclerview"        : "com.android.support:recyclerview-v7:${dependVersion.support}"."cardview"            : "com.android.support:cardview-v7:${dependVersion.support}"."annotations"         : "com.android.support:support-annotations:${dependVersion.support}"."constraint-layout"   : "Com. Android. Support. The constraint, the constraint - layout: 1.0.2".Copy the code

Modified to

"support-v4"          : "Androidx. Legacy: legacy - support - v4:1.0.0"."appcompat-v7"        : "Androidx. Appcompat: appcompat: 1.0.2"."design"              : "Com. Google. Android. Material: material: 1.1.0 - alpha09"."recyclerview"        : "Androidx. Recyclerview: recyclerview: 1.0.0."."cardview"            : "Androidx. Cardview: cardview: 1.0.0."."annotations"         : "Androidx. Annotation: the annotation: 1.0.0."."constraint-layout"   : "Androidx. Constraintlayout: constraintlayout: 1.1.3.".Copy the code

2. Modify Java files such as Activity/Fragment and XML layout files

Activity/Fragment/XML (including classes that use the support package, etc.). After Migrate, the classes in the support package do not correspond exactly. There will be many errors, so you need to change the classes in androidX. If you delete the original references one by one and then import the correct reference package again, the workload will be very heavy. Therefore, a global replacement can be used: Edit ->Find -> Replace in path.

Common corresponding relationships are sorted out as follows:

android.support.annotation.Nullable androidx.annotation.Nullable android.support.annotation.NonNull androidx.annotation.NonNull; androidx.appcompat.widget.GridLayoutManager androidx.recyclerview.widget.GridLayoutManager androidx.appcompat.widget.RecyclerView androidx.recyclerview.widget.RecyclerView; androidx.appcompat.widget.LinearLayoutManager androidx.recyclerview.widget.LinearLayoutManager androidx.appcompat.widget.LinearSmoothScroller androidx.recyclerview.widget.LinearSmoothScroller androidx.appcompat.widget.OrientationHelper androidx.recyclerview.widget.OrientationHelper androidx.appcompat.widget.SnapHelper androidx.recyclerview.widget.SnapHelper androidx.core.app.FragmentTransaction androidx.fragment.app.FragmentTransaction androidx.core.app.Fragment; androidx.fragment.app.Fragment; androidx.core.app.FragmentManager androidx.fragment.app.FragmentManager; androidx.core.app.FragmentStatePagerAdapter androidx.fragment.app.FragmentStatePagerAdapter androidx.core.view.ViewPager  androidx.viewpager.widget.ViewPager androidx.core.app.FragmentActivity androidx.fragment.app.FragmentActivity android.support.annotation.ColorInt androidx.annotation.ColorInt android.support.annotation.ColorRes androidx.annotation.ColorRes android.support.annotation.FloatRange androidx.annotation.FloatRange android.support.annotation.AttrRes androidx.annotation.AttrRes android.support.design.widget.AppBarLayout com.google.android.material.appbar.AppBarLayout android.support.design.widget.CoordinatorLayout androidx.coordinatorlayout.widget.CoordinatorLayoutCopy the code

In the layout XML file, it will not be replaced automatically, you need to replace it manually

<androidx.constraintlayout.ConstraintLayout/>             <androidx.constraintlayout.widget.ConstraintLayout/>
<android.support.design.widget.CollapsingToolbarLayout/>  <com.google.android.material.appbar.CollapsingToolbarLayout/>
<android.support.v7.widget.Toolbar/>                      <androidx.appcompat.widget.Toolbar/>
<android.support.v4.view.ViewPager/>                      <androidx.viewpager.widget.ViewPager/>
<android.support.v7.widget.RecyclerView/>                 <androidx.recyclerview.widget.RecyclerView/>Copy the code

3. Check for gaps

1) There may be a few omissions or errors in the global replacement, you can Find in Path, use the support keyword global search, and then replace

2) After the replacement, Rebuild. If an error occurs, the compilation fails. After several compilation attempts, the final compilation is successful, indicating that the project code modification is complete.

5. Other issues

1. Annotation processor conflicts

The butterknife used in the project does not support androidx, causing errors to be reported in files that reference butterknife. Version 10.1.0 has been made compatible so will be in the project

"butterknife"         : "Com. Jakewharton: butterknife: 8.8.1."."butterknife-compiler": "Com. Jakewharton: butterknife - compiler: 8.8.1".Copy the code

Upgrade to 10.1.0

"butterknife"         : "Com. Jakewharton: butterknife: 10.1.0."."butterknife-compiler": "Com. Jakewharton: butterknife - compiler: 10.1.0".Copy the code

2, Android dependency ‘Androidx. core:core’ has different version for the compile (1.0.0) and Runtime (1.0.1) Classpath.You should manually set the same version via DependencyResolution compilation error

This problem is caused by the inconsistency between the imported dependency library version and the build environment version. You can solve this problem by adding configuration to the build.gradle file in the project root directory, as shown below

Subprojects {project. Configurations. All {/ / AndroidX version different solutions resolutionStrategy eachDependency {details - >if (details.requested.group == 'androidx.core') {
                details.useVersion '1.1.0'// Set the version number to the version you want}}}}Copy the code

6. Confuse the configuration

The confusion file before modification is the confusion configuration for the support library. After the migration, you need to add the confusion configuration for AndroidX

-keep class com.google.android.material.** {*; } -keep class androidx.** {*; } -keep public class * extends androidx.** -keep interface androidx.** {*; } -dontwarn com.google.android.material.** -dontnote com.google.android.material.** -dontwarn androidx.**Copy the code

Reference Documents:

Developer. The android. Google. Cn/jetpack/and…

Developer. The android. Google. Cn/jetpack/and…

Developer. The android. Google. Cn/jetpack/and…