Skip to content

Instantly share code, notes, and snippets.

@damienstanton
Created August 23, 2019 20:49
Show Gist options
  • Select an option

  • Save damienstanton/ae48e1ca9264122e37e00426f8ff8ab0 to your computer and use it in GitHub Desktop.

Select an option

Save damienstanton/ae48e1ca9264122e37e00426f8ff8ab0 to your computer and use it in GitHub Desktop.
Kotlin cross platform minimal example
package com.github.jeremyrempel.unsplash
import android.util.Log
actual fun log(level: LogLevel, tag: String, message: String, error: Throwable) {
when (level) {
is LogLevel.DEBUG -> Log.d(tag, message, error)
is LogLevel.INFO -> Log.i(tag, message, error)
is LogLevel.WARN -> Log.w(tag, message, error)
is LogLevel.ERROR -> Log.e(tag, message, error)
}
}
actual fun log(level: LogLevel, tag: String, message: String) {
when (level) {
is LogLevel.DEBUG -> Log.d(tag, message)
is LogLevel.INFO -> Log.i(tag, message)
is LogLevel.WARN -> Log.w(tag, message)
is LogLevel.ERROR -> Log.e(tag, message)
}
}
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
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
}
}
packagingOptions {
exclude 'META-INF/*.kotlin_module'
}
}
kotlin {
targets {
fromPreset(presets.android, 'android')
// This preset is for iPhone emulator
// Switch here to presets.iosArm64 (or iosArm32) to build library for iPhone device
fromPreset(presets.iosX64, 'ios') {
compilations.main.outputKinds('FRAMEWORK')
}
}
sourceSets {
configure([androidMain]) {
dependsOn commonMain
}
configure([iosMain]) {
dependsOn commonMain
}
// configure([ios64Main]) {
// dependsOn iosMain
// }
}
}
dependencies {
commonMainImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
commonMainImplementation "io.ktor:ktor-client-core:$ktor_version"
commonMainImplementation "io.ktor:ktor-client-json:$ktor_version"
commonMainImplementation "org.kodein.di:kodein-di-erased:$kodein_version"
androidMainImplementation fileTree(dir: 'libs', include: ['*.jar'])
androidMainImplementation 'com.android.support:appcompat-v7:28.0.0'
androidMainImplementation 'com.android.support.constraint:constraint-layout:1.1.3'
androidMainImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
androidMainImplementation "io.ktor:ktor-client-android:$ktor_version"
androidMainImplementation "io.ktor:ktor-client-json-jvm:$ktor_version"
androidMainImplementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
androidMainImplementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
iosMainImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
iosMainImplementation "io.ktor:ktor-client-ios:$ktor_version"
iosMainImplementation "io.ktor:ktor-client-json-native:$ktor_version"
iosMainImplementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
commonTestImplementation 'org.jetbrains.kotlin:kotlin-test'
commonTestImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
// 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 "link${buildType.toLowerCase().capitalize()}Framework${target.capitalize()}"
doLast {
def srcFile = kotlin.targets."$target".compilations.main.getBinary("FRAMEWORK", buildType)
def targetDir = getProperty("configuration.build.dir")
copy {
from srcFile.parent
into targetDir
include 'app.framework/**'
include 'app.framework.dSYM'
}
}
}
buildscript {
repositories {
maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
maven { url "https://dl.bintray.com/kotlin/kotlin-dev/" }
google()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "com.android.tools.build:gradle:$gradle_android_version"
}
}
allprojects {
repositories {
maven { url "https://kotlin.bintray.com/kotlinx" }
maven { url "https://kotlin.bintray.com/ktor" }
google()
jcenter()
}
}
package com.github.jeremyrempel.unsplash
sealed class LogLevel {
object DEBUG : LogLevel()
object INFO : LogLevel()
object WARN : LogLevel()
object ERROR : LogLevel()
}
expect fun log(level: LogLevel, tag: String, message: String)
expect fun log(level: LogLevel, tag: String, message: String, error: Throwable)
package com.github.jeremyrempel.unsplash
import platform.Foundation.NSLog
actual fun log(level: LogLevel, tag: String, message: String, error: Throwable) =
NSLog("[$level]: ($tag), $message, $error")
actual fun log(level: LogLevel, tag: String, message: String) = NSLog("[$level]: ($tag)")
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "kotlin-multiplatform") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
}
}
}
}
rootProject.name = 'multitest'
enableFeaturePreview('GRADLE_METADATA')
include ':app'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment