From 9ba47f46aa07135b1046a9ca6d0a047e7a2ea759 Mon Sep 17 00:00:00 2001 From: Sushant Sikka Date: Mon, 10 Nov 2025 18:52:46 +0530 Subject: [PATCH 1/8] Migrating snippets from Kotlin learning guide --- kotlin/build.gradle.kts | 1 + .../android/learn-kotlin/build.gradle.kts | 76 +++++++++ .../example/kotlin_samples/KotlinSamples.kt | 149 ++++++++++++++++++ .../example/kotlin_samples/MainActivity.kt | 56 +++++++ 4 files changed, 282 insertions(+) create mode 100644 kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts create mode 100644 kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt create mode 100644 kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt diff --git a/kotlin/build.gradle.kts b/kotlin/build.gradle.kts index 12feddf2d..96dc5d862 100644 --- a/kotlin/build.gradle.kts +++ b/kotlin/build.gradle.kts @@ -54,6 +54,7 @@ android { } } dependencies { + implementation("libs.androidx.compose.foundation:foundation") implementation(libs.androidx.lifecycle.viewmodel.ktx) implementation(libs.kotlinx.coroutines.android) testImplementation(libs.kotlinx.coroutines.test) diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts new file mode 100644 index 000000000..39fc33f6e --- /dev/null +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts @@ -0,0 +1,76 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) +} + +android { + namespace = "com.example.kotlin_samples" + compileSdk = 36 + + defaultConfig { + applicationId = "com.example.kotlin_samples" + minSdk = 24 + targetSdk = 36 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + kotlinOptions { + jvmTarget = "11" + } + buildFeatures { + compose = true + } +} + +dependencies { + + // Core Android KTX for quality-of-life Kotlin extensions + implementation("androidx.core:core-ktx:1.13.1") + + // Activity library with Compose integration for `setContent` and `enableEdgeToEdge` + implementation("androidx.activity:activity-compose:1.9.0") + + // Jetpack Compose Bill of Materials (BOM) + // The BOM ensures that all of your Compose libraries use the same, compatible version. + val composeBom = platform("androidx.compose:compose-bom:2024.05.00") + implementation(composeBom) + androidTestImplementation(composeBom) + + // Compose UI for core components like Modifier and layout primitives + implementation("androidx.compose.ui:ui") + + // Compose Material 3 for Material Design components like Scaffold and Text + implementation("androidx.compose.material3:material3") + + // Compose Foundation for building blocks like scrolling + implementation("androidx.compose.foundation:foundation") + + // Tooling for displaying @Preview composables in Android Studio + implementation("androidx.compose.ui:ui-tooling-preview") + debugImplementation("androidx.compose.ui:ui-tooling") + + // Other standard dependencies for testing and lifecycle management + implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.1") + testImplementation("junit:junit:4.13.2") + androidTestImplementation("androidx.test.ext:junit:1.1.5") + androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") + androidTestImplementation("androidx.compose.ui:ui-test-junit4") + debugImplementation("androidx.compose.ui:ui-test-manifest") +} \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt new file mode 100644 index 000000000..cace089f4 --- /dev/null +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt @@ -0,0 +1,149 @@ +package com.example.kotlin_samples + +// A top-level constant used by several snippets. +private const val count = 42 + +/** + * This function consolidates the output of all language snippets + * into a single string for display in the UI. + */ +fun getLanguageSamplesOutput(): String { + return """ + --- Control Flow --- + ${controlFlowSnippets()} + + --- Null Safety --- + ${nullSafetySnippets()} + + --- Functions --- + ${functionSnippets()} + + --- Higher-Order Functions --- + ${higherOrderFunctionSnippets()} + """.trimIndent() +} + +private fun controlFlowSnippets(): String { + val output = StringBuilder() + + // [START kotlin_control_flow_if_else_if] + val result1 = if (count == 42) { + "I have the answer." + } else if (count > 35) { + "The answer is close." + } else { + "The answer eludes me." + } + output.appendLine("if-else if-else: $result1") + // [END kotlin_control_flow_if_else_if] + + // [START kotlin_control_flow_if_else] + val result2 = if (count == 42) { + "I have the answer." + } else { + "The answer eludes me." + } + output.appendLine("if-else: $result2") + // [END kotlin_control_flow_if_else] + + // [START kotlin_control_flow_if_as_expression] + val answerStringFromIf: String = if (count == 42) { + "I have the answer." + } else if (count > 35) { + "The answer is close." + } else { + "The answer eludes me." + } + output.appendLine("if as expression: $answerStringFromIf") + // [END kotlin_control_flow_if_as_expression] + + // [START kotlin_control_flow_when_as_expression] + val answerStringFromWhen = when { + count == 42 -> "I have the answer." + count > 35 -> "The answer is close." + else -> "The answer eludes me." + } + output.appendLine("when as expression: $answerStringFromWhen") + // [END kotlin_control_flow_when_as_expression] + + return output.toString() +} + +private fun nullSafetySnippets(): String { + // [START kotlin_null_safety_smart_cast] + val languageName: String? = "Kotlin" + var result = "Language name is null" + if (languageName != null) { + // No need to write languageName?.uppercase() + result = languageName.uppercase() + } + return "Smart cast result: $result" + // [END kotlin_null_safety_smart_cast] +} + +// [START kotlin_defining_a_function] +private fun generateAnswerString(): String { + return if (count == 42) { + "I have the answer." + } else { + "The answer eludes me" + } +} +// [END kotlin_defining_a_function] + +// [START kotlin_function_with_parameter] +private fun generateAnswerStringWithParam(countThreshold: Int): String { + return if (count > countThreshold) { + "I have the answer." + } else { + "The answer eludes me." + } +} +// [END kotlin_function_with_parameter] + + +private fun functionSnippets(): String { + val output = StringBuilder() + + // [START kotlin_calling_a_function] + val answerString = generateAnswerString() + output.appendLine("Function call: $answerString") + // [END kotlin_calling_a_function] + + // [START kotlin_calling_a_function_with_argument] + val answerStringWithArg = generateAnswerStringWithParam(41) + output.appendLine("Function with argument: $answerStringWithArg") + // [END kotlin_calling_a_function_with_argument] + + return output.toString() +} + +// [START kotlin_higher_order_function_definition] +private fun stringMapper(str: String, mapper: (String) -> Int): Int { + return mapper(str) +} +// [END kotlin_higher_order_function_definition] + +private fun higherOrderFunctionSnippets(): String { + val output = StringBuilder() + + // [START kotlin_lambda_declaration] + val stringLengthFunc: (String) -> Int = { input -> + input.length + } + // [END kotlin_lambda_declaration] + + // [START kotlin_lambda_invocation] + val stringLength: Int = stringLengthFunc("Android") + output.appendLine("Lambda invocation: Length of 'Android' is $stringLength") + // [END kotlin_lambda_invocation] + + // [START kotlin_higher_order_function_call_default] + val length1 = stringMapper("Android") { input -> + input.length + } + output.appendLine("Trailing lambda call: Length is $length1") + // [END kotlin_higher_order_function_call_default] + + return output.toString() +} diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt new file mode 100644 index 000000000..6cde89d91 --- /dev/null +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt @@ -0,0 +1,56 @@ +package com.example.kotlin_samples + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import com.example.kotlin_samples.ui.theme.KotlinsamplesTheme + +class MainActivity : ComponentActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContent { + KotlinsamplesTheme { + // We replace the default Scaffold content with our new composable + SampleScreen() + } + } + } +} + +@Composable +fun SampleScreen(modifier: Modifier = Modifier) { + Scaffold(modifier = modifier.fillMaxSize()) { innerPadding -> + // Get the output from our LanguageSamples.kt file + val output = getLanguageSamplesOutput() + + // Use a scrolling Text element to display the results + Text( + text = output, + modifier = Modifier + .padding(innerPadding) + .padding(16.dp) // Add some content padding + .fillMaxSize() + .verticalScroll(rememberScrollState()) // Make content scrollable + ) + } +} + +@Preview(showBackground = true) +@Composable +fun SampleScreenPreview() { + KotlinsamplesTheme { + SampleScreen() + } +} From 2aab9f041b72985be7872f0269dc6ae8f349bf03 Mon Sep 17 00:00:00 2001 From: sushantsikka <8463679+sushantsikka@users.noreply.github.com> Date: Mon, 10 Nov 2025 13:25:43 +0000 Subject: [PATCH 2/8] Apply Spotless --- .../com/example/kotlin_samples/KotlinSamples.kt | 16 ++++++++++++++++ .../com/example/kotlin_samples/MainActivity.kt | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt index cace089f4..1669cda42 100644 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.kotlin_samples // A top-level constant used by several snippets. diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt index 6cde89d91..61f59046a 100644 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.kotlin_samples import android.os.Bundle From 00ac50b49275ae101877f2ade8ece420f3faefd3 Mon Sep 17 00:00:00 2001 From: Sushant Sikka Date: Tue, 11 Nov 2025 21:35:45 +0530 Subject: [PATCH 3/8] Update KotlinSamples.kt --- .../example/kotlin_samples/KotlinSamples.kt | 233 ++++++++++-------- 1 file changed, 136 insertions(+), 97 deletions(-) diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt index 1669cda42..79fd857fd 100644 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt @@ -1,165 +1,204 @@ -/* - * Copyright 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +package com.example.android.kotlin.snippets -package com.example.kotlin_samples +import androidx.compose.ui.text.toUpperCase -// A top-level constant used by several snippets. +// A top-level constant used by the snippets below. private const val count = 42 -/** - * This function consolidates the output of all language snippets - * into a single string for display in the UI. +/* + * The following are function definitions from the provided snippets. + * They are defined at the top level so they can be called by other snippets. + * Some have been given unique names to prevent compilation errors, but their + * bodies are unchanged. */ -fun getLanguageSamplesOutput(): String { - return """ - --- Control Flow --- - ${controlFlowSnippets()} - --- Null Safety --- - ${nullSafetySnippets()} +// [START kotlin_defining_a_function] +// From Snippet 6 +fun generateAnswerString(): String { + val answerString = if (count == 42) { + "I have the answer." + } else { + "The answer eludes me" + } + + return answerString +} +// [END kotlin_defining_a_function] + +// [START kotlin_function_with_parameter] +// From Snippet 8 +// Note: This function has the same signature as the one from Snippet 10 and 11. +// It is kept here as the primary example. +fun generateAnswerString(countThreshold: Int): String { + val answerString = if (count > countThreshold) { + "I have the answer." + } else { + "The answer eludes me." + } + + return answerString +} +// [END kotlin_function_with_parameter] - --- Functions --- - ${functionSnippets()} +// [START kotlin_function_with_explicit_return] +// From Snippet 10 +fun generateAnswerStringWithExplicitReturn(countThreshold: Int): String { + return if (count > countThreshold) { + "I have the answer." + } else { + "The answer eludes me." + } +} +// [END kotlin_function_with_explicit_return] + +// [START kotlin_function_as_single_expression] +// From Snippet 11 +fun generateAnswerStringAsExpression(countThreshold: Int): String = if (count > countThreshold) { + "I have the answer" +} else { + "The answer eludes me" +} +// [END kotlin_function_as_single_expression] - --- Higher-Order Functions --- - ${higherOrderFunctionSnippets()} - """.trimIndent() +// [START kotlin_higher_order_function_definition] +// From Snippet 14 +fun stringMapper(str: String, mapper: (String) -> Int): Int { + // Invoke function + return mapper(str) } +// [END kotlin_higher_order_function_definition] -private fun controlFlowSnippets(): String { - val output = StringBuilder() +/** + * The main entry point for this file. Running this function will execute + * each of the original snippets in order. + */ +fun main() { + println("--- Snippet 1 ---") + runSnippet1() + println("\n--- Snippet 2 ---") + runSnippet2() + println("\n--- Snippet 3 ---") + runSnippet3() + println("\n--- Snippet 4 ---") + runSnippet4() + println("\n--- Snippet 5 ---") + runSnippet5() + println("\n--- Snippet 7 (no output) ---") + runSnippet7() + println("\n--- Snippet 9 (no output) ---") + runSnippet9() + println("\n--- Snippet 12 (no output) ---") + runSnippet12() + println("\n--- Snippet 13 ---") + runSnippet13() + println("\n--- Snippet 15 (no output) ---") + runSnippet15() + println("\n--- Snippet 16 (no output) ---") + runSnippet16() +} + +fun runSnippet1() { // [START kotlin_control_flow_if_else_if] - val result1 = if (count == 42) { - "I have the answer." + if (count == 42) { + println("I have the answer.") } else if (count > 35) { - "The answer is close." + println("The answer is close.") } else { - "The answer eludes me." + println("The answer eludes me.") } - output.appendLine("if-else if-else: $result1") // [END kotlin_control_flow_if_else_if] +} +fun runSnippet2() { // [START kotlin_control_flow_if_else] - val result2 = if (count == 42) { - "I have the answer." + if (count == 42) { + println("I have the answer.") } else { - "The answer eludes me." + println("The answer eludes me.") } - output.appendLine("if-else: $result2") // [END kotlin_control_flow_if_else] +} +fun runSnippet3() { // [START kotlin_control_flow_if_as_expression] - val answerStringFromIf: String = if (count == 42) { + val answerString: String = if (count == 42) { "I have the answer." } else if (count > 35) { "The answer is close." } else { "The answer eludes me." } - output.appendLine("if as expression: $answerStringFromIf") + + println(answerString) // [END kotlin_control_flow_if_as_expression] +} +fun runSnippet4() { // [START kotlin_control_flow_when_as_expression] - val answerStringFromWhen = when { + val answerString = when { count == 42 -> "I have the answer." count > 35 -> "The answer is close." else -> "The answer eludes me." } - output.appendLine("when as expression: $answerStringFromWhen") - // [END kotlin_control_flow_when_as_expression] - return output.toString() + println(answerString) + // [END kotlin_control_flow_when_as_expression] } -private fun nullSafetySnippets(): String { +fun runSnippet5() { // [START kotlin_null_safety_smart_cast] - val languageName: String? = "Kotlin" - var result = "Language name is null" + val languageName: String? = null if (languageName != null) { - // No need to write languageName?.uppercase() - result = languageName.uppercase() + // No need to write languageName?.toUpperCase() + println(languageName.toUpperCase()) } - return "Smart cast result: $result" // [END kotlin_null_safety_smart_cast] } -// [START kotlin_defining_a_function] -private fun generateAnswerString(): String { - return if (count == 42) { - "I have the answer." - } else { - "The answer eludes me" - } -} -// [END kotlin_defining_a_function] - -// [START kotlin_function_with_parameter] -private fun generateAnswerStringWithParam(countThreshold: Int): String { - return if (count > countThreshold) { - "I have the answer." - } else { - "The answer eludes me." - } -} -// [END kotlin_function_with_parameter] - - -private fun functionSnippets(): String { - val output = StringBuilder() - +fun runSnippet7() { // [START kotlin_calling_a_function] val answerString = generateAnswerString() - output.appendLine("Function call: $answerString") // [END kotlin_calling_a_function] +} +fun runSnippet9() { // [START kotlin_calling_a_function_with_argument] - val answerStringWithArg = generateAnswerStringWithParam(41) - output.appendLine("Function with argument: $answerStringWithArg") + val answerString = generateAnswerString(42) // [END kotlin_calling_a_function_with_argument] - - return output.toString() -} - -// [START kotlin_higher_order_function_definition] -private fun stringMapper(str: String, mapper: (String) -> Int): Int { - return mapper(str) } -// [END kotlin_higher_order_function_definition] - -private fun higherOrderFunctionSnippets(): String { - val output = StringBuilder() +fun runSnippet12() { // [START kotlin_lambda_declaration] val stringLengthFunc: (String) -> Int = { input -> input.length } // [END kotlin_lambda_declaration] +} +fun runSnippet13() { // [START kotlin_lambda_invocation] + val stringLengthFunc: (String) -> Int = { input -> + input.length + } + val stringLength: Int = stringLengthFunc("Android") - output.appendLine("Lambda invocation: Length of 'Android' is $stringLength") // [END kotlin_lambda_invocation] + // The original snippet did not print the result, so we don't either. +} +fun runSnippet15() { // [START kotlin_higher_order_function_call_default] - val length1 = stringMapper("Android") { input -> + stringMapper("Android", { input -> input.length - } - output.appendLine("Trailing lambda call: Length is $length1") + }) // [END kotlin_higher_order_function_call_default] +} - return output.toString() +fun runSnippet16() { + // [START kotlin_higher_order_function_call_trailing_lambda] + stringMapper("Android") { input -> + input.length + } + // [END kotlin_higher_order_function_call_trailing_lambda] } From cec5f46a2ec34d6c3d5cca88d4f14ae901d7ded4 Mon Sep 17 00:00:00 2001 From: sushantsikka <8463679+sushantsikka@users.noreply.github.com> Date: Tue, 11 Nov 2025 16:10:45 +0000 Subject: [PATCH 4/8] Apply Spotless --- .../com/example/kotlin_samples/KotlinSamples.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt index 79fd857fd..e6c97d682 100644 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.example.android.kotlin.snippets import androidx.compose.ui.text.toUpperCase From 1e4b6ab76feb6d935a4cd8f282140ad01d6cff61 Mon Sep 17 00:00:00 2001 From: Sushant Sikka Date: Wed, 12 Nov 2025 18:44:04 +0530 Subject: [PATCH 5/8] Removing build.gradle.kts and MainActivity.kt files --- .../android/learn-kotlin/build.gradle.kts | 76 ------------------- .../example/kotlin_samples/MainActivity.kt | 72 ------------------ 2 files changed, 148 deletions(-) delete mode 100644 kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts delete mode 100644 kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts deleted file mode 100644 index 39fc33f6e..000000000 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/build.gradle.kts +++ /dev/null @@ -1,76 +0,0 @@ -plugins { - alias(libs.plugins.android.application) - alias(libs.plugins.kotlin.android) - alias(libs.plugins.kotlin.compose) -} - -android { - namespace = "com.example.kotlin_samples" - compileSdk = 36 - - defaultConfig { - applicationId = "com.example.kotlin_samples" - minSdk = 24 - targetSdk = 36 - versionCode = 1 - versionName = "1.0" - - testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" - } - - buildTypes { - release { - isMinifyEnabled = false - proguardFiles( - getDefaultProguardFile("proguard-android-optimize.txt"), - "proguard-rules.pro" - ) - } - } - compileOptions { - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 - } - kotlinOptions { - jvmTarget = "11" - } - buildFeatures { - compose = true - } -} - -dependencies { - - // Core Android KTX for quality-of-life Kotlin extensions - implementation("androidx.core:core-ktx:1.13.1") - - // Activity library with Compose integration for `setContent` and `enableEdgeToEdge` - implementation("androidx.activity:activity-compose:1.9.0") - - // Jetpack Compose Bill of Materials (BOM) - // The BOM ensures that all of your Compose libraries use the same, compatible version. - val composeBom = platform("androidx.compose:compose-bom:2024.05.00") - implementation(composeBom) - androidTestImplementation(composeBom) - - // Compose UI for core components like Modifier and layout primitives - implementation("androidx.compose.ui:ui") - - // Compose Material 3 for Material Design components like Scaffold and Text - implementation("androidx.compose.material3:material3") - - // Compose Foundation for building blocks like scrolling - implementation("androidx.compose.foundation:foundation") - - // Tooling for displaying @Preview composables in Android Studio - implementation("androidx.compose.ui:ui-tooling-preview") - debugImplementation("androidx.compose.ui:ui-tooling") - - // Other standard dependencies for testing and lifecycle management - implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.1") - testImplementation("junit:junit:4.13.2") - androidTestImplementation("androidx.test.ext:junit:1.1.5") - androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") - androidTestImplementation("androidx.compose.ui:ui-test-junit4") - debugImplementation("androidx.compose.ui:ui-test-manifest") -} \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt deleted file mode 100644 index 61f59046a..000000000 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/MainActivity.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.kotlin_samples - -import android.os.Bundle -import androidx.activity.ComponentActivity -import androidx.activity.compose.setContent -import androidx.activity.enableEdgeToEdge -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.verticalScroll -import androidx.compose.material3.Scaffold -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import com.example.kotlin_samples.ui.theme.KotlinsamplesTheme - -class MainActivity : ComponentActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - enableEdgeToEdge() - setContent { - KotlinsamplesTheme { - // We replace the default Scaffold content with our new composable - SampleScreen() - } - } - } -} - -@Composable -fun SampleScreen(modifier: Modifier = Modifier) { - Scaffold(modifier = modifier.fillMaxSize()) { innerPadding -> - // Get the output from our LanguageSamples.kt file - val output = getLanguageSamplesOutput() - - // Use a scrolling Text element to display the results - Text( - text = output, - modifier = Modifier - .padding(innerPadding) - .padding(16.dp) // Add some content padding - .fillMaxSize() - .verticalScroll(rememberScrollState()) // Make content scrollable - ) - } -} - -@Preview(showBackground = true) -@Composable -fun SampleScreenPreview() { - KotlinsamplesTheme { - SampleScreen() - } -} From 1aededbfbfb267cecbb1ebb39794d1299d1bc7c0 Mon Sep 17 00:00:00 2001 From: Sushant Sikka Date: Wed, 12 Nov 2025 18:45:59 +0530 Subject: [PATCH 6/8] Update build.gradle.kts --- kotlin/build.gradle.kts | 1 - 1 file changed, 1 deletion(-) diff --git a/kotlin/build.gradle.kts b/kotlin/build.gradle.kts index 96dc5d862..12feddf2d 100644 --- a/kotlin/build.gradle.kts +++ b/kotlin/build.gradle.kts @@ -54,7 +54,6 @@ android { } } dependencies { - implementation("libs.androidx.compose.foundation:foundation") implementation(libs.androidx.lifecycle.viewmodel.ktx) implementation(libs.kotlinx.coroutines.android) testImplementation(libs.kotlinx.coroutines.test) From b7568e95886b56369efdf07f365dd21c3de6ed25 Mon Sep 17 00:00:00 2001 From: Sushant Sikka Date: Wed, 12 Nov 2025 18:55:27 +0530 Subject: [PATCH 7/8] Updating region tags --- .../example/kotlin_samples/KotlinSamples.kt | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt index e6c97d682..8860f5e54 100644 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt @@ -28,7 +28,7 @@ private const val count = 42 * bodies are unchanged. */ -// [START kotlin_defining_a_function] +// [START android_kotlin_defining_a_function] // From Snippet 6 fun generateAnswerString(): String { val answerString = if (count == 42) { @@ -39,9 +39,9 @@ fun generateAnswerString(): String { return answerString } -// [END kotlin_defining_a_function] +// [END android_kotlin_defining_a_function] -// [START kotlin_function_with_parameter] +// [START android_kotlin_function_with_parameter] // From Snippet 8 // Note: This function has the same signature as the one from Snippet 10 and 11. // It is kept here as the primary example. @@ -54,9 +54,9 @@ fun generateAnswerString(countThreshold: Int): String { return answerString } -// [END kotlin_function_with_parameter] +// [END android_kotlin_function_with_parameter] -// [START kotlin_function_with_explicit_return] +// [START android_kotlin_function_with_explicit_return] // From Snippet 10 fun generateAnswerStringWithExplicitReturn(countThreshold: Int): String { return if (count > countThreshold) { @@ -65,24 +65,24 @@ fun generateAnswerStringWithExplicitReturn(countThreshold: Int): String { "The answer eludes me." } } -// [END kotlin_function_with_explicit_return] +// [END android_kotlin_function_with_explicit_return] -// [START kotlin_function_as_single_expression] +// [START android_kotlin_function_as_single_expression] // From Snippet 11 fun generateAnswerStringAsExpression(countThreshold: Int): String = if (count > countThreshold) { "I have the answer" } else { "The answer eludes me" } -// [END kotlin_function_as_single_expression] +// [END android_kotlin_function_as_single_expression] -// [START kotlin_higher_order_function_definition] +// [START android_kotlin_higher_order_function_definition] // From Snippet 14 fun stringMapper(str: String, mapper: (String) -> Int): Int { // Invoke function return mapper(str) } -// [END kotlin_higher_order_function_definition] +// [END android_kotlin_higher_order_function_definition] /** @@ -115,7 +115,7 @@ fun main() { } fun runSnippet1() { - // [START kotlin_control_flow_if_else_if] + // [START android_kotlin_control_flow_if_else_if] if (count == 42) { println("I have the answer.") } else if (count > 35) { @@ -123,21 +123,21 @@ fun runSnippet1() { } else { println("The answer eludes me.") } - // [END kotlin_control_flow_if_else_if] + // [END android_kotlin_control_flow_if_else_if] } fun runSnippet2() { - // [START kotlin_control_flow_if_else] + // [START android_kotlin_control_flow_if_else] if (count == 42) { println("I have the answer.") } else { println("The answer eludes me.") } - // [END kotlin_control_flow_if_else] + // [END android_kotlin_control_flow_if_else] } fun runSnippet3() { - // [START kotlin_control_flow_if_as_expression] + // [START android_kotlin_control_flow_if_as_expression] val answerString: String = if (count == 42) { "I have the answer." } else if (count > 35) { @@ -147,11 +147,11 @@ fun runSnippet3() { } println(answerString) - // [END kotlin_control_flow_if_as_expression] + // [END android_kotlin_control_flow_if_as_expression] } fun runSnippet4() { - // [START kotlin_control_flow_when_as_expression] + // [START android_kotlin_control_flow_when_as_expression] val answerString = when { count == 42 -> "I have the answer." count > 35 -> "The answer is close." @@ -159,62 +159,63 @@ fun runSnippet4() { } println(answerString) - // [END kotlin_control_flow_when_as_expression] + // [END android_kotlin_control_flow_when_as_expression] } fun runSnippet5() { - // [START kotlin_null_safety_smart_cast] + // [START android_kotlin_null_safety_smart_cast] val languageName: String? = null if (languageName != null) { // No need to write languageName?.toUpperCase() println(languageName.toUpperCase()) } - // [END kotlin_null_safety_smart_cast] + // [END android_kotlin_null_safety_smart_cast] } fun runSnippet7() { - // [START kotlin_calling_a_function] + // [START android_kotlin_calling_a_function] val answerString = generateAnswerString() - // [END kotlin_calling_a_function] + // [END android_kotlin_calling_a_function] } fun runSnippet9() { - // [START kotlin_calling_a_function_with_argument] + // [START android_kotlin_calling_a_function_with_argument] val answerString = generateAnswerString(42) - // [END kotlin_calling_a_function_with_argument] + // [END android_kotlin_calling_a_function_with_argument] } fun runSnippet12() { - // [START kotlin_lambda_declaration] + // [START android_kotlin_lambda_declaration] val stringLengthFunc: (String) -> Int = { input -> input.length } - // [END kotlin_lambda_declaration] + // [END android_kotlin_lambda_declaration] } fun runSnippet13() { - // [START kotlin_lambda_invocation] + // [START android_kotlin_lambda_invocation] val stringLengthFunc: (String) -> Int = { input -> input.length } val stringLength: Int = stringLengthFunc("Android") - // [END kotlin_lambda_invocation] + // [END android_kotlin_lambda_invocation] // The original snippet did not print the result, so we don't either. } fun runSnippet15() { - // [START kotlin_higher_order_function_call_default] + // [START android_kotlin_higher_order_function_call_default] stringMapper("Android", { input -> input.length }) - // [END kotlin_higher_order_function_call_default] + // [END android_kotlin_higher_order_function_call_default] } fun runSnippet16() { - // [START kotlin_higher_order_function_call_trailing_lambda] + // [START android_kotlin_higher_order_function_call_trailing_lambda] stringMapper("Android") { input -> input.length } - // [END kotlin_higher_order_function_call_trailing_lambda] + // [END android_kotlin_higher_order_function_call_trailing_lambda] } + From 5daf0adf962ff7ac35460df3b7c94b782ed86603 Mon Sep 17 00:00:00 2001 From: Sushant Sikka Date: Wed, 12 Nov 2025 19:41:04 +0530 Subject: [PATCH 8/8] Update KotlinSamples.kt --- .../java/com/example/kotlin_samples/KotlinSamples.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt index 8860f5e54..ad986371a 100644 --- a/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt +++ b/kotlin/src/main/kotlin/com/example/android/learn-kotlin/java/com/example/kotlin_samples/KotlinSamples.kt @@ -16,8 +16,6 @@ package com.example.android.kotlin.snippets -import androidx.compose.ui.text.toUpperCase - // A top-level constant used by the snippets below. private const val count = 42 @@ -166,8 +164,7 @@ fun runSnippet5() { // [START android_kotlin_null_safety_smart_cast] val languageName: String? = null if (languageName != null) { - // No need to write languageName?.toUpperCase() - println(languageName.toUpperCase()) + println(languageName.uppercase()) } // [END android_kotlin_null_safety_smart_cast] }