Skip to content

Commit 017b86a

Browse files
Fixes:#3550; Added hello-world Android Kotlin example using Mill (#3679)
# Pull Request Fixes: #3550 ## Description Added `Kotlin Android "Hello World" Application Example` while reusing `AndroidSdkModule`, `KotlinModule` and creating new Module `AndroidAppModule` with Kotlin Support and Proper `Documentation`. ## Related Issues - Link to related issue #3550. ## Checklist - [x] Android Support for Kotlin Added in the example section for "HelloWorld" Android Application. - [x] Added Kotlin Source files and Customized AndroidAppModule. - [x] Code is clean and follows project conventions. - [x] Documentation has been updated. - [x] Tests have been added or updated. - [x] All tests pass. ## Additional Notes Actually, I was wondering that we reused the `AndroidSdkModule` but created a new Module `AndroidAppModule` for Andorid Workflow but what we can do is to provide `Kotlin support in the Existing AndroidAppModule` and then based on user query they can use the functionality. So for this i need @lihaoyi Sir your Permission and Guidance, Please Review this PR suggest changes...
1 parent b4d4fea commit 017b86a

File tree

7 files changed

+178
-0
lines changed

7 files changed

+178
-0
lines changed

docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* xref:kotlinlib/publishing.adoc[]
3737
* xref:kotlinlib/build-examples.adoc[]
3838
* xref:kotlinlib/web-examples.adoc[]
39+
* xref:kotlinlib/android-examples.adoc[]
3940
4041
.Build Tool Comparisons
4142
* xref:comparisons/maven.adoc[]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
= (Experimental) Android Builds
2+
:page-aliases: android_app_kotlin_examples.adoc
3+
4+
++++
5+
<script>
6+
gtag('config', 'AW-16649289906');
7+
</script>
8+
++++
9+
10+
This page provides an example of using Mill as a build tool for Android applications.
11+
This workflow is still pretty rough and nowhere near production ready, but can serve as
12+
a starting point for further experimentation and development.
13+
14+
== Relevant Modules
15+
16+
These are the main Mill Modules that are relevant for building Android apps:
17+
18+
* {mill-doc-url}/api/latest/mill/scalalib/AndroidSdkModule.html[`mill.scalalib.AndroidSdkModule`]: Handles Android SDK management and tools.
19+
* {mill-doc-url}/api/latest/mill/kotlinlib/android/AndroidAppKotlinModule.html[`mill.kotlinlib.android.AndroidAppKotlinModule`]: Provides a framework for building Android applications.
20+
* {mill-doc-url}/api/latest/mill/kotlinlib/KotlinModule.html[`mill.kotlinlib.KotlinModule`]: General Kotlin build tasks like compiling Kotlin code and creating JAR files.
21+
22+
== Simple Android Hello World Application
23+
24+
include::partial$example/kotlinlib/android/1-hello-world.adoc[]
25+
26+
This example demonstrates how to create a basic "Hello World" Android application
27+
using the Mill build tool. It outlines the minimum setup required to compile Kotlin code,
28+
package it into an APK, and run the app on an Android device.
29+
30+
== Understanding `AndroidSdkModule` and `AndroidAppKotlinModule`
31+
32+
The two main modules you need to understand when building Android apps with Mill
33+
are `AndroidSdkModule` and `AndroidAppKotlinModule`.
34+
35+
`AndroidSdkModule`:
36+
37+
* This module manages the installation and configuration of the Android SDK, which includes
38+
tools like `aapt`, `d8`, `zipalign`, and `apksigner`. These tools are used
39+
for compiling, packaging, and signing Android applications.
40+
41+
`AndroidAppKotlinModule`:
42+
This module provides the step-by-step workflow for building an Android app. It handles
43+
everything from compiling the code to generating a signed APK for distribution.
44+
45+
1. **Compiling Kotlin code**: The module compiles your Kotlin code into `.class` files, which is the first step in creating an Android app.
46+
2. **Packaging into JAR**: It then packages the compiled `.class` files into a JAR file, which is necessary before converting to Android's format.
47+
3. **Converting to DEX format**: The JAR file is converted into DEX format, which is the executable format for Android applications.
48+
4. **Creating an APK**: The DEX files and Android resources (like layouts and strings) are packaged together into an APK file, which is the installable file for Android devices.
49+
5. **Optimizing with zipalign**: The APK is optimized using `zipalign` to ensure better performance on Android devices.
50+
6. **Signing the APK**: Finally, the APK is signed with a digital signature, allowing it to be distributed and installed on Android devices.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.helloworld.app">
2+
<uses-sdk android:minSdkVersion="9"/>
3+
<uses-sdk android:targetSdkVersion="35"/>
4+
<application android:label="Hello World" android:debuggable="true">
5+
<activity android:name=".MainActivity"
6+
android:exported="true">
7+
<intent-filter>
8+
<action android:name="android.intent.action.MAIN"/>
9+
<category android:name="android.intent.category.LAUNCHER"/>
10+
</intent-filter>
11+
</activity>
12+
</application>
13+
</manifest>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.helloworld.app
2+
3+
import android.app.Activity
4+
import android.os.Bundle
5+
import android.widget.TextView
6+
import android.view.Gravity
7+
import android.view.ViewGroup.LayoutParams
8+
9+
class MainActivity : Activity() {
10+
override fun onCreate(savedInstanceState: Bundle?) {
11+
super.onCreate(savedInstanceState)
12+
13+
// Create a new TextView
14+
val textView = TextView(this)
15+
16+
// Set the text to "Hello, World!"
17+
textView.text = "Hello, World!"
18+
19+
// Set text size
20+
textView.textSize = 32f
21+
22+
// Center the text within the view
23+
textView.gravity = Gravity.CENTER
24+
25+
// Set layout parameters (width and height)
26+
textView.layoutParams = LayoutParams(
27+
LayoutParams.MATCH_PARENT,
28+
LayoutParams.MATCH_PARENT
29+
)
30+
31+
// Set the content view to display the TextView
32+
setContentView(textView)
33+
}
34+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This section sets up a basic Android project using Mill.
2+
// We utilize `AndroidAppKotlinModule` and `AndroidSdkModule` to streamline the process of
3+
// building an Android application with minimal configuration.
4+
//
5+
// By extending `AndroidAppKotlinModule`, we inherit all Android-related tasks such as
6+
// resource generation, APK building, DEX conversion, and APK signing.
7+
// Additionally, `AndroidSdkModule` is embedded, making SDK management seamless.
8+
9+
//// SNIPPET:BUILD
10+
package build
11+
12+
import mill._
13+
import kotlinlib._
14+
import mill.kotlinlib.android.AndroidAppKotlinModule
15+
import mill.javalib.android.AndroidSdkModule
16+
17+
// Create and configure an Android SDK module to manage Android SDK paths and tools.
18+
object androidSdkModule0 extends AndroidSdkModule{
19+
def buildToolsVersion = "35.0.0"
20+
}
21+
22+
// Actual android application
23+
object app extends AndroidAppKotlinModule {
24+
25+
def kotlinVersion = "2.0.0"
26+
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)
27+
}
28+
29+
////SNIPPET:END
30+
31+
32+
/** Usage
33+
34+
> ./mill show app.androidApk
35+
".../out/app/androidApk.dest/app.apk"
36+
37+
*/
38+
39+
// This command triggers the build process, which installs the Android Setup, compiles the kotlin
40+
// code, generates Android resources, converts kotlin bytecode to DEX format, packages everything
41+
// into an APK, optimizes the APK using `zipalign`, and finally signs it.
42+
//
43+
// This Mill build configuration is designed to build a simple "Hello World" Android application.
44+
// By extending `AndroidAppKotlinModule`, we leverage its predefined Android build tasks, ensuring that
45+
// all necessary steps (resource generation, APK creation, and signing) are executed automatically.
46+
//
47+
// ### Project Structure:
48+
// The project follows the standard Android app layout. Below is a typical project folder structure:
49+
//
50+
// ----
51+
// .
52+
// ├── build.mill
53+
// ├── AndroidManifest.xml
54+
// └── app/src/main/kotlin
55+
// └── com/helloworld/app
56+
// └── MainActivity.kt
57+
// ----
58+
//

