From 56fab73305a768414a65d4cea83ffdd87dedb870 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Wed, 12 Jun 2019 10:50:57 +0200 Subject: [PATCH] Add shared kotlin multiplatform library --- Habitica/build.gradle | 2 + settings.gradle | 1 + shared/.gitignore | 1 + shared/build.gradle | 89 +++++++++++++++++++ .../shared/habitica/PlatformLogger.kt | 26 ++++++ .../com/habitrpg/shared/habitica/Logger.kt | 42 +++++++++ .../shared/habitica/PlatformLogger.kt | 22 +++++ shared/src/main/AndroidManifest.xml | 5 ++ 8 files changed, 188 insertions(+) create mode 100644 shared/.gitignore create mode 100644 shared/build.gradle create mode 100644 shared/src/androidMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt create mode 100644 shared/src/commonMain/kotlin/com/habitrpg/shared/habitica/Logger.kt create mode 100644 shared/src/iosMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt create mode 100644 shared/src/main/AndroidManifest.xml diff --git a/Habitica/build.gradle b/Habitica/build.gradle index 6d4c15acf..4eb6db49c 100644 --- a/Habitica/build.gradle +++ b/Habitica/build.gradle @@ -134,6 +134,8 @@ dependencies { implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0' implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0' implementation 'com.plattysoft.leonids:LeonidsLib:1.3.2' + + implementation project(':shared') } android { diff --git a/settings.gradle b/settings.gradle index 5f73326f0..60df51733 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ +include ':shared' include 'Habitica', ':Habitica', ':seeds-sdk' \ No newline at end of file diff --git a/shared/.gitignore b/shared/.gitignore new file mode 100644 index 000000000..796b96d1c --- /dev/null +++ b/shared/.gitignore @@ -0,0 +1 @@ +/build diff --git a/shared/build.gradle b/shared/build.gradle new file mode 100644 index 000000000..8fdad12ed --- /dev/null +++ b/shared/build.gradle @@ -0,0 +1,89 @@ +apply plugin: 'com.android.library' +apply plugin: 'kotlin-multiplatform' +apply plugin: 'kotlin-kapt' + +android { + compileSdkVersion 28 + + defaultConfig { + + minSdkVersion 21 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + } + } + sourceSets{ + main.java.srcDirs += 'src/androidMain/kotlin' + } + compileOptions { + targetCompatibility = '1.8' + } +} + +kotlin { + targets { + final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \ + ? presets.iosArm64 : presets.iosX64 + + fromPreset(iOSTarget, 'ios') { + binaries { + framework('shared') + } + } + + android() + } + + sourceSets { + commonMain.dependencies { + api 'org.jetbrains.kotlin:kotlin-stdlib-common' + } + + commonMain.kotlin.srcDirs += 'src/commonMain/kotlin' + + androidMain.dependencies { + api 'org.jetbrains.kotlin:kotlin-stdlib' + } + + iosMain.dependencies { + implementation "com.squareup.sqldelight:ios-driver:1.1.3" + } + + iosMain.kotlin.srcDirs += 'src/iosMain/kotlin' + } +} + +// workaround for https://youtrack.jetbrains.com/issue/KT-27170 +configurations { + compileClasspath +} + +task packForXCode(type: Sync) { + final File frameworkDir = new File(buildDir, "xcode-frameworks") + final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG' + final def framework = kotlin.targets.ios.binaries.getFramework("shared", mode) + + inputs.property "mode", mode + dependsOn framework.linkTask + + from { framework.outputFile.parentFile } + into frameworkDir + + doLast { + new File(frameworkDir, 'gradlew').with { + text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n" + setExecutable(true) + } + } +} +tasks.build.dependsOn packForXCode + +tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { + kotlinOptions { + jvmTarget = "1.8" + } +} \ No newline at end of file diff --git a/shared/src/androidMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt b/shared/src/androidMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt new file mode 100644 index 000000000..9f3b5cde5 --- /dev/null +++ b/shared/src/androidMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt @@ -0,0 +1,26 @@ +package com.habitrpg.shared.habitica + +import android.util.Log +import space.thelen.shared.cluetective.BuildConfig + +actual class PlatformLogger actual constructor() { + actual val enabled: Boolean + get() = BuildConfig.DEBUG + + actual fun logDebug(tag: String, message: String) { + Log.d(tag, message) + } + + actual fun logInfo(tag: String, message: String) { + Log.i(tag, message) + } + + actual fun logError(tag: String, message: String) { + Log.e(tag, message) + } + + actual fun logError(tag: String, message: String, exception: Throwable) { + Log.e(tag, message, exception) + } + +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/habitrpg/shared/habitica/Logger.kt b/shared/src/commonMain/kotlin/com/habitrpg/shared/habitica/Logger.kt new file mode 100644 index 000000000..912824958 --- /dev/null +++ b/shared/src/commonMain/kotlin/com/habitrpg/shared/habitica/Logger.kt @@ -0,0 +1,42 @@ +package com.habitrpg.shared.habitica + +expect class PlatformLogger() { + val enabled: Boolean + + fun logDebug(tag: String, message: String) + fun logInfo(tag: String, message: String) + fun logError(tag: String, message: String) + fun logError(tag: String, message: String, exception: Throwable) +} + +enum class LogLevel { + ERROR, INFO, DEBUG +} + +class Logger { + + companion object { + private val platformLogger = PlatformLogger() + + val enabled + get() = platformLogger.enabled + + fun log(level: LogLevel, tag: String, message: String) { + if (!enabled) return + when (level) { + LogLevel.ERROR -> platformLogger.logError(tag, message) + LogLevel.INFO -> platformLogger.logInfo(tag, message) + LogLevel.DEBUG -> platformLogger.logDebug(tag, message) + } + } + + fun logException(tag: String, message: String, exception: Throwable? = null) { + if (!enabled) return + exception?.let { + platformLogger.logError(tag, message, exception) + } ?: run { + platformLogger.logError(tag, message) + } + } + } +} \ No newline at end of file diff --git a/shared/src/iosMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt b/shared/src/iosMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt new file mode 100644 index 000000000..ea47be86d --- /dev/null +++ b/shared/src/iosMain/kotlin/com/habitrpg/shared/habitica/PlatformLogger.kt @@ -0,0 +1,22 @@ +package com.habitrpg.shared.habitica + +actual class PlatformLogger actual constructor() { + actual val enabled: Boolean + get() = true + + actual fun logDebug(tag: String, message: String) { + println("[DEBUG] $tag: $message") + } + + actual fun logInfo(tag: String, message: String) { + println("[INFO] $tag: $message") + } + + actual fun logError(tag: String, message: String) { + println("[ERROR] $tag: $message") + } + + actual fun logError(tag: String, message: String, exception: Throwable) { + println("[ERROR] $tag: $message\n${exception.getStackTrace().joinToString("\n")}") + } +} \ No newline at end of file diff --git a/shared/src/main/AndroidManifest.xml b/shared/src/main/AndroidManifest.xml new file mode 100644 index 000000000..5b1f56cda --- /dev/null +++ b/shared/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + +