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

ribesg

01/25/2019, 2:54 PM
I’m trying to write an MPP Android & iOS Kotlin project from scratch. Do you really need to have the “main”
android {…}
block in the top build.gradle file? EDIT: see thread
Maybe I misunderstood how this works and the root project should be basically empty
k

kgonyon

01/25/2019, 3:16 PM
Yes, mpp requires either the com.android.library or com.android.application plugins which each require the android block. See https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#android-support for more information.
r

ribesg

01/25/2019, 3:44 PM
The documentation provides Gradle DSL code but never states which Gradle file we’re talking about, so that does not help
Right now my root project build.gradle.kts file is completely empty, it looks like it’s how it’s supposed to work
k

kgonyon

01/25/2019, 3:47 PM
You want to place the android and MPP Gradle DSL in any submodule you want to be a mpp project. So if you have a submodule that is supposed to be your app, you would want to create a build.gradle.kts file in that submodule with the mpp plugin.
If you can post your project structure I can better help you.
r

ribesg

01/25/2019, 3:59 PM
Ok so I found another bunch of example and now I think I understand that you’re supposed to have a root project and a subproject with kotlin-multiplatform and all your platforms. Is that correct?
Why do you need a submodule? Can’t you do it all with a single Gradle project then?
k

kgonyon

01/25/2019, 4:05 PM
You can do it in your root project. If that is what you are aiming for then you can place the needed gradle dsl in your root build.gradle.kts
Copy code
buildscript {
    repositories {
        google()
        maven { url '<https://dl.bintray.com/kotlin/kotlin-eap>' }
        maven { url '<https://plugins.gradle.org/m2/>' }
        maven { url '<https://dl.bintray.com/jetbrains/kotlin-native-dependencies>' }
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}

repositories {
    google()
    maven { url '<https://dl.bintray.com/kotlin/kotlin-eap>' }
    maven { url "<https://kotlin.bintray.com/kotlinx>" }
    mavenCentral()
    jcenter()
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
apply plugin: 'maven-publish'



android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "0.0.1"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), '<http://proguard-rules.pro|proguard-rules.pro>'
        }
    }
}
Copy code
kotlin {
    android()

    // This is for iPhone emulator
    // Switch here to iosArm64 (or iosArm32) to build library for iPhone device
    iosArm64("ios") {
        compilations.main {
            outputKinds("framework")

    }
    sourceSets {
        commonMain {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialization_version"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
                implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
            }
        }
        commonTest {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
                implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
            }
        }
        iosMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$kotlin_coroutines_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$kotlin_serialization_version"
            }
        }
        iosTest {
        }
    }
}
That is an example from my project. That is all in the root build.gradle file.
r

ribesg

01/25/2019, 4:09 PM
Is this project a 100% Kotlin app? I only saw example projects which were either 100% Kotlin MPP libraries or MPP Apps with the iOS side in Swift
k

kgonyon

01/25/2019, 4:10 PM
This project is 100% Kotlin library that gets compiled and included into existing iOS app in swift and a separate android app in Kotlin.
r

ribesg

01/25/2019, 4:14 PM
Yeah, that’s what I usually see. I don’t want to make a library (not yet and I have good examples for that), I want to make an app, written only in Kotlin
k

kgonyon

01/25/2019, 4:18 PM
I see. I am not fully certain how you would setup the iOS app to use Kotlin. Perhaps someone else has an example of this.
r

ribesg

01/25/2019, 4:19 PM
The closest thing to what I want that I found ios-wise is this https://github.com/JetBrains/kotlinconf-spinner/tree/master/clients/ios
You can see that the XCode project contains no code
My idea is to build a MPP project with the Android & common parts working together then adding iOS from there, I think I’ll need to do some custom things like in the example I just linked as the support isn’t huge yet
Thanks for your help!
2 Views