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

how to include dependence library when export aar file

我正在实施一个具有推送通知和 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.access0(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)

谢谢,

据我所知,aar 文件不包含依赖项,因此您必须在主项目中也添加这些依赖项。

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

在 Application 项目的 app/build.gradle:

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

而不是编译

'com.example:myLibrary:versionX',

成功

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

看到这个answer