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

Nikolai

07/16/2019, 12:46 AM
Hi, everyone I have a question about multiplatform project structure. In IDEA we have two main patterns mobile shared library and mobile android/ios. For library I understand it. We have app.framework for iOS and jar file for android. As for Mobile Android/iOS pattern I don't have clear picture. We still have framework for iOS, but how common module code applied to android? I saw in build.gradle that we use task
Copy code
kotlin {
    android("android")
    ....
}
But I don't understand what exactly it doing, what means "android" as parameter, what we have as output? And as result how common module code goes to android part? P.S. Also, is where any place I can found docs to understand that closures have this plugin (kotlin-multiplatform), plugin tasks, what they do and how they works?
r

ribesg

07/16/2019, 8:34 AM
I’m not using the default project created by idea, but the small block of code you have here defines an Android target for the multiplatform plugin. The parameter is the name of the target (and is also the default name so I have no idea why it’s there). This creates another sourceSet in addition to common in which you can write Android code. From there you can create an Android library (aar) that you would use in your Android app
k

Kris Wong

07/16/2019, 12:57 PM
it allows you to use the Android SDK in your Android source set
vs. pure Java
n

Nikolai

07/16/2019, 2:14 PM
@ribesg, @Kris Wong Thanks a lot. Can you, please, explain where did you get this info, in official docs about multiplatform projects? Or kotlin-multiplatform plugin have documentation? Seems like am getting closer to understanding. I will provide full content of the build.gradle below, so you can see it and maybe correct me. So, this command create new source set and allow me to use androidSDK inside this source set. Because, I apply :
Copy code
apply plugin: 'com.android.application'
in same build.gradle plugin able to build a android app from this source set using all android.application closures ('android', 'dependencies'). The only question is how common module applied to this source set. As separate library or as part of the codebase or something else?
Copy code
plugins {
    id 'kotlin-multiplatform' version '1.3.30'
}
repositories {
    google()
    jcenter()
    mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId 'org.jetbrains.kotlin.mpp_app_android'
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName '1.0'
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

kotlin {
    android("android")
    // This is for iPhone emulator
    // Switch here to iosArm64 (or iosArm32) to build library for iPhone device
    iosX64("ios") {
        binaries {
            framework()
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
            }
        }
        commonTest {
            dependencies {
        		implementation kotlin('test-common')
        		implementation kotlin('test-annotations-common')
            }
        }
        androidMain {
            dependencies {
                implementation kotlin('stdlib')
            }
        }
        androidTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        iosMain {
        }
        iosTest {
        }
    }
}

// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
    def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
    def target = project.findProperty('kotlin.target') ?: 'ios'
    dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask

    doLast {
        def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
        def targetDir = getProperty('configuration.build.dir')
        copy {
            from srcFile.parent
            into targetDir
            include 'app.framework/**'
            include 'app.framework.dSYM'
        }
    }
}
r

ribesg

07/16/2019, 2:15 PM
Don’t use both the
plugins { }
block and
apply plugin
, unless you know that you need to.
apply plugin
is the old way of doing it
Most of my knowledge comes from months of reading documentations, examples and trying things with the help of the IDE.
n

Nikolai

07/16/2019, 2:17 PM
Yes, same for me, but I can add help developers from Slack channels as well 👍
k

Kris Wong

07/16/2019, 2:21 PM
this is not for building an android app. it's for building an android library.
your library will be consumed, in theory, in an android app
MPP project targets are libraries
n

Nikolai

07/16/2019, 2:36 PM
Ok, thanks
6 Views