android - 在应用程序中找不到传递库依赖项

标签 android android-studio gradle android-gradle-plugin android-library

假设我有一个库模块,其中包含一些第 3 方库,例如 OkHttp。当我在我的应用程序中包含这个库时,我无法使用这些第 3 方库。我阅读了以下文章 Article 1 , Article 2 并尝试过
1. 编译项目(':library-name')
{在将(库的).aar 文件作为模块导入到我的项目之后
2. 我将 .aar 文件包含在 libs 文件夹中并添加了以下依赖项

build.gradle(项目级别)

allprojects {
repositories {
    flatDir {
        dirs 'libs'
    }
}
}

build.gradle(应用层)

compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.myapp.package:library-name:1.0.0@aar'){
transitive=true
}

3。类似于第二个,但在
build.gradle(应用层)

compile fileTree(dir: 'libs', include: ['*.jar'])
compile (name:'library-name', ext:'aar'){
transitive=true
}

但是,我仍然无法使用我的库中存在的传递库。我收到以下异常。

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/MediaType;

谁能帮忙

编辑:
下面是我的库的build.gradle

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'

compile 'org.apache.httpcomponents:httpcore:4.4.4'
compile 'org.apache.httpcomponents:httpclient:4.5.1'

compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'petrov.kristiyan.colorpicker:colorpicker-library:1.0.3'


testCompile 'junit:junit:4.12'

testCompile 'org.mockito:mockito-core:1.10.19'

testCompile 'org.hamcrest:hamcrest-library:1.1'

compile files('notificationlog-0.1.0.jar')

我能够在我的应用程序中使用 notificationlog,它是我库中的依赖项,但我无法使用 okhttpcolorpicker

最佳答案

aar 文件不包含嵌套(或 transitive)依赖项 并且没有描述库所用依赖项的 pom 文件。

这意味着,如果您使用 flatDir 存储库导入 aar 文件,您还必须在项目中指定依赖项

没有意义:

compile (name:'library-name', ext:'aar'){
    transitive=true
}

因为 aar 没有描述依赖关系的 pom 文件,所以 gradle 无法添加任何依赖关系,因为它不知道它们。

关于android - 在应用程序中找不到传递库依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35311507/

相关文章:

java - 尝试在空对象引用上调用虚拟方法 'int com.yinkyade.scheduler.model.User.getId()'

java - 如何制作包含内部版本号的 TeamCity 工件?

gradle - 抑制 Gradle 的 JavaExec 输出

android - 设置适配器为空

android - Android NDK 上的嵌套函数

java - 如何从 android studio 中删除不兼容类型错误

Android Studio - 重新启用标记为不再显示的弹出窗口

java - 任务 ':dex'的执行失败

java - Android 抽屉导航中的后退导航不会更改所选项目

android - 样式化 ChromeCast MediaRoute 按钮