How to build 1 jar of 3 submodules using gradle

I have:

  • Android Studio 3
  • gradle 4.1
  • gradle tools 3: classpath 'com.android.tools.build:gradle:3.0.1'

When I had one module and gradle 2 tools were used, I used:

task makeJar(type: Copy) {
    def releaseFolder = new File('build/intermediates/bundles/release/')
    if (releaseFolder.exists()) {
        from('build/intermediates/bundles/release/')
    } else {
        from('build/intermediates/bundles/default/')
    }
    into('build/outputs/jar/')
    include('classes.jar')
    rename ('classes.jar', 'MY-Android-SDK.jar')
}

You now have 3 modules:

              MainModule (com.my)
                 /   \
(com.my.m1) Module1   Module2 (com.my.m2)

I want to create a MY-Android-SDK.jarfile from all three modules .

So far I can create 3 jar files for each module and somehow combine them, but I believe that this is not the right way to do this.

Any ideas?

+4
source share
2 answers

First of all, if you are creating Android libraries, create an AAR, not a JAR. AAR files follow the structure of the android SDK and can have manifest files and therefore require permission and xml resources, among other things.

-, ( !), ( AAR/JAR). , SDK, , ( , SDK, gradle , fiddling with proguard ), (, , , DFP . , Inversion of Control, , amazon. amazon amazon.)

, . , , , build.gradle. "api":

api 'com.myname:moduleX:xxx'

com.myname: moduleY, X . Gradle maven . AAR Android "" maven (mavenLocal() gradle)). "api", . , , , AAR .

, - fat/Uber JAR, , "". , gradle . , . ( maven maven shade). , -, , . , gradle maven, , , . !

+3

settings.gradle

settings.gradle

rootProject.name = 'MainModule'
include 'module2'
include 'module3'

build.gradle, MainModule, :

build.gradle

configurations {
    modules
    compile.extendsFrom(modules)
}

dependencies{
    modules project(':moudle2')
    modules project(':moudle3')
}

task packageAll(type: Jar) {
    manifest {
        attributes 'Main-Class': 'YourMainClass'
    }

    // pack yours main module
    from sourceSets.main.output

    // pack your other two modules
    from {
        configurations.modules.collect { it.isDirectory() ? it : zipTree(it) }
    }

    // pack all dependencies in the project
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }

    version = ""
    baseName = "MY-Android-SDK"
}
+1

Source: https://habr.com/ru/post/1691023/


All Articles