example/package.mill

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ object `package` extends RootModule with Module {
3939
object web extends Cross[ExampleCrossModule](build.listIn(millSourcePath / "web"))
4040
}
4141
object kotlinlib extends Module {
42+
object android extends Cross[ExampleCrossModuleJava](build.listIn(millSourcePath / "android"))
4243
object basic extends Cross[ExampleCrossModuleKotlin](build.listIn(millSourcePath / "basic"))
4344
object module extends Cross[ExampleCrossModuleKotlin](build.listIn(millSourcePath / "module"))
4445
object dependencies extends Cross[ExampleCrossModuleKotlin](build.listIn(millSourcePath / "dependencies"))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mill.kotlinlib.android
2+
3+
import mill.kotlinlib.KotlinModule
4+
import mill.javalib.android.AndroidAppModule
5+
6+
/**
7+
* Trait for building Android applications using the Mill build tool.
8+
*
9+
* This trait defines all the necessary steps for building an Android app from Kotlin sources,
10+
* integrating both Android-specific tasks and generic Kotlin tasks by extending the
11+
* [[KotlinModule]] (for standard Kotlin tasks)
12+
* and [[AndroidAppModule]] (for Android Application Workflow Process).
13+
*
14+
* It provides a structured way to handle various steps in the Android app build process,
15+
* including compiling Kotlin sources, creating DEX files, generating resources, packaging
16+
* APKs, optimizing, and signing APKs.
17+
*
18+
* [[https://developer.android.com/studio Android Studio Documentation]]
19+
*/
20+
@mill.api.experimental
21+
trait AndroidAppKotlinModule extends AndroidAppModule with KotlinModule {}

0 commit comments

Comments
 (0)