swooby swooby - 1 year ago 239
Android Question

Android Gradle 1.1 Module Unit Test dependency on another Android Module in same Project

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

├ 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?



Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download