Android Studio-如何将jar与其他jar一起导出?

时间:2016-11-23 11:31:44

标签: android android-studio gradle jar sdk

我使用下面的gradle生成.jar文件

当我使用下面的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)

如何实现这一目标......是否有任何好转。谢谢你提前

1 个答案:

答案 0 :(得分:-1)

你想要达到的目的是建立一个所谓的" Fat Jar"。

将另一项任务添加到您的资源库build.gradle

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

并通过命令行调用:

gradlew fatJar

如果您只想在.jar文件夹中添加lib,请添加到build.gradle

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

从命令行调用:

gradlew :{library_module_name}:jar