Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello world Kotlin Jetpack Compose Example; Fixes:#3550 #3769

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
103b7b8
Hello world Kotlin Jetpack Compose Example; Fixes:#3550
himanshumahajan138 Oct 18, 2024
d05eb8b
Merge branch 'issue-3550' of https://github.com/himanshumahajan138/mi…
himanshumahajan138 Oct 18, 2024
a73defd
Reviews Resolved ; Fixes: #3550
himanshumahajan138 Oct 19, 2024
cd32044
Fixed JsonForamtters.scala ; Fixes: #3550
himanshumahajan138 Oct 19, 2024
bfa6b1d
Revert JsonForamtters.scala ; Fixes: #3550
himanshumahajan138 Oct 19, 2024
56b1ba8
Merge branch 'main' into issue-3550
himanshumahajan138 Oct 20, 2024
dbd641e
Merge branch 'com-lihaoyi:main' into issue-3550
himanshumahajan138 Oct 20, 2024
b9ca48e
Merge branch 'main' into issue-3550
himanshumahajan138 Oct 20, 2024
2d14014
Merge branch 'main' into issue-3550
himanshumahajan138 Oct 20, 2024
d9d78b8
Merge branch 'main' into issue-3550
himanshumahajan138 Oct 21, 2024
c81b905
Final Code Updated and Docs Generated, Ready to Merge After Review; F…
himanshumahajan138 Oct 21, 2024
84875da
Final Code Updated and Docs Generated, Ready to Merge After Review; F…
himanshumahajan138 Oct 21, 2024
5bb8991
Fixed Silly Mistake (MainActivity.kt) ; Fixes: #3550
himanshumahajan138 Oct 21, 2024
e410fdd
Final Fixture
himanshumahajan138 Oct 21, 2024
5ad9ba1
Merge branch 'main' into issue-3550
himanshumahajan138 Oct 23, 2024
15d8bfd
Merge branch 'main' into issue-3550
himanshumahajan138 Oct 24, 2024
971ea8f
Whole Android Updated; Fixes:#3550
himanshumahajan138 Oct 24, 2024
b7b7b61
Merge branch 'main' into issue-3550
himanshumahajan138 Oct 26, 2024
ed45d53
Merge branch 'com-lihaoyi:main' into issue-3550
himanshumahajan138 Oct 29, 2024
041ed51
Merge branch 'com-lihaoyi:main' into issue-3550
himanshumahajan138 Oct 31, 2024
26e5cfc
Merge branch 'main' into issue-3550
himanshumahajan138 Nov 8, 2024
2c632c1
Fixture Clean
himanshumahajan138 Nov 8, 2024
8d153b7
Fixture Clean
himanshumahajan138 Nov 8, 2024
d4b436a
Merge branch 'main' into issue-3550
himanshumahajan138 Nov 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/modules/ROOT/pages/kotlinlib/android-examples.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ This example demonstrates how to create a basic "Hello World" Android applicatio
using the Mill build tool. It outlines the minimum setup required to compile Kotlin code,
package it into an APK, and run the app on an Android device.

== Kotlin Android Jetpack Compose Application

include::partial$example/kotlinlib/android/2-jetpack-compose-hello-world.adoc[]

This example demonstrates how to create a basic "Hello World" Jetpack Compose Android application
using the Mill build tool. It outlines the minimum setup required to compile Kotlin code,
Compiling Jetpack Compose Libraries, package it into an APK, and run the app on an Android device.

== Understanding `AndroidSdkModule` and `AndroidAppKotlinModule`

The two main modules you need to understand when building Android apps with Mill
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.helloworld.app">
<uses-sdk android:minSdkVersion="9"/>
<uses-sdk android:targetSdkVersion="35"/>
<application android:label="Hello World" android:debuggable="true">
<application android:label="@string/app_name" android:theme="@android:style/Theme.Light.NoTitleBar" android:debuggable="true">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<color name="white">#FFFFFF</color>
<color name="text_green">#34A853</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">HelloWorldApp</string>
<string name="hello_world">Hello, World Java!</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Gravity;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;
import android.view.Gravity;

import android.graphics.Color;

public class MainActivity extends Activity {
@Override
Expand All @@ -16,20 +15,26 @@ protected void onCreate(Bundle savedInstanceState) {
// Create a new TextView
TextView textView = new TextView(this);

// Set the text to "Hello, World!"
textView.setText("Hello, World!");
// Set the text to the string resource
textView.setText(getString(R.string.hello_world));

// Set text size
textView.setTextSize(32);

// Center the text within the view
textView.setGravity(Gravity.CENTER);

// Set layout parameters (width and height)
// Set the layout parameters (width and height)
textView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));

