Android库似乎没有包含它的依赖项(迫使我在app模块中添加这些依赖项)

时间:2017-03-28 21:06:41

标签: android gradle android-gradle

我正在开发一个Android库和一个使用它的演示应用。到目前为止,我通过源代码编译了应用程序和lib,现在我已经构建了lib并希望使用AAR文件而不是lib的源代码 - 我收到有关缺少类的错误运行时。

看起来该库的依赖项未包含在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时 - 错误没有发生。

图书馆很简单。我们正在使用flavor并且multiDexEnabled设置为true。

为什么在使用AAR文件构建时找不到lib的依赖项?任何补救措施?

谢谢!

2 个答案:

答案 0 :(得分:2)

aar文件不包含传递依赖项。

另一种方法是在 maven存储库(公共或私有)中发布库。在这种情况下,使用pom文件的Gradle也可以下载依赖项。

答案 1 :(得分:2)

要创建一个独立的jar(胖罐),您可以研究jarjar(https://github.com/shevek/jarjar)之类的解决方案。