android – 如何在导出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)

谢谢,

解决方法:

在Application项目的app / build.gradle中启用对aar依赖项的传递依赖:

而不是编译

‘com.示例:在MyLibrary:versionX’,

做了

编译( ‘com.示例:在MyLibrary:versionX’){传递=真}

看这answer

上一篇:AAR文件中的类在Android Studio中不可用


下一篇:不再使用Android Studio 3.0在输出文件夹中创建AAR库