O
O
Oleg Dolgih2015-01-20 14:40:17
Android
Oleg Dolgih, 2015-01-20 14:40:17

How to build jar with dependencies from android-library module?

There is a gradle module, which in turn depends on:
another gradle module,
aar file (google-play-services)
jar file (gson).
I need to assemble a jar from all this with all the dependencies, that is, the final jar should have class files:
my modules,
google-play services,
gson.
This is done in order to be able to connect the android library as a jar file (without the obligatory use of gradle or maven, which can connect aar)
I don’t know how to do this.
here is the gradle.build

buildscript {
    repositories {
        mavenCentral()
    }
    
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'android-maven'

group = "com.foo"
version = "1.0"

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['res']
        }
    }

    defaultConfig {
        minSdkVersion 5
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile project (':amlogger')
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.google.code.gson:gson:2.3'
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Dolgih, 2015-08-17
@olegvarmy

Well, actually, I'll answer myself

android.libraryVariants.all { variant ->
    tasks.create(name: "fatJar${variant.name.capitalize()}", type: Jar, dependsOn: assembleRelease) {
        doFirst {
            baseName = variant.name
            destinationDir = file('output')
            duplicatesStrategy = DuplicatesStrategy.EXCLUDE
            from zipTree(tasks.getByName("package${variant.name.capitalize()}Jar").outputs.files.singleFile)
            def closure = { file ->
                if (file.name.endsWith('.aar')) {
                    println file.name
                    List<File> listJarFiles = zipTree(file).findAll { fileInAar ->
                        fileInAar.name.endsWith('.jar')
                    }
                    def listClassesFilesFromJars = listJarFiles.collect {
                        zipTree(it)
                    }
                    listClassesFilesFromJars
                } else if (file.name.endsWith('.jar')) {
                    return zipTree(file)
                }
            }
            from configurations.compile.files.collect(closure)
            from configurations.getByName("${variant.name}Compile").files.collect(closure)

        }
    }
}

adds a task called fatJar{variantName}
each task goes through all compile and {variantName}Compile dependencies, collects jar and aar files (if the file is aar, then it gets classes.jar + jars from libs) and all this is packed into a file with the name {variantName}.jar
and a second script that does the same, but does not pack everything into a fatJar, but collects all the libraries in one directory
android.libraryVariants.all { variant ->
    tasks.create(name: "collectJars${variant.name.capitalize()}", type: Copy, dependsOn: assembleRelease) {
        delete file("output/outputJars${variant.name.capitalize()}")
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        from tasks.getByName("package${variant.name.capitalize()}Jar").outputs.files.singleFile, {
            rename 'classes.jar', variant.name + "-" + project.version + ".jar"
        }
        def closure = { file ->
            if (file.name.endsWith('.aar')) {
                println file.name
                zipTree(file).findAll { fileInAar ->
                    fileInAar.name.endsWith('.jar')
                }.each { f ->
                    if(f.name == "classes.jar") {
                        from f, {
                            println f.name
                            rename "classes", file.name.substring(0, file.name.lastIndexOf("."))
                        }
                    } else {
                        from f
                    }

                }
            } else if (file.name.endsWith('.jar')) {
                from file
            }
        }
        configurations.compile.files.collect(closure)
        configurations.getByName("${variant.name}Compile").files.collect(closure)
        into 'output/outputJars' + variant.name.capitalize()
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question