6

The Java Jar build.gradle module:

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"

This Jar is supposed to be a library used as an utility library for unit testing android projects:

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)
}

This include:

    testCompile project(':test-utils')

fails with:

Error:Module 'my-android-app:test-utils:unspecified' depends on one or more Android Libraries but is a jar

Which is because RxAndroid is an Android .aar library.

I need RxAndroid in my test utility module because one of the things the module provide is a junit MethodRule for setting up Rx Schedulers through the @Rule annotation and other custom annotations in @Test methods.

This is needed because RxAndroid AndroidScheduler use Android Handler / Looper class, which are part of the Android framework and are not available in unit tests. The Android Scheduler can be replaced using RxAndroidPlugins.

Which means I only need the JAR part of the RxAndroid AAR (classes.jar inside rxandroid artifact).

Is there a way to tell gradle I only need the Java part of the AAR as dependency?

I do not consider a solution manually extracting the classes.jar from the AAR and including it as a dependency. That's a workaround.

This Java test library is supposed to be shared among my projects and I'm currently copying the MethodRule class in every of my projects which is far from ideal.

Daniele Segato
  • 12,314
  • 6
  • 62
  • 88
  • refer to this link : http://stackoverflow.com/a/26228126/7111867 it may help u – Ashish Jan 31 '17 at 10:03
  • thank you, but that's for handling local dependencies *as-if* they were standard artifacts. In other words it makes your libs/ directory act like a fake maven repository and thus allow you to include dependency in the standard compile 'group:name:version@type' fashion. What I need is get the dependency from jcenter (real maven repo) but only depend to the `classes.jar` inside it (aka = the java part) – Daniele Segato Jan 31 '17 at 10:26
  • try this one: http://stackoverflow.com/a/34525498/7111867 i think you also wants something like this – Ashish Jan 31 '17 at 10:31
  • that's really interesting, but, unless I'm completely off on how gradle works, removing stuff from the aar is not going to make it a non-android-library package or make the java project include the classes.jar file inside it. Thank you for your input anyway, very instructive. – Daniele Segato Jan 31 '17 at 10:59
  • anytime welcome :) – Ashish Jan 31 '17 at 11:10

0 Answers0