Gradle:分发可执行的混淆 Jar 文件

des*_*vai 3 jar proguard gradle

我正在尝试使用带有 proguard 的 gradle 来混淆代码,然后生成一个 zip 文件进行分发。我想使用分发插件,但它总是包含由 jar 任务生成的 jar。有什么方法可以强制分发插件省略原始(非混淆)jar 并只包含混淆的 jar?除了原始 jar 之外,我还可以轻松添加混淆 jar,但我想分发混淆 jar而不是原始jar ,以便生成的执行脚本针对混淆版本运行。

这是我的删节版 build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:5.3.3'
    }
}

apply plugin: 'java'
apply plugin: 'application'

task obfuscate(type: proguard.gradle.ProGuardTask) {
    configuration 'proguard.txt'

    injars "build/libs/${rootProject.name}.jar"
    outjars "build/libs/${rootProject.name}-release.jar"
}

jar.finalizedBy(project.tasks.obfuscate)

distributions {
    main {
        contents {
            from(obfuscate) {
                into "lib"
            }
            from(jar) {
                exclude "*.jar"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在发行版块中尝试了很多东西来尝试排除原始 jar,但似乎没有任何效果。

任何想法将不胜感激。

des*_*vai 5

这不是最好的解决方案,但我能够通过在混淆步骤结束时重命名罐子来解决这个问题。现在,我将原始 jar 命名为类似的名称,<JAR_NAME>-original.jar并为混淆后的 jar 指定原始 jar 的名称。我仍然希望有更好的方法来做到这一点,但这似乎有效。

这是更新后的删节build.gradle文件:

import java.nio.file.Paths

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:5.3.3'
    }
}

apply plugin: 'java'
apply plugin: 'application'

def jarNameWithoutExtension = jar.archiveName.with { it.take(it.lastIndexOf(".")) }
def obfuscatedJarName = "${jarNameWithoutExtension}-release.jar"
def jarFileLocation = jar.archivePath.parent
def obfuscatedFilePath = Paths.get(jarFileLocation, obfuscatedJarName)

task obfuscate(type: proguard.gradle.ProGuardTask) {
    configuration 'proguard.txt'

    injars jar.archivePath
    outjars obfuscatedFilePath.toString()

    // Rename the original and obfuscated jars.  We want the obfuscated jar to
    // have the original jar's name so it will get included in the distributable
    // package (generated by installDist / distZip / distTar / assembleDist).
    doLast {
        jar.archivePath.renameTo(Paths.get(jarFileLocation, "$jarNameWithoutExtension-original.jar").toFile())

        obfuscatedFilePath.toFile().renameTo(jar.archivePath)
    }
}

jar.finalizedBy(project.tasks.obfuscate)
Run Code Online (Sandbox Code Playgroud)