As part of migration from Android to KMP, I need t...
# multiplatform
m
As part of migration from Android to KMP, I need to migrate the check (within a KMP library) for whether the current build is debug build (in Android, simply
BuildConfig.DEBUG
). I’ve tried
BuildKonfig
but that doesn’t provide a good solution for this either. Now I’m looking at creating an Android-only shared module (for this as well other things like resource ids not handled by KMP) and adding that as a dependency to androidMain of the KMP library. I would need to add solutions for other platforms, but am happy to defer that until later. Is that Android dependency-based strategy good or is there a better solution?
z
If I understand your use case correctly, you could probably add an `expect`/`actual` function in your KMP module for this, and have the Android implementation return
BuildConfig.DEBUG
in
androidMain
.
m
I could only find a way for an Android-module to generate the
BuildConfig
. This is why I proposed the Android module dependency. Do you know of a way for KMP
androidMain
to be configured to build
BuildConfig
?
z
A KMP library is an Android library if you have an Android target configured, so you can have a
BuildConfig
generated there. I created this quick example, seems to work fine (implemented on Android, stubbed on iOS): https://github.com/zsmb13/BuildConfigExample250513/commit/6db2088c732bfe58ff5ffa83cda768def0d0da05
👍 1
m
Interesting, thanks. I created the KMP module using the latest Android Studio
New Module / Kotlin Multiplatform Shared Module
, which adds the plugin:
com.android.kotlin.multiplatform.library
and this cannot be used with
com.android.library
in the same module. I guess this is why you are able to generate the
BuildConfig
. For the KMP android library plugin it allows configuration via
kotlin {  androidLibrary { … } }
and it’s not clear to me how to convert that to the old Android way, or indeed whether I should go that route.
z
Oh, I see, I'm not sure if there's equivalent config in the new plugin yet. I'll try to find out!
thank you color 1
m
Is there documentation for migration from a KMP module using the
com.android.library
plugin to
com.android.kotlin.multiplatform.library
? EDIT: https://developer.android.com/kotlin/multiplatform/plugin
👌 1
Generally, the workaround of the KMP library module having a dependency on an Android-only module seems to be working. I use that Android-only module to store resources (for resource ids) as well as
AndroidManifest
(because I need to use
addManifestPlaceholders
which is unavailable in
androidLibrary
dsl). And for the
BuildConfig
as mentioned above. This is of course only a temporary solution as I continue to migrate the app to KMP. The one thing that isn’t working are unit tests that call code that reference resources. The tests (which are in
androidHostTest
) fail with:
java.lang.NoClassDefFoundError: foo/bar/R$string
z
Update: I got replies from the AGP folks, having a separate Android module with the AGP library plugin is really the way to do this with the new KMP Android plugin, as it doesn't offer variants (or a lot of other Android-specific features) on purpose.
I don't have any context for the test issue - I suggest opening a new thread or filing an issue somewhere for that part
m
Thanks Márton. I wonder why they don’t want to support variants. This is very important for me, although not for the module I’m currently migrating. Is there an alternative approach they recommend across platforms?
z
There's no built-in solution in KMP. We have an issue that tracks this request here that you can vote for / comment on with your use case: https://youtrack.jetbrains.com/issue/KT-33432/MPP-build-variant-feature
👍 1