Skip to content

Commit

Permalink
Android: add aar dependencies support, create proper build pipeline, …
Browse files Browse the repository at this point in the history
…add Jetpack Compose example
  • Loading branch information
0xnm committed Jan 4, 2025
1 parent 609720f commit 89304c4
Show file tree
Hide file tree
Showing 34 changed files with 1,181 additions and 289 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
install-android-sdk: false

- java-version: 17
millargs: "'example.android.__.local.test'"
millargs: "-j1 'example.android.__.local.test'"
install-android-sdk: true

- java-version: 17
Expand Down
40 changes: 7 additions & 33 deletions example/android/javalib/1-hello-world/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,35 @@ import mill.javalib.android.AndroidTestModule
// Create and configure an Android SDK module to manage Android SDK paths and tools.
object androidSdkModule0 extends AndroidSdkModule {
def buildToolsVersion = "35.0.0"
def bundleToolVersion = "1.17.2"
}

// Actual android application
object app extends AndroidAppModule {
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)
def androidMinSdk = 19
def androidCompileSdk = 35

// Configuration for ReleaseKey
override def releaseKeyPath = super.millSourcePath

def androidReleaseKeyName: T[String] = Task { "releaseKey.jks" }
def androidReleaseKeyAlias: T[String] = Task { "releaseKey" }
def androidReleaseKeyPass: T[String] = Task { "MillBuildTool" }
def androidReleaseKeyStorePass: T[String] = Task { "MillBuildTool" }
def androidReleaseKeyName: T[Option[String]] = Task { Some("releaseKey.jks") }
def androidReleaseKeyAlias: T[Option[String]] = Task { Some("releaseKey") }
def androidReleaseKeyPass: T[Option[String]] = Task { Some("MillBuildTool") }
def androidReleaseKeyStorePass: T[Option[String]] = Task { Some("MillBuildTool") }

override def androidVirtualDeviceIdentifier: String = "java-test"

private def mainRoot = millSourcePath

object test extends AndroidAppTests with TestModule.Junit4 {
def testFramework = "com.novocode.junit.JUnitFramework"
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"junit:junit:4.13.2"
)
}

object it extends AndroidAppIntegrationTests with AndroidTestModule.AndroidJUnit {
def repositoriesTask = Task.Anon {
super.repositoriesTask() ++
Seq(MavenRepository("https://maven.google.com"))
}
object it extends AndroidAppInstrumentedTests with AndroidTestModule.AndroidJUnit {

def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)

override def instrumentationPackage = "com.helloworld.app"

/* TODO this needs to change to the location of the debug keystore once integration test */
override def releaseKeyPath = mainRoot

def androidReleaseKeyName: T[String] = Task {
"releaseKey.jks"
}

def androidReleaseKeyAlias: T[String] = Task {
"releaseKey"
}

def androidReleaseKeyPass: T[String] = Task {
"MillBuildTool"
}

def androidReleaseKeyStorePass: T[String] = Task {
"MillBuildTool"
}

/* TODO currently the dependency resolution ignores the platform type and kotlinx-coroutines-core has
* conflicting classes with kotlinx-coroutines-core-jvm . Remove the exclusions once the dependency
* resolution resolves conflicts between androidJvm and jvm platform types
Expand Down
12 changes: 5 additions & 7 deletions example/android/javalib/2-app-bundle/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ import mill.javalib.android.{AndroidSdkModule, AndroidAppBundle}

object androidSdkModule0 extends AndroidSdkModule {
def buildToolsVersion = "35.0.0"
def bundleToolVersion = "1.17.2"
}

object bundle extends AndroidAppBundle {
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)
def androidCompileSdk = 35

override def releaseKeyPath = millSourcePath

def androidReleaseKeyName: T[String] = Task { "releaseKey.jks" }
def androidReleaseKeyAlias: T[String] = Task { "releaseKey" }
def androidReleaseKeyPass: T[String] = Task { "MillBuildTool" }
def androidReleaseKeyStorePass: T[String] = Task { "MillBuildTool" }
def androidReleaseKeyName: T[Option[String]] = Task { Some("releaseKey.jks") }
def androidReleaseKeyAlias: T[Option[String]] = Task { Some("releaseKey") }
def androidReleaseKeyPass: T[Option[String]] = Task { Some("MillBuildTool") }
def androidReleaseKeyStorePass: T[Option[String]] = Task { Some("MillBuildTool") }
}

////SNIPPET:END
Expand Down
3 changes: 1 addition & 2 deletions example/android/javalib/3-linting/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import mill.javalib.android.{AndroidSdkModule, AndroidAppModule, AndroidLintRepo
// Create and configure an Android SDK module to manage Android SDK paths and tools.
object androidSdkModule0 extends AndroidSdkModule {
def buildToolsVersion = "35.0.0"
def bundleToolVersion = "1.17.2"
}

// Actual android application with linting config
object app extends AndroidAppModule {
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)
override def releaseKeyPath = millSourcePath
def androidCompileSdk = 35

// Set path to the custom `lint.xml` config file. It is usually at the root of the project
def androidLintConfigPath = Task { Some(PathRef(millSourcePath / "lint.xml")) }
Expand Down
24 changes: 7 additions & 17 deletions example/android/kotlinlib/1-hello-kotlin/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,34 @@ import coursier.maven.MavenRepository
// Create and configure an Android SDK module to manage Android SDK paths and tools.
object androidSdkModule0 extends AndroidSdkModule {
def buildToolsVersion = "35.0.0"
def bundleToolVersion = "1.17.2"
}

// Actual android application
object app extends AndroidAppKotlinModule {

def kotlinVersion = "2.0.20"
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)
def androidMinSdk = 19
def androidCompileSdk = 35

// Configuration for ReleaseKey
override def releaseKeyPath = millSourcePath
def androidReleaseKeyName: T[String] = Task { "releaseKey.jks" }
def androidReleaseKeyAlias: T[String] = Task { "releaseKey" }
def androidReleaseKeyPass: T[String] = Task { "MillBuildTool" }
def androidReleaseKeyStorePass: T[String] = Task { "MillBuildTool" }
def androidReleaseKeyName: T[Option[String]] = Task { Some("releaseKey.jks") }
def androidReleaseKeyAlias: T[Option[String]] = Task { Some("releaseKey") }
def androidReleaseKeyPass: T[Option[String]] = Task { Some("MillBuildTool") }
def androidReleaseKeyStorePass: T[Option[String]] = Task { Some("MillBuildTool") }
override def androidVirtualDeviceIdentifier: String = "kotlin-test"
override def androidEmulatorPort: String = "5556"

/* TODO this won't be needed once the debug keystore is implemented */
private def mainRoot = millSourcePath

object test extends AndroidAppKotlinTests with TestModule.Junit4 {
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"junit:junit:4.13.2"
)
}

