在Android Gradle 3.6.1中运行构建任务后未生成Jar

时间:2020-03-12 08:58:02

标签: android gradle build

我的build jar任务在3.5.3年级可以正常工作,但是在3.6.1年级不起作用。我检查了我的构建文件,但是/build/intermediates/packaged-classes/目录却很奇怪。

gradle脚本:

task buildJar(dependsOn: ['build'], type: Jar) {
    doFirst {
        manifest {
            attributes 'Jar-VersionName': jarVersionName
            attributes 'Jar-VersionCode': jarVersionCode
        }
    }
    from zipTree(file('build/intermediates/packaged-classes/release/classes.jar'))
    appendix = ""
    baseName = "pp"
    version = jarVersionName + "_" + jarVersionCode
    classifier = "release"
    extension = "jar"
    destinationDir = file('build/libs/')
}

//dx --dex --output=target.jar origin.jar
task buildDex(dependsOn: ['buildJar'], type: Exec) {
    workingDir buildDir.absolutePath + "/libs"
    executable "dx"
    args "--dex"
    args "--output=" + buildJar.archiveName
    args buildJar.archiveName
}

错误日志:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':p2p:buildJar'.
> Cannot expand ZIP '/Users/william/AndroidStudioProjects/Work/lib_plugin/project/build/intermediates/packaged-classes/release/classes.jar' as it does not exist.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

我环顾四周,显然我可以在这些解决方案之间进行选择,但是它们对我来说太老了。

No release bundle folder after upgrading Android Studio

.jar file not generating in android studio

Gradle not generate jar

那么,生成的jar的新路径在哪里?

1 个答案:

答案 0 :(得分:2)

我找到了!将gradle版本升级到4.0时,我在class.jar路径中找到了build/intermediates/aar_main_jar/release/classes.jar

因此,完整的构建任务代码如下:

def jarPath = buildDir.absolutePath + "/libs"
def jarBaseName = "plugin"

task buildJar(dependsOn: ['build'], type: Jar) {
    doFirst {
        manifest {
            attributes 'Jar-VersionCode': jarVersionCode
        }
    }
    from zipTree(file('build/intermediates/aar_main_jar/release/classes.jar'))
    // [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]
    archiveBaseName = jarBaseName
    archiveAppendix = null
    archiveVersion = "$jarVersionName-$jarVersionCode"
    archiveClassifier = "release"
    archiveExtension = "jar"
    destinationDirectory = file(jarPath)
}

// dx --dex --output=target.jar origin.jar
task buildDex(dependsOn: ['buildJar'], type: Exec) {
    workingDir jarPath
    executable "dx"
    args "--dex"
    args "--output=" + buildJar.archiveFileName.get()
    args buildJar.archiveFileName.get()
}