Program type already present: androidx.versionedparcelable.NonParcelField

I was annoyed, so I recorded the problem specifically.

Created a new test project, nothing happened but glide 4.11.0 was introduced.

implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

First, an error was reported, and it was prompted to add something. Then follow the prompts and add it in mf.

tools:replace="android:appComponentFactory"
android:appComponentFactory="任意字符串都行"

 After adding it, we come to the error in the title, see the picture

Program type already present: androidx.versionedparcelable.NonParcelField

You can see from the prompt that there is a file conflict. There should be some package conflict, and then search for the conflicting file.

One is the androidx package and the other is the android.support package. But I did not introduce the androidx package, so it may be because Glide was introduced, so the androidx package was introduced. That's going to be difficult because I have to use Glide. After searching online, I found that adding these two lines of code to the gradle.properties file can solve this problem.

android.useAndroidX=true
android.enableJetifier=true

 Then I found that import android.support.v7.app.AppCompatActivity; cannot be used, so I imported it directly.

import androidx.appcompat.app.AppCompatActivity;

 xml uses the constraint layout of com.android.support:appcompat-v7 by default, and an error is reported. Change it to

androidx.constraintlayout.widget.ConstraintLayout。
Finally, in gradle, you need to replace the dependency of androidx's constraint layout. (PS: Otherwise, the error 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 will be reported. #2: Error inflating class androidx.constraintlayout.ConstraintLayout)

Then it will run. 
总结一下:
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"

Guess you like

Origin blog.csdn.net/health7788/article/details/123012077