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

Gunslingor

04/06/2020, 8:33 PM
I'm in aw of how powerful it could be... but its confusing. Like the Kotlin Multiplatform plugin creates this template... why has it proven so difficult to add android and iOS as source sets? Its as if Multiplatform plugin doesn't have them or something.
this should work I suspect, can't get it working after 16 hours =(
When I control clickmy android line it take me to a real function... so I have no idea.
Copy code
fun android(
    name: String = "android",
    configure: KotlinAndroidTarget.() -> Unit = { }
): KotlinAndroidTarget =
    configureOrCreate(
        name,
        presets.getByName("android") as KotlinAndroidTargetPreset,
        configure
    )
r

russhwolf

04/06/2020, 8:50 PM
You need to add some extra setup for Android. Apply the
com.android.library
plugin, add a top-level
android{}
block with sdk version config, add a manifest file.
g

Gunslingor

04/06/2020, 8:50 PM
Like this right?
Copy code
plugins {
    java
    id("com.android.application")
    kotlin("android")
    kotlin("multiplatform") version "1.3.71"
}
r

russhwolf

04/06/2020, 8:50 PM
yeah
g

Gunslingor

04/06/2020, 8:51 PM
Plugin [id: 'com.android.application'] was not found in any of the following sources: BLANK
Copy code
repositories {
    mavenCentral()
    jcenter()
    jcenter {
        url = uri("<https://kotlin.bintray.com/kotlin-js-wrappers>")
    }
    maven {
        url = uri("<https://jitpack.io>")
    }
    google()
}
r

russhwolf

04/06/2020, 8:52 PM
Where's that repositories block? Needs to be either in
buildSrc
of a parent build.gradle, or in
pluginManagement
of settings.gradle
g

Gunslingor

04/06/2020, 8:53 PM
its in the top level build.gradle.kt file above the kotlin {...}
what I am really trying to setup
When I remove this android stuff from the top level gradle config, IntelliJ can import the project but androidstudio cant. Additionally I noticed wierd incomplete stuff both JB Gradle plugins like it isn't really working. You can see that in the bellow, missing stuff, wierd src folder there (or is this normal?). You can also see I have no problem loading the other android Native as a source set
It feels to me like the kotlin multiplatform plugin breaks the android plugins
Though I read the android plug in was obsolete... sorry man, brain hurts.
r

russhwolf

04/06/2020, 9:04 PM
That article is two years old so without reading it I can promise you it gets some details wrong
g

Gunslingor

04/06/2020, 9:04 PM
Its really the only one I could find that really tried to explain it.
I've read so much trying to fix this, lol
r

russhwolf

04/06/2020, 9:07 PM
We've been maintaining this sample at Touchlab that might be helpful for config: https://github.com/touchlab/KaMPKit
I also did this other hello world sample a little while back that hits all your platforms: https://github.com/russhwolf/multiplatform-hello
The Android Gradle plugin does a lot of weird things, so it behaves a little differently than other platforms, but works pretty well with the multiplatform plugin these days
That wasn't always the case a year or two ago though
g

Gunslingor

04/06/2020, 9:10 PM
ok, i'm starting to think the google() repo is being blocked on my internet... why I can't even download the those plugins I suspect. I suspect I'm gonna want to avoid android studio and develop in intellij
r

russhwolf

04/06/2020, 9:12 PM
Check if you can build KaMPKit. It has a pretty good out-of-the-box configuration. If that doesn't work, maybe it's your internet. If it does then there's something wrong with your configuration
g

Gunslingor

04/06/2020, 9:12 PM
alright, I'll give that a try.. thanks for the extra gobys I never saw those.
r

russhwolf

04/06/2020, 9:13 PM
Also if you want more up-to-date tutorials there's some codelabs that Jetbrains is maintaining now https://play.kotlinlang.org/hands-on/overview
g

Gunslingor

04/06/2020, 9:14 PM
Ah yes, been thru many already... still need to learn cooroutines
Noticing that your project doesn't use the multiplatform plugin and also it doesn't use custom source sets. Actually... looking at it, I'm not seeing a JS or JVM component either so its pure iOS/Android right? I think the difference shows my problem; I can use the iOS/android templates fine but consider JS front end or client as well and JVM/server is part of the project too so I want one git and one way to kick it all off.
oh, yeah it does under shared
Alright, I'll study this overnight and shut up 😃
r

russhwolf

04/06/2020, 9:25 PM
The other project has jvm and js if that helps
s

Sebastien Leclerc Lavallee

