[JAVA] Use ProGuard with Gradle

I've never used ProGuard in Gradle, so make a note of how to do it.

Note: This article is * not * about Android. When you say ProGuard in Gradle, there are only articles for Android, but this is not the case.

build.gradle

buildscript {
    dependencies {
        classpath(
                'net.sf.proguard:proguard-gradle:6.0.3'
        )
    }
}

//Omission...

jar {
    manifest {
        attributes 'Main-Class': 'rip.deadcode.Main'  //Modify according to your Main class
    }
    //Make Fat Jar
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}

task proguard(type: proguard.gradle.ProGuardTask, dependsOn: jar) {

    def javaHome = System.getProperty('java.home')

    //JAR for Shrink
    injars jar.archivePath

    libraryjars files(
            "${javaHome}/lib/rt.jar",  //Java SE runtime
            "${javaHome}/lib/jce.jar"  //crypto module
    )

    //If you don't use Fat JAR, add dependent libraries to libraryjars
//    libraryjars configurations.compile.files

    //Output destination please as you like
    outjars("${jar.destinationDir}/proguarded.jar")

    //Classes that do not shrink
    keep("public class ${jar.manifest.attributes['Main-Class']} { public static void main(java.lang.String[]); }")

    dontwarn("ch.qos.logback.**")
    dontwarn('afu.org.checkerframework.**')
    dontwarn("org.checkerframework.**")
    dontwarn('org.slf4j.**')
}

//ProGuard when assembling
assemble.dependsOn(proguard)

point

ProGuard on AWS Lambda

I'd like to remove unnecessary code as much as possible to improve the speed at cold start, but various settings are required.

keep("public class rip.deadcode.bot.Application { *; }")  //Method implementing RequestHandler
keep("public interface com.amazonaws.services.lambda.runtime.RequestHandler { *; }")
keep("class com.amazonaws.** { *; }")
keep("class com.fasterxml.** { *; }")
keepattributes("Signature,*Annotation*")

With RequestStreamHandler, it seems that many parts of this time will not be a problem, so it may be necessary to use it instead of wearing it sideways.

Recommended Posts

Use ProGuard with Gradle
Use WebJars with Gradle
Use jlink with gradle
Use log4j2 with YAML + Gradle
Integration Test with Gradle
Use Puphpeteer with Docker
Use XVim2 with Xcode 12.0.1
Use CentOS with LXD
Install Gradle with ubuntu16.04
DataNucleus starting with Gradle
Use ngrok with Docker
Use webmock with Rspec
How to use Gradle
Get started with Gradle
Use Lambda Layers with Java
Gradle + Kotlin-Generate jar with DSL
Use Thymeleaf with Azure Functions
Use pfx certificate with Okhttp3
Use Bulk API with RestHighLevelClient
Lombok not working with Gradle5
Use SDKMAN! With Git Bash
Remote debugging with Gradle test
Use multiple databases with Rails 6.0
Use Spring JDBC with Spring Boot
Use Ruby with Google Colab
Use SpatiaLite with Java / JDBC
Hello World with SpringBoot / Gradle
[Docker] Use whenever with Docker + Rails
Use PlantUML with Visual Studio Code
Use Basic Authentication with Spring Boot
Build a Java project with Gradle
Use java with MSYS and Cygwin
Use constructor with arguments in cucumber-picocontainer
Gradle
Use Microsoft Graph with standard Java
Use PostgreSQL inet type with DbUnit
I tried using JOOQ with Gradle
Why use orchestration tools with Docker
Use bootstrap 4 with PlayFramework 2.6 (no CDN)
Let's use Amazon Textract with Ruby
Output test coverage with clover + gradle
I can't install lombok with Gradle.
Use Git with SourceTree and Eclipse
Use Azure Bing SpellCheck with Java
Use JDBC with Java and Scala.
Use DataDog APM with unsupported frameworks
Use Java 11 with Google Cloud Functions
How to use mssql-tools with alpine
Beginning with Spring Boot 0. Use Spring CLI
Use cuda11.0 with pytorch using Docker
Develop Processing with IntelliJ + Kotlin + Gradle
Spring Boot gradle build with Docker
[Java] Create an executable module with Gradle
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
Use Firebase Realtime Database with Cocos Creator.
I want to use DBViewer with Eclipse 2018-12! !!
CICS-Run Java applications-(3) Build management with Gradle
Use Symbolic Link with Docker multi-stage builds
Use FacesContext as a Mock with PowerMockito
[JaCoCo (Java Code Coverage)] Use with NetBeans