Does anyone know how to declare a KMP library modu...
# multiplatform
m
Does anyone know how to declare a KMP library module proguard file? All the examples I’ve seen use an
android { }
block to specify the
consumerProguardFiles
but that’s not applicable to a KMP library module. EDIT: Is this relevant? https://developer.android.com/reference/tools/gradle-api/8.3/null/com/android/build/api/dsl/KmpOptimization EDIT2: I tried this but it doesn’t seem to help:
Copy code
kotlin {
    android {
        optimization {
            val file = project.file("proguard-rules.pro")
            println("proguard: " + file.canonicalPath) // check it's the right location
            consumerKeepRules.files.add(file)
        }
    }
SOLVED:
Copy code
androidLibrary {
    optimization {
        consumerKeepRules.publish = true
        consumerKeepRules.files.add(project.file("proguard-rules.pro"))
    }
}
z
Is this with the new KMP Android library plugin?
t
IIRC, KMP does not currently support Proguard for all targets– or really any outside of Android. Proguard runs on compiled class files to produce new modified class files. So, it can be applied to JVM code as well, but that isn’t supported out of the box. You’d need to apply the guardsquare plugin yourself. If you already know all that, did you mean to use
keepRules
instead of
consumerKeepRules
?
It would be pretty nifty to have a KSP plugin that could read in proguard configs and do the same work on the IR… 🤔
Without waiting for a brand new tool, this article (from last year) looks like it’s promising: https://medium.com/@dfs.techblog/code-obfuscations-in-kotlin-multiplatform-kmp-494d2cd0a416
m
@zsmb I believe so. How can I verify it’s being used?
Note: I’m only using Android target at the moment - using Android Studio “Generate Signed App Bundle or APK…” to build the release APKs
z
You can check if you're adding the target with
androidLibrary
(new KMP Android plugin) or
androidTarget
(old general Android library plugin)
Or you can check which plugin you're applying, the new one is
com.android.kotlin.multiplatform.library
m
I’m using
androidLibrary { }
and
Copy code
plugins {
    alias(libs.plugins.kotlin.multiplatform)
    alias(libs.plugins.android.kotlin.multiplatform.library)
    alias(libs.plugins.compose.compiler)
    alias(libs.plugins.compose.multiplatform)
}
z
I'm not sure if this new plugin supports Proguard config (yet?), its feature set is more limited than the regular Android plugin. cc @Madalin Valceleanu @Hakan Mehmed from the AGP side
m
In my case, since it’s only one rule, I just move it to the android app module so it can work for now.
👍 1
I guess it’s quite important since there is no @Keep annotation in KMP right?
z
Actually, I think you're in the right spot with the
optimization
API, should be used here
Copy code
kotlin {
    androidLibrary {
        optimization {
            consumerKeepRules
        }
    }
}
I don't know what the exact syntax is for adding files to these rules 😄
@Keep
indeed is JVM/Android only, but it seems like it could be a common annotation that's just ignored on other platforms, worth filing an issue for probably https://developer.android.com/reference/kotlin/androidx/annotation/Keep
m
Thanks @zsmb, so I changed
android
to
androidLibrary
(which makes more sense) but that didn’t help unfortunately. But then I added:
Copy code
consumerKeepRules.publish = true
and voila! Thanks to the inspiration from this link: https://android.googlesource.com/platform/frameworks/support/+/4e7d49d349e86d337aa6f1411bff30d7143fc729%5E%21/
nice spin 1
🎉 1
Should that issue be filed with Jetbrains or Android issuetracker?
z
Both the Keep annotation and any issues around this new plugin are for the Google tracker
👍 1
m
Hmm, I’m not seeing a relevant component, or just the Android Gradle Plugin?
z
thank you color 1
For the annotation that is. The plugin would be AGP (or AS, which I think is where you can create issues)