04/07/2020, 12:12 AM
The KampKit is time saver. To start from scratch or check how something is done or test something. It’s really great!
g

Gunslingor

04/07/2020, 12:13 AM
Ok great.. question, do I have to have the android app in that app folder?
s

Sebastien Leclerc Lavallee

04/07/2020, 12:14 AM
I’ve seen some example using another folder but more than often the android app is inside the app folder. Like a convention
g

Gunslingor

04/07/2020, 12:18 AM
Ok, thanks... yeah I'm not a fan of mashing these conventions together... every target should get its aptly named folders... js/jvm/ios/android... without this basic ability to organize architectures I'd be losing important abilities.
s

Sebastien Leclerc Lavallee

04/07/2020, 12:24 AM
There are 2 things here. Code specific to the Android app will be under the app folder and shared code with some Android specific will be under something like ´shared/src/androidMain’
g

Gunslingor

04/07/2020, 12:37 AM
Yeah, trying to setup a complete project structure like this. I hate having these spurious files and folders liying around... even the .idea and .gradle folders pissed me off for a while... thank fully they start with a dot so stay out of my way up top... but when I'm programming for any target I want to either stay in that targets srcDir the entire time or the common folder.... Maybe the it sounds like the app and iOS folders should go in the build folder? I don't really understand why they are need in addition to the androidMain and iOSMain folders
I have a sneaking suspicion if I moved the android folder to project root, renamed it app and jiggled everything it would work much easier... but I hate compromise the design of a larger system for the conventions to a piece maker... Make conventions standard across all mfrs or they simply aren't standards... they are company conventions that, often, are designed to rope you into buying more shit... like apples proprietary USB "Standard"... $40 USB cables, yippy!
s

Sebastien Leclerc Lavallee

04/07/2020, 12:44 AM
The project setup you show me doesn’t have any iOS or Android app. Only shared framework that will be used in some other apps.
Better have a look à the Kampkit project.
g

Gunslingor

04/07/2020, 12:48 AM
I dont understand why the folders are needed, cant an entire simple hello world android app be built and gradle automated with all code inside the src/android folder and all output inside the build folder?
I understand it has something to do with an android devs conventions and tooling... but android devs arent doing what I'm doing, android dev is 1/5 of the project, android shouldnt have any say on my root project strucuture, but if it must it can dictate what goes in the android folder... I hate laking full control of my software.
s

Sebastien Leclerc Lavallee

04/07/2020, 12:53 AM
It’s really to have the shared code and the Android app in 2 folders. To separate everything. It’s like you have 3 projects inside the same repo. One Android app, one iOS app and a shared library
g

Gunslingor

04/07/2020, 1:03 AM
Okay, that's what I thought... it just makes no sense so my engineering brain cant accept it...lol
Then I guess the shared library has sub projects of ALL targets and shares code with common... I guess...
Curious question for 6 months down the road when I start deep into ios... will it be possible to write everything for ios in kotlin or is that just a fantasy?
So basically what I've been trying to build is the shared library with the app folder renamed android and shoved into the shared folder without a shared android part.
Lol, still feel that should be a workable setup with multiplatform plugin...
s

Sebastien Leclerc Lavallee

04/07/2020, 1:08 AM
Kotlin multiplatform is not for all UI related stuff
g

Gunslingor

04/07/2020, 1:10 AM
Hmmm... that makes sense I guess... okay then... I'll rework everything and trust in the system...
s

Sebastien Leclerc Lavallee

04/07/2020, 1:11 AM
I did a personal project using KMP and working on a new work project with it
Everything is working good
g

Gunslingor

04/07/2020, 4:36 AM
I don't know man, this seem impossible. Can't seem to find anything when I start trying to bring android into this project. I keep rolling back to when my project was just js/jvm/common and then trying again to merge in android stuff... over and over again I keep failing... same kind of basic errors, can't find anything. Using default examples and modernizing when needed. Is it possible that js/jvm and android need different versions of gradle or something?
s

Sebastien Leclerc Lavallee

04/07/2020, 1:59 PM
That error comes from the root
build.gradle.kts
and I think it’s not where you want to add
implementation
g

Gunslingor