object it extends AndroidAppKotlinIntegrationTests with AndroidTestModule.AndroidJUnit {
object it extends AndroidAppKotlinInstrumentedTests with AndroidTestModule.AndroidJUnit {

override def instrumentationPackage = "com.helloworld.app"

/* TODO this needs to change to something better once integration tests work with debug keystore */
override def releaseKeyPath = mainRoot
def androidReleaseKeyName: T[String] = Task { "releaseKey.jks" }
def androidReleaseKeyAlias: T[String] = Task { "releaseKey" }
def androidReleaseKeyPass: T[String] = Task { "MillBuildTool" }
def androidReleaseKeyStorePass: T[String] = Task { "MillBuildTool" }

/* TODO currently the dependency resolution ignores the platform type and kotlinx-coroutines-core has
* conflicting classes with kotlinx-coroutines-core-jvm . Remove the exclusions once the dependency
* resolution resolves conflicts between androidJvm and jvm platform types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.composetest">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ComposeTest">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.ComposeTest">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.composetest

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.composetest.ui.theme.ComposeTestTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
ComposeTestTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { _ ->
CounterScreen(
modifier = Modifier.padding(),
)
}
}
}
}
}

@Suppress("FunctionName")
@Composable
fun CounterScreen(modifier: Modifier = Modifier) {
val clickCount = remember { mutableIntStateOf(0) }

Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text(
text = "Button clicked: ${clickCount.intValue} times",
modifier = Modifier.padding(bottom = 16.dp),
)

Button(
onClick = { clickCount.intValue++ },
colors = ButtonDefaults.buttonColors().copy(
containerColor = Color.Cyan,
),
) {
Text("Click me!")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.composetest.ui.theme

import androidx.compose.ui.graphics.Color

val Purple80 = Color(0xFFD0BCFF)
val PurpleGrey80 = Color(0xFFCCC2DC)
val Pink80 = Color(0xFFEFB8C8)

val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.composetest.ui.theme

import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext

private val DarkColorScheme = darkColorScheme(
primary = Purple80,
secondary = PurpleGrey80,
tertiary = Pink80,
)

private val LightColorScheme = lightColorScheme(
primary = Purple40,
secondary = PurpleGrey40,
tertiary = Pink40,

/* Other default colors to override
background = Color(0xFFFFFBFE),
surface = Color(0xFFFFFBFE),
onPrimary = Color.White,
onSecondary = Color.White,
onTertiary = Color.White,
onBackground = Color(0xFF1C1B1F),
onSurface = Color(0xFF1C1B1F), */
)

@Suppress("FunctionName")
@Composable
fun ComposeTestTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit,
) {
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}

darkTheme -> DarkColorScheme
else -> LightColorScheme
}

MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.composetest.ui.theme

import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp

// Set of Material typography styles to start with
val Typography = Typography(
bodyLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp,
),
/* Other default text styles to override
titleLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 22.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp
),
labelSmall = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Medium,
fontSize = 11.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp
) */
)
Loading

0 comments on commit 89304c4

Please sign in to comment.