有被气到,所以特地记录一下问题
新建了一个测试的项目,什么都没有就是引入了glide 4.11.0
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
先报了一个错误,提示让加东西,那就按照提示在mf中加上吧
tools:replace="android:appComponentFactory"
android:appComponentFactory="任意字符串都行"
加上之后就来到标题的错误了,看图
Program type already present: androidx.versionedparcelable.NonParcelField
从提示可以看到是有是有文件冲突了,应该是有什么包冲突了,然后查找一下冲突文件。
一个是androidx的包,一个是android.support包。但是我并没有引入androidx的包,那么有可能是因为引入了Glide,所以引入了androidx的包。那就很难搞了,因为我要用Glide。上网搜索之后发现了在gradle.properties文件中加入这两行代码可以解决这问题。
android.useAndroidX=true
android.enableJetifier=true
然后发现import android.support.v7.app.AppCompatActivity;用不了了,直接导入
import androidx.appcompat.app.AppCompatActivity;
xml默认用了com.android.support:appcompat-v7的约束布局,也报错,改成
androidx.constraintlayout.widget.ConstraintLayout。 最后在gradle中要换成androidx的约束布局的依赖。(PS:否则会报错java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rxjavademo/com.example.rxjavademo.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.ConstraintLayout) 然后就能运行了。
总结一下:
1.在AndroidManifest.xml文件的application中加两行代码
tools:replace="android:appComponentFactory"
android:appComponentFactory="任意字符串都行"
2.在gradle.properties文件加入两行代码,
加了之后会使用androidx的包,android.support的包就不会用了,
这里比较坑的是用了com.android.support:appcompat-v7的东西可以编译,打包,
但是会奔溃。。所以要检查用了com.android.support:appcompat-v7包的地方,
如果有报和这包有关的错误,可以试试改成androidx的包。
android.useAndroidX=true
android.enableJetifier=true
3.最后一点,如果报错为java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.rxjavademo/com.example.rxjavademo.MainActivity}:
android.view.InflateException:
Binary XML file line #2:
Binary XML file line #2:
Error inflating class androidx.constraintlayout.ConstraintLayout
要将使用到的约束布局改为androidx.constraintlayout.widget.ConstraintLayout
然后有一点很容易忽略,在gradle的依赖中要引入androidx的约束布局包
implementation "androidx.constraintlayout:constraintlayout:1.1.3"