From 9adc93f04ba5b3c3cb0938d35d70db12311d042a Mon Sep 17 00:00:00 2001 From: theapache64 Date: Mon, 5 Sep 2022 16:01:42 +0530 Subject: [PATCH] add android sample for falling balls mpp (#2275) Co-authored-by: theapache64 --- .../falling-balls-mpp/.idea/.gitignore | 8 ++++ ...falling_balls_mpp_desktop_1_0_SNAPSHOT.xml | 8 ++++ .../falling_balls_mpp_js_1_0_SNAPSHOT.xml | 8 ++++ .../falling-balls-mpp/.idea/compiler.xml | 6 +++ .../falling-balls-mpp/.idea/gradle.xml | 19 ++++++++ .../.idea/jarRepositories.xml | 45 +++++++++++++++++++ .../examples/falling-balls-mpp/.idea/misc.xml | 5 +++ .../examples/falling-balls-mpp/.idea/vcs.xml | 7 +++ .../falling-balls-mpp/build.gradle.kts | 32 +++++++++++++ .../falling-balls-mpp/gradle.properties | 2 + .../falling-balls-mpp/settings.gradle.kts | 4 ++ .../src/androidMain/AndroidManifest.xml | 21 +++++++++ .../jetbrains/fallingballs/MainActivity.kt | 23 ++++++++++ .../src/androidMain/res/values/strings.xml | 3 ++ .../kotlin/fallingBalls/FallingBalls.kt | 12 +++-- 15 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 experimental/examples/falling-balls-mpp/.idea/.gitignore create mode 100644 experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_desktop_1_0_SNAPSHOT.xml create mode 100644 experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_js_1_0_SNAPSHOT.xml create mode 100644 experimental/examples/falling-balls-mpp/.idea/compiler.xml create mode 100644 experimental/examples/falling-balls-mpp/.idea/gradle.xml create mode 100644 experimental/examples/falling-balls-mpp/.idea/jarRepositories.xml create mode 100644 experimental/examples/falling-balls-mpp/.idea/misc.xml create mode 100644 experimental/examples/falling-balls-mpp/.idea/vcs.xml create mode 100644 experimental/examples/falling-balls-mpp/src/androidMain/AndroidManifest.xml create mode 100644 experimental/examples/falling-balls-mpp/src/androidMain/kotlin/org/jetbrains/fallingballs/MainActivity.kt create mode 100644 experimental/examples/falling-balls-mpp/src/androidMain/res/values/strings.xml diff --git a/experimental/examples/falling-balls-mpp/.idea/.gitignore b/experimental/examples/falling-balls-mpp/.idea/.gitignore new file mode 100644 index 0000000000..13566b81b0 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_desktop_1_0_SNAPSHOT.xml b/experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_desktop_1_0_SNAPSHOT.xml new file mode 100644 index 0000000000..59061987c5 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_desktop_1_0_SNAPSHOT.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/build/libs + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_js_1_0_SNAPSHOT.xml b/experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_js_1_0_SNAPSHOT.xml new file mode 100644 index 0000000000..f6bafb49b8 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/artifacts/falling_balls_mpp_js_1_0_SNAPSHOT.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/build/libs + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/.idea/compiler.xml b/experimental/examples/falling-balls-mpp/.idea/compiler.xml new file mode 100644 index 0000000000..b73660a625 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/.idea/gradle.xml b/experimental/examples/falling-balls-mpp/.idea/gradle.xml new file mode 100644 index 0000000000..b47d25fae1 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/.idea/jarRepositories.xml b/experimental/examples/falling-balls-mpp/.idea/jarRepositories.xml new file mode 100644 index 0000000000..bc6a72eadf --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/jarRepositories.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/.idea/misc.xml b/experimental/examples/falling-balls-mpp/.idea/misc.xml new file mode 100644 index 0000000000..50c0927c79 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/.idea/vcs.xml b/experimental/examples/falling-balls-mpp/.idea/vcs.xml new file mode 100644 index 0000000000..ccc96c563b --- /dev/null +++ b/experimental/examples/falling-balls-mpp/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/build.gradle.kts b/experimental/examples/falling-balls-mpp/build.gradle.kts index c5a80129cf..01b05ff5e4 100644 --- a/experimental/examples/falling-balls-mpp/build.gradle.kts +++ b/experimental/examples/falling-balls-mpp/build.gradle.kts @@ -15,6 +15,7 @@ buildscript { } plugins { + id("com.android.application") kotlin("multiplatform") id("org.jetbrains.compose") } @@ -29,6 +30,7 @@ repositories { google()} kotlin { + android() jvm("desktop") js(IR) { browser() @@ -98,6 +100,15 @@ kotlin { } } + val androidMain by getting { + dependsOn(commonMain) + kotlin.srcDirs("src/jvmMain/kotlin") + dependencies { + api("androidx.appcompat:appcompat:1.4.1") + implementation("androidx.activity:activity-compose:1.4.0") + } + } + val desktopMain by getting { dependencies { implementation(compose.desktop.currentOs) @@ -210,3 +221,24 @@ project.tasks.withType(org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile::class.ja "-Xir-dce-runtime-diagnostic=log" ) } + +android { + compileSdk = 31 + + defaultConfig { + minSdk = 21 + targetSdk = 31 + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + sourceSets { + named("main") { + manifest.srcFile("src/androidMain/AndroidManifest.xml") + res.srcDirs("src/androidMain/res", "src/commonMain/resources") + } + } +} diff --git a/experimental/examples/falling-balls-mpp/gradle.properties b/experimental/examples/falling-balls-mpp/gradle.properties index df22ef282c..702363f570 100644 --- a/experimental/examples/falling-balls-mpp/gradle.properties +++ b/experimental/examples/falling-balls-mpp/gradle.properties @@ -9,3 +9,5 @@ kotlin.mpp.enableGranularSourceSetsMetadata=true # Enable kotlin/native experimental memory model kotlin.native.binary.memoryModel=experimental compose.desktop.verbose=true +agp.version=7.0.4 +android.useAndroidX=true diff --git a/experimental/examples/falling-balls-mpp/settings.gradle.kts b/experimental/examples/falling-balls-mpp/settings.gradle.kts index 17b62c9a6c..80673ec582 100644 --- a/experimental/examples/falling-balls-mpp/settings.gradle.kts +++ b/experimental/examples/falling-balls-mpp/settings.gradle.kts @@ -10,6 +10,10 @@ pluginManagement { plugins { val kotlinVersion = extra["kotlin.version"] as String kotlin("multiplatform").version(kotlinVersion) + kotlin("android").version(kotlinVersion) + + val agpVersion = extra["agp.version"] as String + id("com.android.application").version(agpVersion) val composeVersion = extra["compose.version"] as String id("org.jetbrains.compose").version(composeVersion) diff --git a/experimental/examples/falling-balls-mpp/src/androidMain/AndroidManifest.xml b/experimental/examples/falling-balls-mpp/src/androidMain/AndroidManifest.xml new file mode 100644 index 0000000000..13544326ee --- /dev/null +++ b/experimental/examples/falling-balls-mpp/src/androidMain/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/src/androidMain/kotlin/org/jetbrains/fallingballs/MainActivity.kt b/experimental/examples/falling-balls-mpp/src/androidMain/kotlin/org/jetbrains/fallingballs/MainActivity.kt new file mode 100644 index 0000000000..9ea02a4a76 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/src/androidMain/kotlin/org/jetbrains/fallingballs/MainActivity.kt @@ -0,0 +1,23 @@ +package org.jetbrains.fallingballs + +import FallingBalls +import Game +import Time +import android.os.Bundle +import androidx.activity.compose.setContent +import androidx.appcompat.app.AppCompatActivity +import androidx.compose.runtime.remember + +object AndroidTime : Time { + override fun now(): Long = System.nanoTime() +} + +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContent { + val game = remember { Game(AndroidTime) } + FallingBalls(game) + } + } +} \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/src/androidMain/res/values/strings.xml b/experimental/examples/falling-balls-mpp/src/androidMain/res/values/strings.xml new file mode 100644 index 0000000000..88a8d2c765 --- /dev/null +++ b/experimental/examples/falling-balls-mpp/src/androidMain/res/values/strings.xml @@ -0,0 +1,3 @@ + + Chat app + \ No newline at end of file diff --git a/experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/FallingBalls.kt b/experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/FallingBalls.kt index 5774ced90b..731435037e 100644 --- a/experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/FallingBalls.kt +++ b/experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/FallingBalls.kt @@ -12,9 +12,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.em -import org.jetbrains.skiko.KotlinBackend -import kotlin.math.round +import androidx.compose.ui.unit.sp @Composable fun FallingBalls(game: Game) { @@ -22,10 +20,10 @@ fun FallingBalls(game: Game) { Column { Text( "Catch balls!${if (game.finished) " Game over!" else ""}", - fontSize = 1.8f.em, + fontSize = 20.sp, color = Color(218, 120, 91) ) - Text("Score: ${game.score} Time: ${game.elapsed / 1_000_000} Blocks: ${game.numBlocks.toInt()}", fontSize = 1.8f.em) + Text("Score: ${game.score} Time: ${game.elapsed / 1_000_000} Blocks: ${game.numBlocks.toInt()}", fontSize = 20.sp) Row { if (!game.started) { Slider( @@ -45,7 +43,7 @@ fun FallingBalls(game: Game) { } } ) { - Text(if (game.started) "Stop" else "Start", fontSize = 2f.em) + Text(if (game.started) "Stop" else "Start", fontSize = 25.sp) } if (game.started) { Button( @@ -56,7 +54,7 @@ fun FallingBalls(game: Game) { onClick = { game.togglePause() }) { - Text(if (game.paused) "Resume" else "Pause", fontSize = 2f.em) + Text(if (game.paused) "Resume" else "Pause", fontSize = 25.sp) } } }