0

我用 Gradle 为 Android 平台构建了一个 jar。当我使用该dx命令时,它可以很好地构建一个带有清单的 jar,但dx不支持 Java 8 语法。所以我必须将编译工具更改为d8,它支持 Java 8 语法但它删除了MANIFEST.MF文件。那么如何使用d8编译工具来保存manifest呢?

构建dx在此处输入图像描述

构建d8在此处输入图像描述

Gradle 构建脚本代码:

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=output.jar input.jar
task buildDex1(dependsOn: buildJar, type: Exec) {
    workingDir jarPath
    executable "dx"
    args "--dex"
    args "--output=" + buildJar.archiveFileName.get()
    args buildJar.archiveFileName.get()
}

// d8 --release --output output.jar input.jar
task buildDex2(dependsOn: buildJar, type: Exec) {
    workingDir jarPath
    executable "d8"
    args "--release"
    args "--output"
    args buildJar.archiveFileName.get()
    args buildJar.archiveFileName.get()
}
4

0 回答 0