我有 Android Studio 库项目,它有自己的 Gradle 依赖项:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}
问题出在这两个外部库(retrofit 和 converter-gson)上。 该库项目的结果是 .aar 文件。但是当我在单独的项目中使用这个 .aar 文件时,我收到 Unresolved 类错误:
java.lang.NoClassDefFoundError: Failed resolution of: Lretrofit/Retrofit$Builder;
有没有办法将我的库项目中的那些 gradle 依赖项包含到最终的 .aar 文件中?
我试过this answer但它对我不起作用。
编辑: 我的整个 build.gradle 文件来自库项目。
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}
编辑 2: 这是我的应用程序项目中 build.gradle 脚本的一部分,它使用之前作为 .aar 文件生成的库。
3个版本,但都不起作用(都没有从库中自动下载依赖项)
(注意,我将此 .aar 文件添加到我的应用程序项目的 lib 文件夹中,并将 flatDir 指令添加到它的 gradle.build 中)。我没有执行 File->New->NewModule->ImportExistingJAR/AAR
repositories {
flatDir {
dirs 'libs'
}
}
1.
compile(name:'mylibrary-release', ext:'aar')
2.
compile(name:'mylibrary-release', ext:'aar') {
transitive = true
}
3.
compile(':mylibrary-release@aar') {
transitive = true
}
最佳答案
您的 AAR 库不是问题。库不应包含其依赖项,因为这可能会进一步导致版本冲突。
您的应用项目未传递加载库依赖项的原因是 flatdir
。以下是文档中的相关部分 ( https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver ):
This type of repository does not support any meta-data formats like Ivy XML or Maven POM files. Instead, Gradle will dynamically generate a module descriptor (without any dependency information) based on the presence of artifacts.
正确的方法是将您的库托管在公共(public)或私有(private) Maven 存储库中。
关于java - 如何将 .jar 依赖项包含到最终的 .aar 库文件中? (安卓工作室1.5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34157525/