4

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
}
Community
  • 1
  • 1
Colin M.
  • 455
  • 1
  • 5
  • 18

1 Answers1

4

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") 
Colin M.
  • 455
  • 1
  • 5
  • 18
myanimal
  • 3,580
  • 26
  • 26
  • Yeah, assumed I could manually extract the lib and include that way, but then I'm doing the thing that dependency management through a build file is supposed to take care of. Good to know there is a way to hack it if need-be (but then resources...) Looks like the android-gradle-aar ecosystem just isn't there yet. – Colin M. Mar 11 '14 at 18:45
  • I used this line, essentially, as also referenced in this similar question to mine: http://stackoverflow.com/a/24954215/821636 'unitTestCompile fileTree(dir: "$project.buildDir/intermediates/exploded-aar/", include:"**/classes.jar") ' as of Android Gradle plugin 0.12.2. Will mark this as accepted since it has evolved into the answer that works the best so far. – Colin M. Aug 25 '14 at 19:49
  • Any luck modifying this method to work with flavors? When I add flavors, the testLocal fails not being able to find _any_ of my classes. – Colin M. Nov 20 '14 at 21:24