-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
application-kotlin example doesn't support using Kotlin imports #403
Comments
Hi @ellet0 ! The kotlin application example is set up to be processed as a "thin" jar. If you run
This is done with the line here:
This means that the standard library classes will be missing from the processed jar indeed; but you can run the jar with the
If you instead want to create a "fat" jar including the standard library, instead of using the shadowjar technique, you can change the
You'll then notice that the standard library jars are read as program jars, rather than library jars:
And then you'll get a "fat" jar in the end, which contains the Kotlin classes; so you can run the jar with
|
I would like to thank you for the detailed answer, while this solution might include the Kotlin standard library it still doesn't include the libraries, usually the jar will become too large after using libraries, is there a way to achieve that? I'm sure there are but still haven't figured that out, maybe at least an example with a popular library like Kotlinx Coroutines (it already has Proguard rules) but I always get errors as mentioned in the issue, and I still get warnings issue with no warnings and the only fix is to use I'm interested in knowing more about using Proguard with libraries in Kotlin/JVM (non-android project) Thank you once again! |
There might be a bug or something outdated in the application-kotlin example, I'm not sure if it's from my side but it is worth the trying
src/main/kotlin/Main.kt
with the following:it's important to import
Random
fromkotlin
and notjava.util
because all java imports (fromjava
) are already in the JRE, Kotlin imports are not and need to be included in the Jar file, the example doesn't use any Kotlin imports (fromkotlin
) or any other third party libraries which doesn't show the power or effect of minimizing the jar using Proguard3. This is why I suggest using Shadow Jar Gradle Plugin since it's the easiest way to include all the dependencies and it's the most used and commonly known, by default Shadow includes all the dependencies without minimizing the jar, which create 1.7MB for a Kotlin Hello world program even if it doesn't use any imports as it will use Kotlin standard library by default
4. Configure
proguard
task to take the Jar from Shadow Plugin and then reduce it, createproguard.pro
in the root project folder with the rules from (https://github.com/Guardsquare/proguard/blob/master/examples/application-kotlin/proguard.pro)5. Now try to run proguard task using
./gradlew proguard
, you will probably getwith no warnings, using
./gradlew shadowJar --info
will output too much info, see this link for the complete output to view it quicklyThe complete
build.gradle.kts
:The
proguard.pro
file:The failure mentioned above can be solved by adding
-ignorewarnings
to theproguard.pro
as a workaround which will solve the problem and still reduce the jar size with no issues (java -jar build/libs/application-minified.jar
will output something like:Hello World! -391866888
)But when including any library like
kotlinx-coroutines-core
orokhttp
and start using them or use something likesuspend
function keyword (using-keep class kotlin.Metadata
doesn't fix the task failure) you will get another errorAdding the rules of
kotlinx-coroutines-core
library from (https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/resources/META-INF/proguard/coroutines.pro) doesn't solve the issueI tried using Java JDK 11, 17, and even 21, macOS Homebrew, and Corretto, the latest version of Proguard (
7.4.2
)This issue might be in newer versions of Kotlin, Proguard or the example might be outdated, while this issue might be out of scope, I still think the example might need to be updated to support using the Kotlin imports, and maybe an example of using at least one popular Kotlin or java library in the example to showcase Proguard
even if the should should not use any third party libraries or even the ones from Kotlin, still using
suspend
or any Kotlin imports is not supported in the example and will cause build failure as mentioned aboveThank you.
The text was updated successfully, but these errors were encountered: