将Gradle依赖项添加到库aar包

5

我正在为一家供应商制作一个图书馆项目,它需要 Android Volley 作为依赖项。 我使用了这个在Android Studio中创建aar文件来创建 .aar 文件,并且将其包含在测试项目中,我使用了这个使用“flatDirs”将本地 .aar 文件添加到 Gradle 构建中不起作用。 图书馆已经链接并且没有错误发生在项目编译中。但是在运行时,测试应用程序崩溃并显示找不到 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库的依赖关系。可以有人请解释一下吗?


添加测试编译 'com.mcxiaoke.volley:library:1.0.19' - Gabriele Mariotti
1
@ItzikSamara,我不希望我的供应商除了aar包之外添加任何东西。这很有道理,对吧?因为如果我将来使用其他库,我不希望他改变他的应用程序代码,只需替换我提供的新**.aar**包即可。 - Chakradhar Reddy Veeramreddy
@ChakradharReddyVeeramreddy,这个问题有更新吗?我也在使用Retrofit时遇到了同样的问题。 - Pararth
@user2450263 找不到解决方法... 不得不指示我们的客户包含所有必要的库。 - Chakradhar Reddy Veeramreddy
@ChakradharReddyVeeramreddy 谢谢您的回复,可能需要这样做。一直没有找到其他的解决方案... - Pararth
显示剩余7条评论
2个回答

1
据我所知,对于.aar库文件,您必须添加"@aar"。而且不需要jar文件,它会自动下载。 尝试这样做:
compile 'com.mcxiaoke.volley:library:1.0.19@aar'

我刚试了一下,这个不起作用。**.aar** 文件本身仍然没有包含 volley 库。 - Chakradhar Reddy Veeramreddy

-1
一个替代方案是将依赖库作为静态 jar 文件放置在项目的 "libs" 文件夹中,而不是使用 gradle 依赖。生成 aar 文件时,libs 文件夹下的 jar 文件将被包含在 aar 中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接