简体   繁体   中英

In Android Studio refer to .apk classes from other module

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM