如何在导出aar文件时包含依赖库

时间:2015-09-01 08:27:57

标签: android gradle android-gradle aar

我正在实现一个具有推送通知和api的库项目。在这个库中,我使用了一些外部库,如playservice-gcm,gson,retrofit等。如果我用库项目编译示例,一切都可以。但是当我使用aar文件时,示例项目无法找到我使用的外部库。

下面是我的图书馆的builde.gradle:

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'
}

以下是示例

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 
}

当我使用aar文件运行示例应用程序时,它会因以下异常而崩溃

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)

谢谢,

2 个答案:

答案 0 :(得分:4)

在Application项目的app/build.gradle

中启用对aar依赖项的传递依赖性

而不是编译

'com.example:myLibrary:versionX'

制作

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

请参阅此answer

答案 1 :(得分:3)

据我所知,aar文件不包含依赖项,然后你必须在主项目中添加这些依赖项。

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