簡體   English   中英

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

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

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

如何實現這一目標...是否有任何轉變。

您想要實現的是構建一個所謂的“胖罐”。

向庫的build.gradle添加另一個任務:

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

並通過命令行調用:

gradlew fatJar

如果您只想從lib文件夾添加.jar ,請添加到build.gradle

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

並從命令行調用:

gradlew :{library_module_name}:jar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM