使用 Android 库 (AAR) 作为带有 Gradle 的 Java (Jar) 项目的依赖项

Dan*_*ato 5 android unit-testing gradle rx-android

Java Jarbuild.gradle模块:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'junit:junit:4.12'

    // rx
    compile "io.reactivex.rxjava2:rxjava:2.0.4"
    compile "io.reactivex.rxjava2:rxandroid:2.0.1"
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"
Run Code Online (Sandbox Code Playgroud)

这个 Jar 应该是一个库,用作单元测试 android 项目的实用程序库:

apply plugin: 'com.android.application'

android {
    // ...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // test
    testCompile 'junit:junit:4.12'
    testCompile project(':test-utils')

    // ... other non-test dependencies here (including rx)
}
Run Code Online (Sandbox Code Playgroud)

这包括:

    testCompile project(':test-utils')
Run Code Online (Sandbox Code Playgroud)

失败:

错误:模块 'my-android-app:test-utils:unspecified' 依赖于一个或多个 Android 库,但它是一个 jar

这是因为RxAndroid是一个 Android .aar 库。

我的测试实用程序模块中需要 RxAndroid,因为该模块提供的其中一项功能是MethodRule用于通过方法中的@Rule注释和其他自定义注释设置 Rx 调度程序的 junit @Test

这是必需的,因为 RxAndroid AndroidScheduler 使用 Android Handler/Looper类,它们是 Android 框架的一部分,在单元测试中不可用。可以使用RxAndroidPlugins.

这意味着我只需要 RxAndroid AAR 的 JAR 部分(classes.jar在 rxandroid 工件中)。

有没有办法告诉 gradle 我只需要 AAR 的 Java 部分作为依赖项?

我不考虑classes.jar从 AAR手动提取并将其作为依赖项包含在内的解决方案。这是一种解决方法。

这个 Java 测试库应该在我的项目之间共享,我目前正在MethodRule我的每个项目中复制这个类,这远非理想。