Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to include dependence library when export aar file

I'm implementing a library project which have push notification and api. In this library I use some external lib such as playservice-gcm, gson, retrofit, etc. Everything is ok if I compile the example with the library project. But when I using aar file, the example project can't not find the the external libs I used.

Below is my builde.gradle of my library:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ('com.android.support:appcompat-v7:22.2.0')
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.jakewharton:butterknife:5.0.0'
    compile 'com.squareup.retrofit:retrofit:1.7.1'
    compile 'de.greenrobot:eventbus:2.2.1'
    compile 'com.google.android.gms:play-services-gcm:7.8.0'
    compile files('libs/commons-io-2.4.jar')
    compile 'com.j256.ormlite:ormlite-android:4.48'
    compile 'com.j256.ormlite:ormlite-core:4.48'
}

and below is the example

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//    compile project(':library') => this is the the libs having source code and work normal
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'de.greenrobot:eventbus:2.2.1'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile project(':library-debug') => this's the module have only aar generate when I create new module from aar file and it can't work 
}

When I run example app with aar file, it crash with below exception

caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.GooglePlayServicesUtil" on path: DexPathList[[zip file "/data/app/com.media.example-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.media.workvoice.libray.checkPlayServices(WorkVoice.java:158)
            at com.media2359.workvoice.WorkVoice.register(WorkVoice.java:75)
            at com.media2359.example.ExampleActiviy.onCreate(ExampleActiviy.java:46)
            at android.app.Activity.performCreate(Activity.java:6093)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2434)
            at android.app.ActivityThread.access$800(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5424)
            at java.lang.reflect.Method.invoke(Native Method)

Thanks,

like image 835
Phu Tang Avatar asked Sep 01 '15 08:09

Phu Tang


2 Answers

Enable transitive dependencies on the aar dependency in the Application project's app/build.gradle:

Instead of compile

'com.example:myLibrary:versionX',

make it

compile('com.example:myLibrary:versionX'){transitive=true}

see this answer

like image 106
Ernest Avatar answered Oct 16 '22 03:10

Ernest


As I know, the aar file doesn't contain the dependencies, then you have to add these dependencies also in the main project.

An alternative is to publish the aar in a maven repository. Gradle in this case with the pom file is able to download also the dependencies.

like image 37
Gabriele Mariotti Avatar answered Oct 16 '22 04:10

Gabriele Mariotti