简体   繁体   中英

Android Studio-how to export a jar with other jars in it?

I use below gradle to generate .jar file

When I export using below gradle I am able to generate .jar file but it has not attached the dependent MyJar file.

apply plugin: 'com.android.library'//To generate Jar

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.0"
    sourceSets {
        main {
            //Path to My source code
            java {
                srcDir 'src/main/java'
            }
        }
    }
    defaultConfig {

        minSdkVersion 15
        targetSdkVersion 24

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile files('libs/MyJar.jar')//MyJar When generating the Jar this is not include by gradle?
}

//task to delete the old jar
task deleteOldJar(type: Delete) {
    delete 'release/ExportedJar.jar'
}

//task to export contents as jar
task exportJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('release/')    
    include('classes.jar')
    //include('MyJar.jar')//Tried this but No luck    
    rename('classes.jar', 'ExportedJar.jar')
}

exportJar.dependsOn(deleteOldJar, build)

How to achieve this...is there any turnaround.Thanks in Advance

What you would like to achieve is to build a so-called "Fat Jar".

Add another task to your library's build.gradle :

task fatJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

and invoke through command line:

gradlew fatJar

If you would like to add only .jar 's from lib folder, then add to build.gradle :

jar {
    into('lib') {
        from 'libs'
    }
}

And invoke from command line:

gradlew :{library_module_name}:jar

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