我正在开发一个 Android 库和一个使用它的演示应用程序。到目前为止,我通过源代码编译了应用程序和库,现在我构建了库并希望使用 AAR 文件而不是库的源代码 - 我在运行时收到有关缺少类的错误。
AAR 文件中似乎未包含库的依赖项。 异常描述:
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/log4j/Logger;
....<clinit>(....java:57) at ...onCreate(...java:65) at android.app.Activity.performCreate(Activity.java:5961) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) ...
这是应用程序的 build.gradle :
apply plugin: 'com.android.application'
def APP_VERSION = 1.0
android {
compileSdkVersion 25
buildToolsVersion "25"
defaultConfig {
applicationId "..."
minSdkVersion 14
targetSdkVersion 25
versionCode 2
versionName APP_VERSION + "(" + appVersionCode + ")"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
packagingOptions {
//pickFirst 'META-INF/license.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/*'
exclude 'META-INF/maven'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile(name:'library_filename', ext:'aar')
<HERE IV'E ADDED THE LIB'S DEPENDENCIES IN ORDER FOR THE APP TO WORK>
//Other dependencies needed by the application
compile 'com.google.android.gms:play-services-base:9.4.0'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.android.support:design:25.2.0'
}
在此示例中,Log4J 发生了这种情况。当我将 Log4J 的依赖项从库的 build.gradle 添加到应用程序的 build.gradle 时,错误没有发生。
这个库很简单。我们正在使用flavors并且multiDexEnabled设置为true。
有什么想法为什么在使用 AAR 文件构建时找不到库的依赖项吗?有什么补救措施吗?
谢谢!
最佳答案
aar 文件不包含传递依赖项。
另一种方法是在 maven 存储库(公共(public)或私有(private))中发布库。在这种情况下,Gradle 使用 pom 文件还可以下载依赖项。
关于Android 库似乎不包含它的依赖项(迫使我在应用程序模块中添加这些依赖项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43079950/