// Set the text color using a resource
textView.setTextColor(getResources().getColor(R.color.text_green));

// Set the background color using a resource
textView.setBackgroundColor(getResources().getColor(R.color.white));

// Set the content view to display the TextView
setContentView(textView);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.helloworld.app">
<uses-sdk android:minSdkVersion="9"/>
<uses-sdk android:targetSdkVersion="35"/>
<application android:label="Hello World" android:debuggable="true">
<application android:label="@string/app_name" android:theme="@android:style/Theme.Light.NoTitleBar" android:debuggable="true">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<color name="white">#FFFFFF</color>
<color name="text_green">#34A853</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">HelloWorldApp</string>
<string name="hello_world">Hello, World Kotlin!</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.helloworld.app

import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import android.view.Gravity
import android.widget.TextView
import android.view.ViewGroup.LayoutParams
import android.graphics.Color

class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -13,8 +14,8 @@ class MainActivity : Activity() {
// Create a new TextView
val textView = TextView(this)

// Set the text to "Hello, World!"
textView.text = "Hello, World Kotlin!"
// Set the text to the string resource
textView.text = getString(R.string.hello_world)

// Set text size
textView.textSize = 32f
Expand All @@ -28,6 +29,12 @@ class MainActivity : Activity() {
LayoutParams.MATCH_PARENT
)

// Set the text color using a resource
textView.setTextColor(getColor(R.color.text_green)) // Using hex color code directly

// Set the background color using a resource
textView.setBackgroundColor(getColor(R.color.white)) // Using hex color code directly

// Set the content view to display the TextView
setContentView(textView)
}
Expand Down
2 changes: 1 addition & 1 deletion example/kotlinlib/android/1-hello-world/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object androidSdkModule0 extends AndroidSdkModule{
// Actual android application
object app extends AndroidAppKotlinModule {

def kotlinVersion = "2.0.0"
def kotlinVersion = "2.0.20"
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworld.app">
<uses-sdk android:minSdkVersion="21"/>
<uses-sdk android:targetSdkVersion="35"/>
<application android:label="@string/app_name" android:theme="@android:style/Theme.Light.NoTitleBar" android:debuggable="true">
<activity android:name=".MainActivity" android:exported="true">
<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,4 @@
<resources>
<color name="white">#FFFFFF</color>
<color name="text_green">#34A853</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">HelloWorldApp</string>
<string name="hello_world">Hello, World!\nJetpack Compose!</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.helloworld.app

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup.LayoutParams
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val linearLayout = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
gravity = Gravity.CENTER
setBackgroundColor(getColor(R.color.white)) // Use color from resources
layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
)
}

val textView = TextView(this).apply {
text = getString(R.string.hello_world)
textSize = 32f
setTextColor(getColor(R.color.text_green)) // Use color from resources
gravity = Gravity.CENTER
setPadding(16, 16, 16, 16)
}

linearLayout.addView(textView)

setContentView(linearLayout)
}
}
42 changes: 42 additions & 0 deletions example/kotlinlib/android/2-jetpack-compose-hello-world/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package build

import mill._
import kotlinlib._
import coursier.maven.MavenRepository
import mill.kotlinlib.android.AndroidAppKotlinModule
import mill.javalib.android.AndroidSdkModule

val maven_google = Seq(
MavenRepository("https://maven.google.com/"),
MavenRepository("https://repo1.maven.org/maven2")
)

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

object app extends AndroidAppKotlinModule {
def kotlinVersion = "2.0.20"
def androidSdkModule = mill.define.ModuleRef(androidSdkModule0)

// If Dependencies are not provided but used in MainActivity then error will occur
override def mandatoryIvyDeps: T[Agg[Dep]] = Task {
super.mandatoryIvyDeps() ++ Agg(
// Jetpack Compose dependencies
ivy"androidx.compose.compiler:compiler:1.5.15",
ivy"androidx.activity:activity:1.8.2",
ivy"androidx.activity:activity-compose:1.8.2",
ivy"androidx.compose.runtime:runtime:1.3.1",
ivy"androidx.compose.material3:material3:1.0.1"
)
}

def repositoriesTask = T.task { super.repositoriesTask() ++ maven_google }
}

/** Usage

> ./mill show app.androidApk
".../out/app/androidApk.dest/app.apk"

*/
Loading
Loading