简体   繁体   English

Android Studio-如何导出包含其他jar的jar?

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

I use below gradle to generate .jar file 我使用下面的gradle生成.jar文件

When I export using below gradle I am able to generate .jar file but it has not attached the dependent MyJar file. 当我使用下面的gradle导出时,我可以生成.jar文件,但它没有附加从属MyJar文件。

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 : 向库的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 : 如果您只想从lib文件夹添加.jar ,请添加到build.gradle

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

And invoke from command line: 并从命令行调用:

gradlew :{library_module_name}:jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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