【问题标题】:Adding gradle dependency to library aar package向库 aar 包添加 gradle 依赖项
【发布时间】:2015-12-25 17:53:56
【问题描述】:

我正在为供应商制作一个库项目,它需要 Android Volley 作为依赖项。我已使用此Create aar file in Android Studio 创建.aar 文件并将其包含在我正在使用此Adding local .aar files to Gradle build using "flatDirs" is not working 的测试项目中。该库链接良好,项目编译没有错误。但是在运行时测试应用程序崩溃说它找不到 Volley

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/android/volley/toolbox/Volley;
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.volley.toolbox.Volley" on path: DexPathList[[zip file "/data/app/in.gridsync.acttest-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我的库项目的 Gradle 文件是这样的

    apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

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

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.mcxiaoke.volley:library:1.0.19'
}

在测试项目中,gradle就是这个

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "in.gridsync.acttest"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile project(':openapp-library')
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
}

我假设,当创建 .aar 包时,它会使用并包含它对 Volley 库的依赖。有人可以解释一下吗?

【问题讨论】:

  • 伙伴,为什么不直接从 Project Structaure 的 depeeneices 选项卡中添加 Volley?
  • 添加 testCompile 'com.mcxiaoke.volley:library:1.0.19'
  • @ItzikSamara,我不希望我的供应商添加任何东西,除了 aar 包。这是有道理的吧?因为如果我将来使用其他库,我不希望他更改他的应用程序代码,而不仅仅是替换我提供的新 .aar 包
  • 你是怎么解决的,我也遇到了同样的问题!
  • 你解决了吗?

标签: android android-gradle-plugin aar


【解决方案1】:

据我所知,您必须为 .aar 库添加“@aar”。 jar文件也不是必需的,它会自行下载。 试试这个:

compile 'com.mcxiaoke.volley:library:1.0.19@aar'

【讨论】:

  • 我刚才试了他的,这个不行。 .aar 本身并不包含 volley 库。
【解决方案2】:

另一种解决方案是将依赖库作为静态 jar 文件放置在项目的“libs”文件夹中,而不是使用 gradle 依赖项。生成 aar 文件时,libs 文件夹下的 jar 文件将包含在 aar 中。

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 1970-01-01
    • 2016-07-28
    • 2021-01-07
    • 1970-01-01
    • 2015-11-06
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多