Gradle Android unit tests that depend on an 'aar'
android, gradle, unit-testing
Solution
I ran into this problem and found a solution - include the classes.jar from the exploded bundle (.aar) in the build folder. I don't think will help with finding resources in .aar dependencies though.
testLocalCompile fileTree(dir: "$project.buildDir/exploded-bundles", include: "**/classes.jar")
Edit: Since Android Gradle build tools 0.9.0 the dependency has changed to:
androidTestCompile fileTree(dir: "$project.buildDir/exploded-aar", include: "**/classes.jar")
As of Android Gradle plugin 0.12.2:
testLocalCompile fileTree(dir: "$project.buildDir/intermediates/exploded-aar/", include:"**/classes.jar")
Problem
Want to be able to depend on an AAR for my unit tests in a Gradle-Android project. I do not want to manually include the external library source or binaries inside my project (for multiple reasons). We can depend on remote aar files like this in the build.gradle: `compile 'com.facebook:facebook-android-sdk:3.6.0:@aar'` But when using the gradle test method described here: https://stackoverflow.com/a/16952507/821636 (Since Jake Wharton's plugin, isn't quite there yet), trying to include an aar as a dependency for that method like this: `testLocalCompile 'com.facebook:facebook-android-sdk:3.6.0:@aar` Does not seem to actually include the aar in the classpath for tests (run with `./gradlew check`) since I get `NoClassDefFoundError` for classes in the aar. NOTE: When jar dependencies are included this way, they work. Thinking there must be something in that `localTest` task that needs to add the aar extension since it's not the default type of dependency (jar). Here is copy of that task copied from the SO answer referenced above: ``` task localTest(type: Test, dependsOn: assembleDebug) { testClassesDir = sourceSets.testLocal.output.classesDir workingDir = "${rootProject.projectDir}/app/src/main" android.sourceSets.main.java.srcDirs.each { dir -> def buildDir = dir.getAbsolutePath().split('/') buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/') sourceSets.testLocal.compileClasspath += files(buildDir) sourceSets.testLocal.runtimeClasspath += files(buildDir) } classpath = sourceSets.testLocal.runtimeClasspath } ```