首页 > 解决方案 > 为什么 support-v4 依赖项未在我的自定义库中添加运行时

问题描述

我要制作自定义库。制作aar后,在另一个中导入,找不到支持依赖。

我的图书馆 Gradle 是:

dependencies {
     implementation fileTree(include: ['*.jar'], exclude: ['classes2.jar'], dir: 'libs')
     compileOnly files('libs/classes2.jar')

     implementation 'com.android.support:support-v4:27.1.1'
     implementation 'com.android.support:multidex:1.0.1'
}

标签: androidandroid-gradle-pluginandroid-library

解决方案


问题是因为您正在使用implementation

当您的模块配置实现依赖项时,它让 Gradle 知道该模块不想在编译时将依赖项泄漏给其他模块。也就是说,依赖项仅在运行时对其他模块可用。

模块依赖的自定义库不会看到支持库。所以,你需要使用api

当一个模块包含一个 api 依赖项时,它让 Gradle 知道该模块想要将该依赖项传递到其他模块,以便它们在运行时和编译时都可以使用它。此配置的行为就像 compile(现在已弃用),您通常应该只在库模块中使用它。

将您的依赖项块更改为如下内容:

dependencies {
     implementation fileTree(include: ['*.jar'], exclude: ['classes2.jar'], dir: 'libs')
     compileOnly files('libs/classes2.jar')

     api 'com.android.support:support-v4:27.1.1'
     api 'com.android.support:multidex:1.0.1'
}

推荐阅读