edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.67k stars 269 forks source link

How to use proguard correctly with TornadoFx? #924

Open TioCoding opened 5 years ago

TioCoding commented 5 years ago

Hi, I'm trying to obfuscate my code with 'Proguard', but I'm having some problems trying to run my application after using Proguard. This is my configuration file:

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

-keepattributes Signature

-keep, allowobfuscation class pe.com.clario.clariodesktopapp.bean.**  { 
    <methods>;
}

-keep class pe.com.clario.clariodesktopapp.view.** {
    <methods>;
}

-keep class pe.com.clario.clariodesktopapp.ClarioDesktopApplication {
    <methods>;
}

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-dontwarn kotlin.**
-dontnote kotlin.**
-keepclassmembers class **$WhenMappings {
    <fields>;
}

-dontwarn org.jetbrains.annotations.**
-dontwarn tornadofx.**
-keep class tornadofx.** { *; }
-dontwarn javax.json.**

My classes are left this way:

ofuscate2

When executing the application I get this error

java.lang.AbstractMethodError: tornadofx.UIComponent.getRoot()Ljavafx/scene/Parent;
        at tornadofx.FXKt.addChildIfPossible(FX.kt:531)
        at tornadofx.FXKt.addChildIfPossible$default(FX.kt:473)
        at tornadofx.LayoutsKt.stackpane(Layouts.kt:399)
        at tornadofx.LayoutsKt.stackpane$default(Layouts.kt:119)

This error occurs because all classes and packages are renamed when using 'Proguard'. Any recommendations on how I can configure proguard with TornadoFx? Without problems?

I'm using JsonModel, and when I run the toJSON() method it also generates that error.

java.lang.AbstractMethodError: pe.com.clario.clariodesktopapp.a.a.toJSON(Ltornadofx/JsonBuilder;)V
        at tornadofx.JsonModel$DefaultImpls.toJSON(Json.kt:53)
        at pe.com.clario.clariodesktopapp.ClarioDesktopApplication.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

This is how the toJSON() method looks after being obfuscated. How can I make it work?

ofuscate

I'm a little new with 'Proguard', I hope you can help me, thank you very much.

edvin commented 5 years ago

I don't have any experience with ProGuard, but maybe the ProGuard guys can answer this easier? :)

rsglobal commented 5 years ago

Please check my one year old proguard task in build.gradle. I believe it will help.

task proguard(type: ProGuardTask, dependsOn: 'shadowJar') {
    injars "${buildDir}/libs/${project.tasks.shadowJar.archiveName}"
    outjars "${buildDir}/libs/dist.jar"

    libraryjars 'c:/Program Files/Java/jre1.8.0_151/lib/rt.jar'
    libraryjars 'c:/Program Files/Java/jre1.8.0_151/lib/ext/jfxrt.jar'

    adaptresourcefilenames '**.zip'

    dontwarn 'org.osgi.**'
    dontwarn 'org.apache.**'
    dontwarn 'tornadofx.**'
    dontwarn 'kotlin.reflect.**'
    keep 'class tornadofx..** { *; }'
    keep 'class org.osgi..** { *; }'
    keep 'class org.apache..** { *; }'
    keep 'class kotlin.reflect..** '
    keep 'class kotlin.text.** { *; } '
    keep 'class watchDog.main { *; }'

    keepclassmembernames 'class kotlinx.** { volatile <fields>; }'

    keepclasseswithmembernames includedescriptorclasses: true, 'class com.fazecast.** { \
        native <methods>; \
    }'

    keepclasseswithmembernames includedescriptorclasses: true, 'class com.fazecast.** { \
        private volatile <fields>; \
    }'

    printmapping 'proguard.map'
}