Skip to content

Commit 7a69216

Browse files
authored
Add viewmodel screen for KMP (#613)
* Add viewmodel screen for KMP * Apply Spotless * Fix snippets by adding compose compiler * Add androidApp to prevent Compose issue --------- Co-authored-by: mlykotom <[email protected]>
1 parent e4396f6 commit 7a69216

File tree

7 files changed

+123
-9
lines changed

7 files changed

+123
-9
lines changed

kmp/androidApp/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

kmp/androidApp/build.gradle.kts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.compose.compiler)
5+
}
6+
7+
android {
8+
namespace = "com.example.kmp.snippets"
9+
compileSdk = libs.versions.compileSdk.get().toInt()
10+
11+
defaultConfig {
12+
applicationId = "com.example.kmp.snippets"
13+
minSdk = libs.versions.minSdk.get().toInt()
14+
targetSdk = libs.versions.targetSdk.get().toInt()
15+
versionCode = 1
16+
versionName = "1.0"
17+
18+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
21+
buildTypes {
22+
release {
23+
isMinifyEnabled = false
24+
proguardFiles(
25+
getDefaultProguardFile("proguard-android-optimize.txt"),
26+
"proguard-rules.pro"
27+
)
28+
}
29+
}
30+
31+
compileOptions {
32+
sourceCompatibility = JavaVersion.VERSION_17
33+
targetCompatibility = JavaVersion.VERSION_17
34+
}
35+
36+
kotlin {
37+
jvmToolchain(17)
38+
}
39+
40+
buildFeatures {
41+
compose = true
42+
}
43+
}
44+
45+
dependencies {
46+
val composeBom = project.dependencies.platform(libs.androidx.compose.bom)
47+
implementation(composeBom)
48+
implementation(project(":kmp:shared"))
49+
50+
implementation(libs.androidx.compose.runtime)
51+
implementation(libs.androidx.compose.ui)
52+
implementation(libs.androidx.compose.foundation)
53+
implementation(libs.androidx.compose.foundation.layout)
54+
implementation(libs.androidx.compose.ui.util)
55+
implementation(libs.androidx.compose.material)
56+
implementation(libs.androidx.lifecycle.runtime)
57+
implementation(libs.androidx.lifecycle.viewModelCompose)
58+
}

kmp/androidApp/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.kmp.snippets
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.lifecycle.viewmodel.compose.viewModel
21+
22+
// [START android_kmp_viewmodel_screen]
23+
// androidApp/ui/MainScreen.kt
24+
25+
@Composable
26+
fun MainScreen(
27+
viewModel: MainViewModel = viewModel(
28+
factory = mainViewModelFactory,
29+
),
30+
) {
31+
// observe the viewModel state
32+
}
33+
// [END android_kmp_viewmodel_screen]

kmp/shared/build.gradle.kts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ plugins {
55
}
66

77
kotlin {
8-
98
// Target declarations - add or remove as needed below. These define
109
// which platforms this KMP module supports.
1110
// See: https://kotlinlang.org/docs/multiplatform-discover-project.html#targets
1211
androidLibrary {
13-
namespace = "com.example.kmp.snippets"
14-
compileSdk = 36
15-
minSdk = 24
12+
namespace = "com.example.kmp.snippets.shared"
13+
compileSdk = libs.versions.compileSdk.get().toInt()
14+
minSdk = libs.versions.minSdk.get().toInt()
1615

1716
withHostTestBuilder {
1817
}
@@ -24,6 +23,8 @@ kotlin {
2423
}
2524
}
2625

26+
jvmToolchain(17)
27+
2728
// For iOS targets, this is also where you should
2829
// configure native binary output. For more information, see:
2930
// https://kotlinlang.org/docs/multiplatform-build-native-binaries.html#build-xcframeworks
@@ -64,9 +65,7 @@ kotlin {
6465

6566
androidMain {
6667
dependencies {
67-
// Add Android-specific dependencies here. Note that this source set depends on
68-
// commonMain by default and will correctly pull the Android artifacts of any KMP
69-
// dependencies declared in commonMain.
68+
7069
}
7170
}
7271

@@ -88,5 +87,4 @@ kotlin {
8887
}
8988
}
9089
}
91-
9290
}

settings.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ include(
3939
":identity:credentialmanager",
4040
":xr",
4141
":watchfacepush:validator",
42-
":kmp:shared",
42+
":kmp:androidApp",
43+
":kmp:shared"
4344
)

0 commit comments

Comments
 (0)