Bradley Campbell
Bradley Campbell

Reputation: 9808

Gradle provided JAR dependency not working as expected

I've been stuck on this problem all day, I'm hoping someone can help me get my program running.

I am building an Annotation Processor for Android, it currently has two modules: "mymodule" and "mymodule-compiler"

mymodule-compiler depends on mymodule:

dependencies {
  compile project(':mymodule')
  ...
}

mymodule uses the netflix "provided" plugin for Android:

apply plugin: 'nebula.provided-base'

dependencies {
  provided 'com.google.android:android:4.1.1.4'
}

This actually works perfectly, however when I run my compiler unit test suite, a lot of tests fail because the Android dependency specified is v4.1.1.4 and I actually need v6.0. v6.0 is not available on Maven (and looks like it never will be because Google haven't updated it since 2012).

I found out that the Android SDK provides a "Unit Test" JAR version of Android for each release, which is the entire SDK "stubbed out". I assume the versions on Maven are stubbed out too(?). I can easily grab that JAR and put it in my project under libs/.

Now when I update the mymodule gradle file:

dependencies {
  provided files('libs/android.jar')
}

My project still compiles correctly and I can see v23 files! However, when I run my unit tests, I see the following error:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
  at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
  at junit.textui.TestRunner.<init>(TestRunner.java:54)
  at junit.textui.TestRunner.<init>(TestRunner.java:48)
  at junit.textui.TestRunner.<init>(TestRunner.java:41)
  at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:205)
  at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:188)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

It shouldn't matter that the Android JAR is stubbed out because I never execute Android code, I only need it for imports.

Does anyone have any ideas about what I'm seeing and how I could fix this?

EDIT 1: More info:

If I remove the Android dependencies from mymodule entirely, and instead put the following into mymodule-compiler.gradle:

dependencies {
  testCompile files('libs/android.jar')
  ...
}

All my tests will pass. This is obviously not a fix though, as 'mymodule' does need the Android dependency.

EDIT 2:

Doing the same test as EDIT 1, but using the netflix provided scope instead of testCompile works too. If I use the provided scope in both projects though, I get the same Stub! exception.

Upvotes: 0

Views: 279

Answers (1)

F43nd1r
F43nd1r

Reputation: 7749

[Worked out together with OP]

The local jar contains additional classes over the maven jar which seem to interfere. To get it working delete these from the jar (specifically in this case: all junit classes).

Comparison of the two jars (left is maven jar, right is local jar):comparison

Upvotes: 1

Related Questions