> 文档中心 > 同时使用AndroidX库和bufferknife库报 package android.support.annotation does not exist 错误

同时使用AndroidX库和bufferknife库报 package android.support.annotation does not exist 错误

    Android项目同时使用AndroidX库和bufferknife库报错: package android.support.annotation does not exist 

    原因:谷歌使用AndroidX扩展库代替老的Android支持库,androidx库中annotation.jar包的包名由android.support.annotation变为androidx.annotation,而bufferknife8.8.1之前一直调用的是android.support.annotation支持库,而使用androidx代替android.support后,导致annotation.jar里的一些类找不到,比如:

    解决办法:应调用bufferknife:9.0.0-rc1以上版本,在Module  build.gradle里修改:

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

在Project  build.gradle里修改:

 dependencies { classpath 'com.android.tools.build:gradle:3.3.0' classpath 'com.jakewharton:butterknife-gradle-plugin:10.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files    }

踩坑:编译报错:Manifest merger failed with multiple errors, see logs

Element uses-permission#android.permission.READ_PHONE_STATE at AndroidManifest.xml:30:5-95 duplicated with element declared at AndroidManifest.xml:15:5-75
D:\Projects\R1911\mower\app\src\main\AndroidManifest.xml:22:18-91 Error:
    Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
    is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
    Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:51:5-288:19 to override.

Execution failed for task ':app:checkDebugDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.0.0-runtime (androidx.core:core:1.0.0) and support-compat-28.0.0-runtime

(com.android.support:support-compat:28.0.0)
 

报错原因:此问题也是AndroidX和android.support间与bufferknife的版本不对应导致,如butterknife:10.0.0和android.support同时使用就会报此错误。

解决方案

法一:需要将android.support升级至androidx,同时确保Butterknife版本大于8.8.1;

法二:将butterknife版本降至8.8.1以下,将androidx相关的库改为android.support相应的库;

注:android.support和androidx库对应关系,

官方地址:工件映射  |  Android 开发者  |  Android Developers

Support库与AndroidX对应关系_乐活青年的博客-CSDN博客_androidx和support区别

  可以参考GitHub上的解决方案:https://github.com/JakeWharton/butterknife/issues/1296  

   如有不足欢迎指正。于2018年11月9日

更新211220