本文介绍了导出aar文件时如何包含依赖库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  apply插件:'com.android.library'

android {
compileSdkVersion 22
buildToolsVersion22.0.1

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

依赖项{
编译fileTree(dir :'libs',include:['* .jar'])
compile('com.android.support:appcompat-v7:22.2.0')
compile'c​​om.squareup.okhttp:okhttp -urlconnection:2.0.0'
compile'c​​om.squareup.okhttp:okhttp:2.0.0'
compile'c​​om.jakewharton:butterknife:5.0.0'
compile'c​​om.squareup .retrofit:改造:1.7.1'
编译'de.greenrobot:eventbus:2.2.1'
编译'com.google.android.gms:play-services-gcm:7.8.0'
编译文件('libs / commons-io-2.4.jar')
compile'c​​om.j256.ormlite:ormlite-android:4.48'
compile'c​​om.j256.ormlite:ormlite-core:4.48'
}

及以下为例

<$ p $ (':':'libs',include:['* .jar'])
//编译项目(':library')=>这是有源代码和工作正常的库
编译'com.android.support:appcompat-v7:22.2.0'
编译'de.greenrobot:eventbus:2.2.1'
编译'com.j256.ormlite:ormlite-android:4.48'
compile'c​​om.j256.ormlite:ormlite-core:4.48'
compile project(':library-debug')=>这个模块只有在我从aar文件创建新模块时才会生成,并且它不能工作
}

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

 由:java.lang引起。 ClassNotFoundException:未在路径中找到类com.google.android.gms.common.GooglePlayServicesUtil:DexPathList [[zip文件/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(Ex在android.app.Activity.performCreate(Activity.java:6093)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
在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.example:myLibrary:versionX'



$ b $ p $ myLibrary:versionX'){transitive = true}



参见


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,

解决方案

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

这篇关于导出aar文件时如何包含依赖库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 17:58