04/07/2020, 4:26 PM
I upgraded to gradle 6.3, no idea how I was running on 5.2 something... you all are running 5.6.2 apparently. Also I had to comment out the clean task and I remove the android and android-extension plugin from this app build file.
Copy code
plugins {
    id("com.android.application")
    kotlin("android")
    kotlin("android.extensions")
}
After doing all that the below screenshot is what I ended up with... everything looks pretty solid to me except the app build file disappeared, something automatically deleted it... will try to add that back now. Okay, I added the example gradle app build file to my project, I know it won't work but I am just testing the plug black because that's what is causing this. first thing I noticed is the:
Copy code
id("com.android.application")
seems to be importing ok, which is great it wasn't before. But it keeps failing on this common error I saw before implying the android and, as previously, android-extensions is failing to download:
Plugin [id: 'org.jetbrains.kotlin.android'] was not found in any of the following sources:
And yet, I have app and shared tasks in my gradle ingtellij plugin, but only when I remove the app gradle file with nothing but... yeah, okay, app everything works when the app file has nothing but
Copy code
plugins {
    id("com.android.application")
}
But fails when it has:
Copy code
plugins {
    id("com.android.application")
    kotlin("android")
}

https://files.slack.com/files-pri/T09229ZC6-F011G7VLQ6M/image.png

Okay it definitely is that fuckin android and android-extensions plug in that seems to be causing all this knightmare... I don't understand how it works in your project or why my project doesnt appear to need it.

https://files.slack.com/files-pri/T09229ZC6-F01197SQCN6/image.png

I thought I had read that the android gradle plugin was obsolete somewhere... I dont know any more, happy I made progress, sad I did it while tallking to myself but hey, its 430AM what can I expect 😃 ... Night Slackers!
Gunslingor  [4:54 AM] Anyone know why this example created an extra buildSrc module instead of putting that in shared/common?
Also wondering if there is a way to remove all these iml files that suddenly started appearing with this setup, presently I've been dealing with this too: https://kotlinlang.slack.com/archives/C3PQML5NU/p1586279154292100
There doesn't appear to be a way to set this, why they created the main I suspect. I don't understand this... a build system that doesn't let me decide where I can place my files this is driving me mad... Becoming disenchanted with gradle, multiplatform and android has its own thing in quarentene.
s

Sebastien Leclerc Lavallee

04/07/2020, 6:03 PM
IML files are necessary for IntelliJ. It’s possible to hide them I think. Check in the options
g

Gunslingor

04/07/2020, 6:07 PM
Yeah, I think I'm going to have to set this up as two isolated projects... js/jvm/common and ios/android/common, those are the only templates that make sense to me and every method I've studied for combining em seems rube goldberg-ish
I think Intellij has a setting where the iml files are stored in the idea folder or a backend DB or something... didn't need them till... yeah, here it is: https://www.jetbrains.com/help/idea/maven-importing.html?_ga=2.191433642.1007620087.1585783643-156223597.1585440891
See what I mean, an iOS/Android Kotlin project lets you set the manifest file but a multiplatform does not; also I dont think the jvm/js and android/ios templates even require local iml files. https://medium.com/@suparn/kotlin-multiplatform-library-for-android-and-ios-9a46585a5c7
s

Sebastien Leclerc Lavallee

04/07/2020, 6:20 PM
This is only a library for iOS and Android. It does not contains app. And starting from here, it’s pretty easy to add JS
g

Gunslingor

04/07/2020, 6:28 PM
ah, your saying that is only for making librarsies... okay that makes sense. I still think I shouldnt have to put the manifest files in main, in androidMain instead and I should be able to store imls externally like before... and I think I should be able to move the iOS and Android folders inside the Main folders... move everything in shared folder up a level and delete shared, move build script into common... I'd be happier with that structure. shared is for making libraries? I guess that makes sense, and main is needed to run it locally, makes wierd and ugly sense I guess... its all just so dam ugly and inflexible
I just know in my head the exact structure I want... really thought/hoped gradle/multiplatform would help me achieve it =(
s

Sebastien Leclerc Lavallee

04/07/2020, 6:32 PM
The manifest is required if you want the library to be used in Android app. And iOSMain et androidMain folders are mostly used to store
actual
declarations
g

Gunslingor

04/07/2020, 6:34 PM
All makes sense... just ugly compared to my impossible vision... I'll proceed with the template, trying to merge it into my project and understand as I go
https://medium.com/icerock/how-to-start-use-kotlin-multiplatform-for-mobile-development-1d3022742178 There is a way move the manifest file to the androidMain and not deal with the separate main. I think the lesson here, at least for me, is that the android gradle plugin is still needed for multiplatform builds, yet the two are completely independant and unaware of each other... which kinda sucks, lol, but hey, my obsessive compulsiveness is satisfied!