4

I have an Android Gradle 1.1 Project with several Modules that have some dependency on the others.

Project
├ Module2
│ ├ src/main: imports classes from Module1
│ └ src/test: imports classes from Module1, tests Module2/src/main
└ Module1
  ├ src/main: implements classes needed by Module2/src/main & Module2/src/test
  └ src/test: tests Module1/src/main

My Modules themselves compile fine.
My question is related to testing the modules. (FYI: I am using Robolectric)

I have seen several "similar" questions related to Android build & test dependencies, but none of the ones that I have found seem to be asking what I am looking for:

These questions are also either old, not focused on using Android Gradle 1.1+ built in Unit Test abilities, or not really that relevant.

What I am asking is: "How can a [java compiled?] unit test import another android module (normally a .aar file) in the same project?"

A "testCompile project(':module1')" doesn't seem to do the trick.

I suspect that I want to either:

  1. Compile the other module that I depend on as a .jar file
  2. Use the other module's already compiled .class files
  3. Extract the .class files from the other module's .aar file
  4. Find some other built in way to do this

Is there a slick way to do this built in to the Android/Gradle build process?

Am I missing something obvious here?

Thanks!

Pv

Community
  • 1
  • 1
swooby
  • 3,005
  • 2
  • 36
  • 43

2 Answers2

0

The secret is to have project.properties and test-project.properties file next to your AndroidManifest.xml

At the test-project.properties insert all your exploded aar resource path (e.g. android.library.reference.3=../../build/intermediates/exploded-aar/AndroidStudioAndRobolectric/core/unspecified)

I explain this a bit more at http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html And a ready to use example can be found at https://github.com/nenick/AndroidStudioAndRobolectric/tree/library

nenick
  • 7,340
  • 3
  • 31
  • 23
  • Awesome! I'll give it a try! – swooby Mar 05 '15 at 19:57
  • I'm trying your solution, but getting a weird java.lang.NullPointerException at org.robolectric.AndroidManifest$MetaData.init(AndroidManifest.java:736). https://github.com/robolectric/robolectric/blob/robolectric-2.4/robolectric/src/main/java/org/robolectric/AndroidManifest.java#L736 I have posted a comment to your blog article. – swooby Mar 05 '15 at 21:46
  • Thanks @nenick, your solution basically works, but there is a slew of Robolectric bugs related to meta-data in the manifest, so I resorted to trying to simplify what I am trying to do. I'll give ya credit for the nifty trick anyway! :) – swooby Mar 06 '15 at 22:43
0

If your module under test is depending on pure Java/non-Android code, an alternative approach is to extract out a new java-library module (one that builds a .jar rather than an .aar) and depend on that. Unit tests can run fine in this case.

(Probably not so helpful for the original poster since they are using Robolectric but still)

user1405990
  • 806
  • 1
  • 8
  • 19