Skip to content

Commit

Permalink
WIP JniSharedPreferencesSettings impl
Browse files Browse the repository at this point in the history
Includes sample and tests via androidInstrumentedTest and lots of JNI

JNI is terrible
  • Loading branch information
russhwolf committed Jan 10, 2025
1 parent eca5f8c commit c36f559
Show file tree
Hide file tree
Showing 39 changed files with 2,114 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ jobs:
./gradlew build --no-daemon --stacktrace
env:
GRADLE_OPTS: -Dorg.gradle.jvmargs="-Xmx3g"

- name: Connected tests
uses: reactivecircus/android-emulator-runner@62dbb605bba737720e10b196cb4220d374026a6d # v2.33.0
with:
api-level: 35
script: ./gradlew multiplatform-settings:androidNative-test-helpers:prepareDebugAndroidNdkSo multiplatform-settings:connectedCheck
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ nexusPublishing {
}

apiValidation {
ignoredProjects.add("tests")
ignoredProjects.addAll(listOf("tests", "androidNative-test-helpers"))
@OptIn(kotlinx.validation.ExperimentalBCVApi::class)
klib {
enabled = true
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
org.gradle.jvmargs=-Xmx2g
android.useAndroidX=true
kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.mpp.enableCInteropCommonization=true
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ androidx-preference = "1.2.1"
androidx-startup = "1.2.0"
androidx-test = "1.6.1"
androidx-testExt = "1.2.1"
androidx-test-orchestrator = "1.5.1"

binaryCompatibilityValidator = "0.16.3"
nexus-publish = "2.0.0"
Expand All @@ -44,6 +45,9 @@ androidx-datastore-preferences-core = { module = "androidx.datastore:datastore-p
androidx-preference = { module = "androidx.preference:preference", version.ref = "androidx-preference" }
androidx-startup-runtime = { module = "androidx.startup:startup-runtime", version.ref = "androidx-startup" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidx-test" }
androidx-test-rules = { module = "androidx.test:rules", version.ref = "androidx-test" }
androidx-test-orchestrator = { module = "androidx.test:orchestrator", version.ref = "androidx-test-orchestrator" }
androidx-test-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-testExt" }

junit = { module = "junit:junit", version.ref = "junit" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
97 changes: 97 additions & 0 deletions multiplatform-settings/androidNative-test-helpers/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2019 Russell Wolf
*
* 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
*
* http://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.
*/

import org.gradle.internal.extensions.stdlib.capitalized
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode

plugins {
id("standard-configuration-without-android")
}

kotlin {
explicitApi = ExplicitApiMode.Disabled

listOf(
androidNativeArm32(),
androidNativeArm64(),
androidNativeX86(),
androidNativeX64()
).forEach { target ->
target.binaries.sharedLib("androidNativeTestHelpers")
}

sourceSets {
commonMain {
dependencies {
implementation(project(":multiplatform-settings"))
}
}
}
}

// Adapted from https://github.com/LandryNorris/JniUtils/blob/d7bd76996fa0c6fc34d408a2df8d5c1e27b56801/sample/build.gradle.kts#L66
enum class Platform(val platformName: String, val archName: String) {
AndroidArm64("androidNativeArm64", "arm64-v8a"),
AndroidArm32("androidNativeArm32", "armeabi-v7a"),
AndroidX64("androidNativeX64", "x86_64"),
AndroidX86("androidNativeX86", "x86")
}

/**
* Create a task to prepare binary for a given platform.
*/
fun createSoPrepareTask(platform: Platform, buildtype: String): Task {
return tasks.create("prepare${buildtype.capitalized()}${platform.platformName.capitalized()}So") {
dependsOn("linkAndroidNativeTestHelpers${buildtype.capitalized()}Shared${platform.platformName.capitalized()}")
val srcFolder =
layout.buildDirectory.file("bin/${platform.platformName}/androidNativeTestHelpers${buildtype.capitalized()}Shared")
val destFolder =
rootProject.projectDir.resolve("multiplatform-settings/src/androidInstrumentedTest/jniLibs/${platform.archName}")

doLast {
copy {
from(srcFolder)
into(destFolder)
include("*.so")
}
}
}
}

val debugBinaryTasks = listOf(
createSoPrepareTask(Platform.AndroidArm64, "debug"),
createSoPrepareTask(Platform.AndroidArm32, "debug"),
createSoPrepareTask(Platform.AndroidX64, "debug"),
createSoPrepareTask(Platform.AndroidX86, "debug")
)
val releaseBinaryTasks = listOf(
createSoPrepareTask(Platform.AndroidArm64, "release"),
createSoPrepareTask(Platform.AndroidArm32, "release"),
createSoPrepareTask(Platform.AndroidX64, "release"),
createSoPrepareTask(Platform.AndroidX86, "release")
)

// TODO wire these tasks into androidUnitTest
tasks.create("prepareDebugAndroidNdkSo") {
debugBinaryTasks.forEach {
this.dependsOn(it)
}
}
tasks.create("prepareReleaseAndroidNdkSo") {
releaseBinaryTasks.forEach {
this.dependsOn(it)
}
}
Loading

0 comments on commit c36f559

Please sign in to comment.