Skip to content

Commit

Permalink
feat: Add KMP module
Browse files Browse the repository at this point in the history
Added a new KMP module named "shared" to the project. This module includes:

- Support for Android and iOS
 platforms.
- A function `createCrashInKMPModule` in the Android source set that triggers a crash.
- Basic unit tests for both Android and common source sets.

This change also involved updating the app module to trigger the crash from the KMP module and upgrading Gradle to version 8.
10.2.
  • Loading branch information
devtoolsqa committed Oct 3, 2024
1 parent 0210a78 commit 5377ef0
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 7 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {

defaultConfig {
applicationId "com.quality.appqualityinsightsqaapp"
minSdk 24
minSdk 27
targetSdk 34
versionCode 6
versionName "6.0"
Expand Down Expand Up @@ -61,4 +61,5 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation(project(":shared"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.example.lib.JavaLibraryClass.createCrashInJavaLibrary;
import static com.example.mylibrary.AndroidLibraryClass.createCrashInAndroidLibrary;
import static com.google.play.dynamic.filters.opted.shared.Platform_androidKt.createCrashInKMPModule;

import android.content.Intent;
import android.os.Bundle;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class MainActivity extends AppCompatActivity {
Button andLibButton2;
Button javalibCrash;
Button realLifeCrashScenario;
Button kmpCrash;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -67,12 +69,14 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
throw new RuntimeException("Test Crash vcs");
throw new RuntimeException("Test Crash vcs");

}
});
andLibButton=findViewById(R.id.and_lib_crash);
andLibButton2=findViewById(R.id.and_lib_crash2);
javalibCrash=findViewById(R.id.java_lib_crash);
kmpCrash=findViewById(R.id.kmp_crash);
realLifeCrashScenario = findViewById(R.id.real_life_crash_scenario);

andLibButton.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -104,6 +108,9 @@ public void onClick(View view) {
realLifeCrashScenario.setOnClickListener(v -> {
startActivity(new Intent(this, RealCrashScenariosMainActivity.class));
});
kmpCrash.setOnClickListener(v -> {
createCrashInKMPModule();
});
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
Expand Down Expand Up @@ -70,4 +71,13 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/kmp_crash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Create crash in KMP module"
app:layout_constraintBottom_toTopOf="@+id/java_lib_crash"
tools:layout_editor_absoluteX="159dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ buildscript {
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.5.0-rc01' apply false
id 'com.android.library' version '8.5.0-rc01' apply false
id 'com.android.test' version '8.5.0-rc01' apply false
id 'com.android.application' version '8.7.0' apply false
id 'com.android.library' version '8.7.0' apply false
id 'com.android.test' version '8.7.0' apply false
id 'org.jetbrains.kotlin.multiplatform' version '2.0.0' apply false
id 'com.android.kotlin.multiplatform.library' version '8.7.0' apply false
}
2 changes: 1 addition & 1 deletion flavorsModule/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {

defaultConfig {
applicationId "com.example.flavorsmodule"
minSdk 24
minSdk 27
targetSdk 34
versionCode 6
versionName "6.0"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Oct 27 14:35:03 PDT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 3 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ plugins {
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}
5 changes: 5 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ pluginManagement {
mavenCentral()
}
}

plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
Expand All @@ -17,3 +21,4 @@ include ':app'
include ':flavorsModule'
include ':mylibrary'
include ':lib'
include ':shared'
103 changes: 103 additions & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
plugins {
id("org.jetbrains.kotlin.multiplatform")
id("com.android.kotlin.multiplatform.library")
}

kotlin {

// Target declarations - add or remove as needed below. These define
// which platforms this KMP module supports.
// See: https://kotlinlang.org/docs/multiplatform-discover-project.html#targets
androidLibrary {
namespace = "com.google.play.dynamic.filters.opted.shared"
compileSdk = 34
minSdk = 24

withAndroidTestOnJvmBuilder {
compilationName = "unitTest"
defaultSourceSetName = "androidUnitTest"
}

withAndroidTestOnDeviceBuilder {
compilationName = "instrumentedTest"
defaultSourceSetName = "androidInstrumentedTest"
sourceSetTreeName = "test"
}.configure {
instrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
}

// For iOS targets, this is also where you should
// configure native binary output. For more information, see:
// https://kotlinlang.org/docs/multiplatform-build-native-binaries.html#build-xcframeworks

// A step-by-step guide on how to include this library in an XCode
// project can be found here:
// https://developer.android.com/kotlin/multiplatform/migrate
val xcfName = "shared"

iosX64 {
binaries.framework {
baseName = xcfName
}
}

iosArm64 {
binaries.framework {
baseName = xcfName
}
}

iosSimulatorArm64 {
binaries.framework {
baseName = xcfName
}
}

// Source set declarations.
// Declaring a target automatically creates a source set with the same name. By default, the
// Kotlin Gradle Plugin creates additional source sets that depend on each other, since it is
// common to share sources between related targets.
// See: https://kotlinlang.org/docs/multiplatform-hierarchy.html
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:2.0.20")
// Add KMP dependencies here
}
}

commonTest {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-test:2.0.0")
}
}

androidMain {
dependencies {
// Add Android-specific dependencies here. Note that this source set depends on
// commonMain by default and will correctly pull the Android artifacts of any KMP
// dependencies declared in commonMain.
}
}

getByName("androidInstrumentedTest") {
dependencies {
implementation("androidx.test:runner:1.6.2")
implementation("androidx.test:core:1.6.1")
implementation("androidx.test.ext:junit:1.2.1")
}
}

iosMain {
dependencies {
// Add iOS-specific dependencies here. This a source set created by Kotlin Gradle
// Plugin (KGP) that each specific iOS target (e.g., iosX64) depends on as
// part of KMP’s default source set hierarchy. Note that this source set depends
// on common by default and will correctly pull the iOS artifacts of any
// KMP dependencies declared in commonMain.
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.google.play.dynamic.filters.opted.shared

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals(
"com.google.play.dynamic.filters.opted.shared.test",
appContext.packageName
)
}
}
4 changes: 4 additions & 0 deletions shared/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.google.play.dynamic.filters.opted.shared

actual fun platform() = "Android"

fun createCrashInKMPModule()
{
val name="test"
val c=name.get(10)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.google.play.dynamic.filters.opted.shared

import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.google.play.dynamic.filters.opted.shared

expect fun platform(): String
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.google.play.dynamic.filters.opted.shared

actual fun platform() = "iOS"

0 comments on commit 5377ef0

Please sign in to comment.