简体   繁体   中英

Android library seems not to include it's dependencies (forcing me to add those dependencies in the app module)

I'm developing an Android library and a demo app that uses it. up until now I compiled both the app and the lib via source code and now that Iv'e built the lib and want to use the AAR file instead of the lib's source code - I get error about missing classes during runtime.

It looks like the dependencies of the library are not included in the AAR file. Exception description:

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) ...

Here is the application's 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'

}

In this example it happened with Log4J. When I added the dependency of Log4J from the library's build.gradle to the application's build.gradle - the error did not happen.

The library is a simple one. We are using flavors and multiDexEnabled is set to true.

Any ideas why the lib's dependencies are not found when building with the AAR file? any remedies?

Thanks!

The aar file doens't contain the transitive dependencies.

The alternative is to publish the library in a maven repository (public or private). Gradle in this case with the pom file is able to download also the dependencies.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM