希望能够依赖 AAR 来进行 Gradle-Android 项目中的单元测试。我不想在我的项目中手动包含外部库源或二进制文件(出于多种原因)。
我们可以在 build.gradle 中像这样依赖远程 aar 文件:
编译 'com.facebook:facebook-android-sdk:3.6.0:@aar'
但是当使用此处描述的 gradle 测试方法时:https://stackoverflow.com/a/16952507/821636 (因为 Jake Wharton's plugin ,还不完全存在),尝试将 aar 作为该方法的依赖项包含在内,如下所示:
testLocalCompile 'com.facebook:facebook-android-sdk:3.6.0:@aar
实际上似乎没有在类路径中包含 aar 以进行测试(使用 ./gradlew check
运行),因为我得到了 aar 中类的 NoClassDefFoundError
。注意:当以这种方式包含 jar 依赖项时,它们会起作用。
认为 localTest
任务中一定有一些东西需要添加 aar 扩展,因为它不是默认类型的依赖项 (jar)。
这是从上面引用的 SO 答案复制的该任务的副本:
task localTest(type: Test, dependsOn: assembleDebug) {
testClassesDir = sourceSets.testLocal.output.classesDir
workingDir = "${rootProject.projectDir}/app/src/main"
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
最佳答案
我遇到了这个问题并找到了解决方案 - 将分解包 (.aar) 中的 classes.jar 包含在构建文件夹中。不过,我认为这不会有助于在 .aar 依赖项中查找资源。
testLocalCompile fileTree(dir: "$project.buildDir/exploded-bundles", include: "**/classes.jar")
编辑:自 Android Gradle 构建工具 0.9.0 以来,依赖项已更改为:
androidTestCompile fileTree(dir: "$project.buildDir/exploded-aar", include: "**/classes.jar")
从 Android Gradle 插件 0.12.2 开始:
testLocalCompile fileTree(dir: "$project.buildDir/intermediates/exploded-aar/", include:"**/classes.jar")
关于android - 依赖于 'aar' 的 Gradle Android 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21099754/