Skip to content

Commit 5933cd2

Browse files
committed
Add example using Kotlin + Compose + Material 3
Change-Id: I6935899f67f2b151bebc2717f290c692a6499b1a
1 parent 8578da9 commit 5933cd2

36 files changed

+941
-0
lines changed

example/compose-material3/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Go bind example using Kotlin + Compose + Material 3
2+
3+
Android project generated using Android Studio: `File > New > New Project > Empty Compose Activity (Material 3)`
4+
5+
### Requirements
6+
7+
1. `ANDROID_HOME` is set (e.g: `export ANDROID_HOME="$HOME/Android/Sdk"`)
8+
2. `gomobile` and `gobind` is available - (e.g: `go install golang.org/x/mobile/cmd/gomobile@latest`)
9+
3. Android NDK 19 to 23 is installed
10+
11+
12+
### Installing the NDK
13+
14+
1. Download [latest Android NDK LTS - r23c](https://dl.google.com/android/repository/android-ndk-r23c-linux.zip)
15+
2. Extract it under `~/Android/Sdk/ndk/`
16+
3. Create the `ndk-bundle` folder as symbolic link:
17+
```
18+
ln -sf ~/Android/Sdk/ndk/android-ndk-r23c ~/Android/Sdk/ndk-bundle
19+
```
20+
21+
_For context: https://github.com/golang/go/issues/35030#issuecomment-641319703_
22+
23+
24+
### Building project
25+
26+
1. From this directory run:
27+
```
28+
gomobile bind -o android/app/libs/hello.aar -androidapi 32 ./hello
29+
```
30+
31+
2. Import this project in Android Studio and build;
32+
33+
During tests `-androidapi` also worked with: `19`, `21`, `22`, `23`, `24`, `26`, `27`, `28`, `29`, `30`, `31` and `32`
34+
35+
> Note that Compose Previews that uses external modules is not working in Android Studio Chipmunk | 2021.2.1 Patch 1 - [see thread](https://issuetracker.google.com/issues/206862224?pli=1)
36+
37+
> You need to run the `gomobile bind` command again every time you make a change to Go code.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
compileSdk 32
8+
9+
defaultConfig {
10+
applicationId "com.example.composematerial3"
11+
minSdk 30
12+
targetSdk 32
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
vectorDrawables {
18+
useSupportLibrary true
19+
}
20+
}
21+
22+
buildTypes {
23+
release {
24+
minifyEnabled false
25+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26+
}
27+
}
28+
compileOptions {
29+
sourceCompatibility JavaVersion.VERSION_1_8
30+
targetCompatibility JavaVersion.VERSION_1_8
31+
}
32+
kotlinOptions {
33+
jvmTarget = '1.8'
34+
}
35+
buildFeatures {
36+
compose true
37+
}
38+
composeOptions {
39+
kotlinCompilerExtensionVersion compose_version
40+
}
41+
packagingOptions {
42+
resources {
43+
excludes += '/META-INF/{AL2.0,LGPL2.1}'
44+
}
45+
}
46+
}
47+
48+
dependencies {
49+
50+
implementation 'androidx.core:core-ktx:1.7.0'
51+
implementation "androidx.compose.ui:ui:$compose_version"
52+
implementation 'androidx.compose.material3:material3:1.0.0-alpha01'
53+
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
54+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
55+
implementation 'androidx.activity:activity-compose:1.3.1'
56+
testImplementation 'junit:junit:4.13.2'
57+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
58+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
59+
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
60+
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
61+
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
62+
63+
implementation files('libs/hello.aar')
64+
}
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.composematerial3
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.example.composematerial3", appContext.packageName)
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.composematerial3">
5+
6+
<application
7+
android:allowBackup="true"
8+
android:dataExtractionRules="@xml/data_extraction_rules"
9+
android:fullBackupContent="@xml/backup_rules"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.ComposeMaterial3"
15+
tools:targetApi="31">
16+
<activity
17+
android:name=".MainActivity"
18+
android:exported="true"
19+
android:label="@string/app_name"
20+
android:theme="@style/Theme.ComposeMaterial3">
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
24+
<category android:name="android.intent.category.LAUNCHER" />
25+
</intent-filter>
26+
</activity>
27+
</application>
28+
29+
</manifest>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.composematerial3
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.layout.fillMaxSize
7+
import androidx.compose.material3.MaterialTheme
8+
import androidx.compose.material3.Surface
9+
import androidx.compose.material3.Text
10+
import androidx.compose.runtime.Composable
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.tooling.preview.Preview
13+
import com.example.composematerial3.ui.theme.ComposeMaterial3Theme
14+
import hello.Hello
15+
16+
class MainActivity : ComponentActivity() {
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
setContent {
20+
ComposeMaterial3Theme {
21+
// A surface container using the 'background' color from the theme
22+
Surface(
23+
modifier = Modifier.fillMaxSize(),
24+
color = MaterialTheme.colorScheme.background
25+
) {
26+
Greeting("Android and Gopher")
27+
}
28+
}
29+
}
30+
}
31+
}
32+
33+
@Composable
34+
fun Greeting(name: String) {
35+
Text(text = Hello.greetings(name))
36+
}
37+
38+
@Preview(showBackground = true)
39+
@Composable
40+
fun DefaultPreview() {
41+
ComposeMaterial3Theme {
42+
Greeting("Android and Gopher")
43+
}
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.composematerial3.ui.theme
2+
3+
import androidx.compose.ui.graphics.Color
4+
5+
val Purple80 = Color(0xFFD0BCFF)
6+
val PurpleGrey80 = Color(0xFFCCC2DC)
7+
val Pink80 = Color(0xFFEFB8C8)
8+
9+
val Purple40 = Color(0xFF6650a4)
10+
val PurpleGrey40 = Color(0xFF625b71)
11+
val Pink40 = Color(0xFF7D5260)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.example.composematerial3.ui.theme
2+
3+
import android.app.Activity
4+
import android.os.Build
5+
import androidx.compose.foundation.isSystemInDarkTheme
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.material3.darkColorScheme
8+
import androidx.compose.material3.dynamicDarkColorScheme
9+
import androidx.compose.material3.dynamicLightColorScheme
10+
import androidx.compose.material3.lightColorScheme
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.SideEffect
13+
import androidx.compose.ui.graphics.toArgb
14+
import androidx.compose.ui.platform.LocalContext
15+
import androidx.compose.ui.platform.LocalView
16+
import androidx.core.view.ViewCompat
17+
18+
private val DarkColorScheme = darkColorScheme(
19+
primary = Purple80,
20+
secondary = PurpleGrey80,
21+
tertiary = Pink80
22+
)
23+
24+
private val LightColorScheme = lightColorScheme(
25+
primary = Purple40,
26+
secondary = PurpleGrey40,
27+
tertiary = Pink40
28+
29+
/* Other default colors to override
30+
background = Color(0xFFFFFBFE),
31+
surface = Color(0xFFFFFBFE),
32+
onPrimary = Color.White,
33+
onSecondary = Color.White,
34+
onTertiary = Color.White,
35+
onBackground = Color(0xFF1C1B1F),
36+
onSurface = Color(0xFF1C1B1F),
37+
*/
38+
)
39+
40+
@Composable
41+
fun ComposeMaterial3Theme(
42+
darkTheme: Boolean = isSystemInDarkTheme(),
43+
// Dynamic color is available on Android 12+
44+
dynamicColor: Boolean = true,
45+
content: @Composable () -> Unit
46+
) {
47+
val colorScheme = when {
48+
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
49+
val context = LocalContext.current
50+
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
51+
}
52+
darkTheme -> DarkColorScheme
53+
else -> LightColorScheme
54+
}
55+
val view = LocalView.current
56+
if (!view.isInEditMode) {
57+
SideEffect {
58+
(view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
59+
ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
60+
}
61+
}
62+
63+
MaterialTheme(
64+
colorScheme = colorScheme,
65+
typography = Typography,
66+
content = content
67+
)
68+
}

0 commit comments

Comments
 (0)