https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

jean

07/16/2020, 7:47 AM
the hands-on tutorial use
jvm("android")
for targeting android but I’m reading in the doc that one could also use
android { ... }
. Is the
android
shortcut useful only to be able to configure build variants, dependencies, etc… ? The doc says also
id("com.android.library")
is necessary but gradle can’t resolve it
Copy code
Plugin [id: 'com.android.library'] was not found in any of the following sources:
(and then nothing, it does not say any sources). What if I want to target android and desktop through the jvm? should I just use
jvm()
?
g

gildor

07/16/2020, 8:11 AM
Have you add android gradle plugin to build script dependencies?
j

jean

07/16/2020, 8:13 AM
I’ve added this
Copy code
buildscript {
    repositories {
        mavenCentral()
        google()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.1.0-beta03")
    }
}
which makes
apply(plugin = "com.android.library")
works but still not for
id("com.android.library")
in the
plugins
bracket
l

louiscad

07/16/2020, 8:14 AM
Is the snippet you've shown in the root project's
build.gradle[.kts]
file?
j

jean

07/16/2020, 8:14 AM
yep
I guess it should since I want to be able to use the plugin in the root gradle file to be able to use
android()
target
l

louiscad

07/16/2020, 8:16 AM
I don't think it is possible, you most likely need to have a subproject for the android library.
j

jean

07/16/2020, 8:18 AM
ok so that would mean the mpp project only use the
jvm()
target and my android project using the shared code should declare a
Copy code
kotlin {
    android { ... }
}
?
l

louiscad

07/16/2020, 8:24 AM
FYI, if you want to share code between Android and JVM, it's currently best to make a plain Kotlin/JVM library module, and ensure the java APIs you use are available on Android (avoid desktop specific APIs). Then, in the desktop or android specific library/app module, you can use the platform specific APIs, and implement some interfaces if needed using these platform APIs.
☝️ 1
👌 1
j

jean

07/16/2020, 8:26 AM
I’ll keep that in mind, thanks!
l

louiscad

07/16/2020, 8:28 AM
Haha, you figured it doesn't explain the
jvm("android")
thing 😄
j

jean

07/16/2020, 8:34 AM
yeah no, I guess they could actually use it since their shared code module is inside the android project 🤷‍♂️
l

louiscad

07/16/2020, 8:38 AM
I think it was easier to test jvm support and fix any issues than android support which they don't control directly, and is more complicated on several levels. I'm hopeful this will improve, especially with 1.4 and 1.4.20
k

Kris Wong

07/16/2020, 1:19 PM
use an android target if you need to do any Android things, like use the Android SDK or configure flavors
otherwise use JVM
13 Views