1. Introduction

A Gradle plugin for yGuard.

2. Usage

There are two choices for applying the plugin described in this document

Option #1

buildscript {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
    dependencies {
        classpath 'org.kordamp.gradle:yguard-gradle-plugin:0.5.0'
    }
}
apply plugin: 'org.kordamp.gradle.yguard'

Option #2

plugins {
    id 'org.kordamp.gradle.yguard' version '0.5.0'
}

2.1. Requirements

Java 8 and Gradle 6 are the minimum requirements to use this plugin.

2.2. DSL

This DSL follows closely the definitions of the Ant DSL.

Take special note that Java keywords use an underscore as suffix, i.e, class_, package_, extends_, and implements_.

yguard {
    enabled
    resources
    classpath
    externalClasses
    inputJars
    outputDirectory
    verbose

    attribute {
        name
        include()
        exclude()
    }

    shrink {
        enabled
        logFile
        createStubs
        entryPointJar

        props {
            errorChecking
            namingScheme
            languageConformity
            overloadEnabled
            obfuscationPrefix
            digests
            exposeAttributes
        }

        keep {
            sourcefile
            localvariabletable
            localVariableTypeTable
            linenumbertable
            runtimeVisibleAnnotations
            runtimeVisibleTypeAnnotations
            runtimeInvisibleAnnotations
            runtimeInvisibleTypeAnnotations
            runtimeVisibleParameterAnnotations
            runtimeInvisibleParameterAnnotations
            sourceDebugExtension

            class_ {
                name
                classes
                methods
                fields
                extends_
                implements_
                include()
                exclude()
            }

            method {
                name
                class_
                include()
                exclude()
            }

            field {
                name
                class_
                include()
                exclude()
            }
        }
    }

    rename {
        enabled
        logFile

        props {
            errorChecking
            namingScheme
            languageConformity
            overloadEnabled
            obfuscationPrefix
            digests
            exposeAttributes
        }

        keep {
            sourcefile
            localvariabletable
            localVariableTypeTable
            linenumbertable
            runtimeVisibleAnnotations
            runtimeVisibleTypeAnnotations
            runtimeInvisibleAnnotations
            runtimeInvisibleTypeAnnotations
            runtimeVisibleParameterAnnotations
            runtimeInvisibleParameterAnnotations
            sourceDebugExtension

            class_ {
                name
                classes
                methods
                fields
                extends_
                implements_
                include()
                exclude()
            }

            method {
                name
                class_
                include()
                exclude()
            }

            field {
                name
                class_
                include()
                exclude()
            }

            package_ {
                include()
                exclude()
            }

            sourceFile {
                mapping
                include()
                exclude()
            }

            lineNumberTable {
                mappingScheme
                scramblingSalt
                include()
                exclude()
            }

            adjust {
                file
                replaceName
                replaceContent
                replacePath
                include()
                exclude()
            }
        }
    }
}

2.3. Tasks

2.3.1. yguardMain

Executes yGuard on the JAR file generated with classes & resources from the main SourceSet.

Type

org.kordamp.gradle.plugin.yguard.tasks.YGuardGradleTask

Classpath

project.configurations.yguard

ExternalClasses

project.configurations.compileClasspath + project.configurations.runtimelasspath

InputJars

project.tasks.jar.archiveFile

OutputDirectory

${project.buildDir}/yguard/main

2.4. Examples

2.4.1. Getting Started

yguardMain {
    shrink {
        logFile = "${project.name}_shrinklog.xml"
        keep {
            class_ {
                classes = 'protected'
                methods = 'protected'
                fields  = 'protected'
                include('com.mycompany.publicapi.**.*'
                exclude('com.mycompany.publicapi.private.*')
                include('com.mycompany.menu.reflection.**.*')
            }
        }
    }

    rename {
        logFile   = "${project.name}_renamelog.xml"
        mainClass = 'com.mycompany.myapp.Main'
        props {
            errorChecking = true
        }
        keep {
            class_ {
                classes = 'protected'
                methods = 'protected'
                fields  = 'protected'
                include('com.mycompany.publicapi.**.*'
                exclude('com.mycompany.publicapi.private.*')
            }
        }
    }
}

2.4.2. Demo Program

yguardMain {
    shrink {
        keep {
            // main method
            method {
                name   = 'void main(java.lang.String[])'
                class_ = 'com.mycompany.myapp.Main'
            }
            // needed for reflection
            class_ {
                name    = 'com.mycompany.myapp.data.DataObject'
                methods = 'public'
                fields  = 'none'
            }
            //  needed for reflection (name only)
            class_ {
                name    = 'com.mycompany.myapp.data.InnerDataObject'
            }
            // needed for serialization
            method {
                name   = 'void writeObject(java.io.ObjectOutputStream)'
                include('com.mycompany.myapp.data.*')
            }
            method {
                name   = 'void readObject(java.io.ObjectInputStream)'
                include('com.mycompany.myapp.data.*')
            }
            field {
                name   = 'serialVersionUID'
                include('com.mycompany.myapp.data.*')
            }
        }
    }