Gradle集成ProGuard报错:无法识别ProGuardTask属性
Java Gradle项目配置ProGuard混淆报错解决
问题背景
我需要为Java项目添加ProGuard混淆功能,实现构建时自动处理JAR包。创建默认build.gradle并添加依赖后,粘贴了ProGuard官方提供的配置代码,但始终报错:
Could not get unknown property 'ProGuardTask' for root project 'MCPets-multipet-gradle' of type org.gradle.api.Project.
原build.gradle内容:
// ProGuard buildscript { repositories { mavenCentral() } dependencies { classpath 'com.guardsquare:proguard-gradle:7.7.0' } } plugins { id 'java' } group 'org.example' version '1.0-SNAPSHOT' repositories { mavenCentral() maven{url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'} maven{url = 'https://repo.papermc.io/repository/maven-public/'} maven{url = 'https://libraries.minecraft.net/'} maven{url = 'https://mvn.lumine.io/repository/maven-public/'} maven{url = 'https://maven.enginehub.org/repo/'} maven{url = 'https://repo.extendedclip.com/content/repositories/placeholderapi/'} maven{url = 'https://oss.sonatype.org/content/repositories/snapshots'} maven{url = 'https://jitpack.io'} } dependencies { // Technical dependencies compileOnly("org.projectlombok:lombok:1.18.38") annotationProcessor("org.projectlombok:lombok:1.18.38") implementation 'com.google.code.gson:gson:2.8.9' // Minecraft dependencies implementation 'org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT' implementation 'io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT' // Plugin dependencies implementation 'io.lumine:Mythic-Dist:5.6.0-SNAPSHOT' implementation 'com.sk89q.worldguard:worldguard-bukkit:7.0.7-SNAPSHOT' implementation 'me.clip:placeholderapi:2.11.6' implementation 'net.luckperms:api:5.4' implementation 'com.ticxo.modelengine:ModelEngine:R4.0.6' implementation 'com.github.LoneDev6:api-itemsadder:3.6.1' } // ProGuard tasks.register('proguard', ProGuardTask) { configuration file('proguard.pro') injars(tasks.named('jar', Jar).flatMap { it.archiveFile }) // Automatically handle the Java version of this build. if (System.getProperty('java.version').startsWith('1.')) { // Before Java 9, the runtime classes were packaged in a single jar file. libraryjars "${System.getProperty('java.home')}/lib/rt.jar" } else { // As of Java 9, the runtime classes are packaged in modular jmod files. libraryjars "${System.getProperty('java.home')}/jmods/java.base.jmod", jarfilter: '!**.jar', filter: '!module-info.class' //libraryjars "${System.getProperty('java.home')}/jmods/....." } verbose outjars(layout.buildDirectory.file("libs/${baseCoordinates}-minified.jar")) }
解决方案
问题根源
ProGuardTask未被Gradle识别,因为没有导入对应的类或应用ProGuard插件。
修复方案一:导入ProGuardTask类
在注册任务前添加导入语句,同时修正baseCoordinates为实际项目坐标:
// 导入ProGuardTask类 import proguard.gradle.ProGuardTask // ProGuard任务注册 tasks.register('proguard', ProGuardTask) { configuration file('proguard.pro') injars(tasks.named('jar').flatMap { it.archiveFile }) // Java版本适配 if (System.getProperty('java.version').startsWith('1.')) { libraryjars "${System.getProperty('java.home')}/lib/rt.jar" } else { libraryjars "${System.getProperty('java.home')}/jmods/java.base.jmod", jarfilter: '!**.jar', filter: '!module-info.class' } verbose // 使用项目的group、name、version生成输出文件名 outjars(layout.buildDirectory.file("libs/${project.group}-${project.name}-${project.version}-minified.jar")) }
修复方案二:应用ProGuard插件(推荐)
改用插件方式引入ProGuard,无需手动导入类,配置更简洁:
- 移除原
buildscript块,在plugins块中添加ProGuard插件:
plugins { id 'java' id 'com.guardsquare.proguard' version '7.7.0' }
- 注册ProGuard任务(插件已自动引入
ProGuardTask):
tasks.register('proguard', ProGuardTask) { configuration file('proguard.pro') injars(tasks.named('jar').flatMap { it.archiveFile }) // Java版本适配逻辑保持不变 if (System.getProperty('java.version').startsWith('1.')) { libraryjars "${System.getProperty('java.home')}/lib/rt.jar" } else { libraryjars "${System.getProperty('java.home')}/jmods/java.base.jmod", jarfilter: '!**.jar', filter: '!module-info.class' } verbose outjars(layout.buildDirectory.file("libs/${project.group}-${project.name}-${project.version}-minified.jar")) }
额外注意事项
- 确保项目根目录存在
proguard.pro配置文件,需根据项目需求配置保留的类(例如Minecraft插件的主类、命令类、事件监听类等,避免混淆后无法被加载)。 - 执行混淆任务:运行
./gradlew proguard,混淆后的JAR包会生成在build/libs目录下。
内容的提问来源于stack exchange,提问作者Nathalie Kitty
相关产品推荐
相关产品推荐

