假设我有一个库模块,其中包含一些第三方库,如OkHttp。当我在我的应用程序中包含此库时,我无法使用这些第三方库。我阅读了以下文章Article 1,
Article 2
并尝试了
1. compile project(':library-name')
{将.aar文件(库)作为模块导入myproject}后
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
这是我库中的依赖项,但我无法使用okhttp
和colorpicker
答案 0 :(得分:3)
aar 文件不包含嵌套(或transitive)依赖项,并且没有pom文件描述库使用的依赖项。
这意味着,如果您使用flatDir
repo 导入aar文件,则还必须在项目中指定依赖项。
没有意义:
compile (name:'library-name', ext:'aar'){
transitive=true
}
因为aar没有描述依赖关系的pom文件,所以gradle无法添加任何依赖项,因为它无法知道它们。