AndroidX 适配笔记

一个做iOS开发的为什么要做Android适配?

Android兄弟离职一段时间了,公司的三个项目还未对Android新版本进行适配,在AndroidQ上运行会闪退。同属我司移动端,这个任务自然就交给我先处理了。

适配笔记

修改gradle版本为3.4.1或更高版本
在项目的build.gradle/allprojects/repositories中添加

maven { url 'https://maven.google.com'}

build.gradle的compileSdkVersion、targetSdkVersion、buildToolsVersion的配置,都设置为29

当前项目的 gradle.properties添加

android.useAndroidX=true
android.enableJetifier=true

遇到错误及解决方法

Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=appchinaDebug, filters=[], versionCode=1, versionName=1.0.0}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = “xxx-${defaultConfig.versionName}-${variant.productFlavors[0].name}.apk”
}

ERROR: Cannot choose between the following variants of project :viewlib:

1.在project的build.gradle中删除
classpath ‘com.neenbedankt.gradle.plugins:android-apt:x.x’
2.在module的build.gradle中删除
apply plugin: ‘android-apt’
3.将module的build.gradle文件中的dependency
apt ‘com.jakewharton:butterknife-compiler:x.x.x’改为
annotationProcessor ‘com.jakewharton:butterknife-compiler:x.x.x’ }

ERROR: All flavors must now belong to a named flavor dimension

在versionName后边加上flavorDimensions “versionCode”

Could not get unknown property ‘bootClasspath’ for object of type

去掉apply plugin: ‘me.tatarka.retrolambda’和classpath ‘me.tatarka:gradle-retrolambda:x.x.x’

Error inflating class me.imid.swipebacklayout.lib.SwipeBackLayout

升级swipebacklayout版本

程序包android.support.annotation不存在

implementation ‘androidx.annotation:annotation:1.1.0’
在代码中将相关引用改为
import androidx.annotation.xxx;

SpringAnimation动画

implementation ‘androidx.dynamicanimation:dynamicanimation:1.0.0’
import androidx.dynamicanimation.animation.SpringAnimation;

The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.

dependencies {
implementation ‘com.jakewharton:butterknife:10.0.0’
annotationProcessor ‘com.jakewharton:butterknife-compiler:10.0.0’
}

ContactFragment无法转换为Fragment

将代码中getSupportFragmentManager()方法改为android.app.Fragment对应的getFragmentManager()

Didn’t find class “androidx.constraintlayout.ConstraintLayout” on path

implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’
用androidx.constraintlayout.widget.ConstraintLayout替换androidx.constraintlayout.ConstraintLayout

Error inflating class android.support.v7.widget.CardView

将android.support.v7.widget.CardView替换成androidx.cardview.widget.CardView

使用swipebacklayout在AndroidQ中有残影

AndroidX自带滑动返回了

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
setSwipeBackEnable(false);
}

glide加载网络图片,在9.0以上机器不显示图片

在AndroidManifest.xml文件里, application字段里 加入 android:usesCleartextTraffic="true"

你可能感兴趣的:(AndroidX 适配笔记)