简体   繁体   中英

Using Proguard in Gradle - can't find referenced class java.lang.String

I'm trying to configure ProGuard as part of my Gradle build scripts for a java application in Ubuntu, accessed through bash, but I can't seem to get it right. I've never used either technology before. Using the ProGuard Manual I wrote the following task, which I currently call on a pre-existing jar file while I try and get something working.

task obfuscate(type: proguard.gradle.ProGuardTask) {
  injars         'build/libs/myapplication.jar'
  outjars        'build/libs/myapplication_out.jar'
  libraryjars    '<java.home>/lib/rt.jar'
  printmapping   'myapplication.map'

  keep 'public class mypackage.MyMainClass {  \
    public static void main(java.lang.String[]);  \
  }'
}

I get back a lot of lines stating missing built in java library files such as:

Warning: myclasses: can't find referenced class java.lang.String

At the bottom of the list I also see:

Could not call ProGuardTask.proguard()

If I check my output directory, I can see it created the directory structure but not the jar file and this is what I see if I run my gradle task with stacktrace on:

Caused by: org.gradle.api.GradleException: Could not call ProGuardTask.proguard() on task

I had a look on the proguard trouble shooting page and it said that I should make sure I'm specifying the run-time library of my platform and that for JSE this is lib/rt.jar. You can see that up there in the library jars argument. What have I missunderstood?

ProGuard is not reading the runtime jar, because its name should be specified differently.

In ProGuard configuration files, you can write:

-libraryjars <java.home>/lib/rt.jar

In Gradle build files, you should use Gradle's style:

libraryjars "${System.getProperty('java.home')}/lib/rt.jar"

Here is what I did to setup my classpath for my gradle proguard task. Used a copy task to gather all the jar files in play and then referenced them in the proguard task.

task prepare(type: Copy) {

    dependsOn 'jar'

    destinationDir = file("$buildDir/assemble/izpack")
    includeEmptyDirs = true

    into('lib') {
        from configurations.compile
    }
    into('proguard') {
        from configurations.runtime {  include '*truelicense*.jar' }
    }
}


task proguard (type: proguard.gradle.ProGuardTask) {

    dependsOn 'prepare'

    configuration "proguard.conf"

    injars jar.archivePath
    fileTree("$buildDir/assemble/izpack/proguard").include("*.jar").each { f ->
        injars f.path, filter: '!META-INF/MANIFEST.MF'
    }

    outjars "$buildDir/assemble/izpack/web/WEB-INF/lib/${jar.baseName}-${jar.version}.${jar.extension}"

    libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
    libraryjars "${System.getProperty('java.home')}/lib/jce.jar"
    fileTree("$buildDir/assemble/izpack/lib").include("**/*.jar").collect { f -> libraryjars f.path }
    fileTree("$buildDir/assemble/izpack/web").include("**/*.jar").collect { f -> libraryjars f.path }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM