= proguardとfatjar = proguardを使って難読化した後、fatjar化する。もちろん、依存ライブラリの難読化などはしない。 {{{ apply plugin: 'java' // グループ group = "sample" /** バージョン */ version = 0.8 // 全ソースがUTF-8であることを指定する //[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } // Javaソースのバージョン sourceCompatibility = 1.8 // 生成クラスのバージョン targetCompatibility = 1.8 def NO_JAR = 'build/normal.jar' def OB_JAR = 'build/obfuscated.jar' def OB_MAP = 'build/ob-map.txt' def OB_FAT = 'build/ob-fat.jar' def NO_FAT = 'build/no-fat.jar' def MAIN_CLASS = "sample.Main' def APP_NAME = "sample" // 依存を取得する場合のリポジトリ repositories { maven { url CENTRAL_REPOSITORY } maven { url INHOUSE_REPOSITORY } } // ソースの指定 sourceSets { main { java { srcDir '....src' } resources { srcDir '....src' } } } // 依存ライブラリの指定 dependencies { compile group: 'javax.inject', name: 'javax.inject', version: '1' .... } jar { // 出力先ファイル destinationDir = file('.') archiveName = NO_JAR // マニフェスト manifest { attributes 'Implementation-Title': APP_NAME, 'Implementation-Version': version attributes "Main-Class" : MAIN_CLASS exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude '.readme' } } // proguard外部ツール設定 buildscript { repositories { maven { url CENTRAL_REPOSITORY } } dependencies { classpath 'net.sf.proguard:proguard-gradle:5.3.2' } } // 難読化する task obfuscation(type: proguard.gradle.ProGuardTask, dependsOn: jar) { injars NO_JAR outjars OB_JAR libraryjars "${System.getProperty('java.home')}/lib/rt.jar" libraryjars "${System.getProperty('java.home')}/lib/ext/jfxrt.jar" libraryjars configurations.compile dontwarn 'javax.annotation.**' dontwarn 'org.apache.**' .... //dontwarn dontnote keep 'public class sample.Main { \ public static void main(java.lang.String[]); \ }' printmapping OB_MAP renamesourcefileattribute 'SourceFile' keepattributes 'Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod' } // ノーマルなfatjarを作成する task noFat(type: Jar) { // 出力先ファイル destinationDir = file('.') archiveName = NO_FAT // 入力ファイル from { configurations.compile.collect { zipTree(it) }; } with jar // マニフェスト manifest { attributes 'Implementation-Title': APP_NAME, 'Implementation-Version': version attributes 'Main-Class': MAIN_CLASS } } // 難読化fatjarを作成する task obFat(type: Jar, dependsOn: [obfuscation]) { // 難読化されていないクラスとリソースを削除しておく。難読化バージョンはOB_JARから取得される doFirst { delete 'build/classes' delete 'build/resources' } // 出力先ファイル指定 destinationDir = file('.') archiveName = OB_FAT // 入力ファイル from { (configurations.compile.collect { it } + file(OB_JAR)).collect { zipTree(it) } } with jar // マニフェスト manifest { attributes 'Implementation-Title': APP_NAME, 'Implementation-Version': version attributes 'Main-Class': MAIN_CLASS } } }}}