AndroidStudio3 Androidx库和Android.support.v4库冲突Duplicate class androidx.annotation.AnimRes found解决办法

Duplicate class androidx.annotation.AnimRes found in modules annotation-1.0.0.jar (androidx.annotation:annotation:1.0.0) and jetified-android-support-v4.jar (android-support-v4.jar)
Duplicate class androidx.annotation.AnimatorRes found in modules annotation-1.0.0.jar (androidx.annotation:annotation:1.0.0) and jetified-android-support-v4.jar (android-support-v4.jar)
Duplicate class androidx.annotation.AnyRes found in modules annotation-1.0.0.jar (androidx.annotation:annotation:1.0.0) and jetified-android-support-v4.jar (android-support-v4.jar)…
看到上面的这些错误,让我好生痛苦,它的变体还有很多,什么media包的,反正都是在android-support-v4.jar里面的包,因为某些原因,我需要在AndroidStudio3.5上面运行程序,编写代码,然后关于这种包的冲突错误一堆一堆的出现,搞得我都怀疑Android开发怎么那么难了。
一顿搜索之后,发现很多网友都遇到过,但是,他们很多都没有解决,解决了的也很幸运。这里我给出自己的解决办法吧。
我们的思路就是,遇到库冲突,无非就是想只是用Androidx的库,而不要使用android-support-v4.jar库,所以目标就是找怎么排除这个库。
方向对了,于是乎,就有了在build.gradle里面加入排除语句:(经过搜寻,下面的语句管用,其实我也不知道到底用的什么版本号,这么多版本,真心不容易,找到了一个合适的,期间,我还尝试使用命令行,但是结果gradlew不管用)
compile (‘com.android.support:support-v4:22.2.1’){
exclude module: ‘support-annotations’
}
如下所示:build.gradle文件里,dependencies里面加入即可,不过,你会发现你需要加入所有的库排除语句,因为android-support-v4.jar里可不止一个annotations包。喜欢折腾的童鞋慢慢折腾吧。

dependencies {
    
    
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //parse html - jsoup
    implementation 'org.jsoup:jsoup:1.13.1'
    //parse json - fastjson
    implementation 'com.alibaba:fastjson:1.2.68'
//    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
   implementation ( 'com.android.support:support-v4:22.2.1'){
    
    
       exclude module: 'support-annotations'
   }

既然这么麻烦,那自然还有更简单的办法呗。其实本来就是有的,但是我搜了一圈,没人告诉我,那就是我们可以直接删除android-support-v4.jar库,请看下图:右键点击,Delete掉它吧。好了,就这样,我们完事了。下回再见,还有gradlew命令行的问题,gradle view的使用问题哦。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/poolooloo/article/details/105865825