0

This question stems from the fact that I am not a Gradle expert and I come from Eclipse + Maven background.

In Maven, I was able to refer to my apk project from another one (a Robolectric sub-project for instance), as the android-maven-plugin was pushing both .apk and (un-dexed) .jar in my local repository.

How can I achieve the same in Android Studio + Gradle?

  • I tried to add apply plugin: 'java' to my app project but the java plugin is not compatible with the android plugin.
  • I tried to build a sub-module of my app with the java plugin only with build.gradle, but R.java is not generated this way:

    sourceSets {
        main {
            java.srcDirs = parent.android.sourceSets.main.java.srcDirs
        }
    } 
    

Is there any other way to do this?

EDIT4: posted solution.

Andrea Richiardi
  • 703
  • 6
  • 21
  • possible duplicate of [Android Library Gradle release JAR](http://stackoverflow.com/questions/19307341/android-library-gradle-release-jar) – Scott Barta Mar 02 '14 at 15:27
  • Yes, adding what's below solves: ```task androidJar(type: Jar) { from android.sourceSets.main.allJava }``` – Andrea Richiardi Mar 03 '14 at 00:58

1 Answers1

0

The solution in my edits actually works well. I report it as solution here. Watch out, it hard-codes the path for the class files, I wasn't able to find out how to use the $buildType in it (is there a way?)

def variantName = "debug" // init to debug

apply plugin: 'android'

android {
    ...
    applicationVariants.all { variant ->
        variantName = variant.name
        buildDebugJar.dependsOn variant.packageApplication
    }
}

// Builds the jar containing compiled .java files only (no resources).
task buildJar(type: Jar) {
    description 'Generates the jar file along with the apk.'
    from new File("$project.buildDir/classes/$variantName")
}

configurations {
    jarOfApk
    transitive = false
}

artifacts {
    jarOfApk buildJar
}

And the referring module(s), will declare a new dependency this way:

compile project(path: ':app', configuration: 'jarOfApk')
Andrea Richiardi
  • 703
  • 6
  • 21