From 9774bd858fa88b5cbe677ba5a9751819c5fc519f Mon Sep 17 00:00:00 2001 From: tarsin Date: Mon, 13 Feb 2023 18:28:46 +0800 Subject: [PATCH] Remove KNI support Not funny, reland it when we really need Kotlin Native --- app/build.gradle.kts | 1 - app/src/main/java/com/hippo/Native.kt | 8 -- gradle.properties | 2 - native/.gitignore | 1 - native/build.gradle.kts | 91 ------------------- .../moe/tarsin/ehviewer/PlatformTypes.kt | 10 -- .../moe/tarsin/ehviewer/PlatformTypes.kt | 7 -- .../moe/tarsin/ehviewer/PlatformTypes.kt | 7 -- .../moe/tarsin/ehviewer/PlatformTypes.kt | 10 -- .../kotlin/moe/tarsin/ehviewer/KNIUtils.kt | 25 ----- .../kotlin/moe/tarsin/ehviewer/LogUtils.kt | 10 -- .../moe/tarsin/ehviewer/PlatformTypes.kt | 5 - .../kotlin/moe/tarsin/ehviewer/RewriteGif.kt | 13 --- .../kotlin/moe/tarsin/ehviewer/mmap.kt | 37 -------- settings.gradle.kts | 2 +- 15 files changed, 1 insertion(+), 228 deletions(-) delete mode 100644 native/.gitignore delete mode 100644 native/build.gradle.kts delete mode 100644 native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt delete mode 100644 native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt delete mode 100644 native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt delete mode 100644 native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt delete mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt delete mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/LogUtils.kt delete mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt delete mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt delete mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a844d4697..9e12c9968 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -195,7 +195,6 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4") implementation("org.jetbrains.kotlinx:kotlinx-serialization-cbor:1.5.0-RC") implementation("org.jsoup:jsoup:1.15.3") - implementation(project(":native")) } configurations.all { diff --git a/app/src/main/java/com/hippo/Native.kt b/app/src/main/java/com/hippo/Native.kt index ea8f70f7f..dda57268c 100644 --- a/app/src/main/java/com/hippo/Native.kt +++ b/app/src/main/java/com/hippo/Native.kt @@ -18,20 +18,12 @@ package com.hippo import java.io.FileDescriptor -import java.nio.ByteBuffer object Native { fun initialize() { System.loadLibrary("ehviewer") - System.loadLibrary("ehviewer_kotlin_native") } @JvmStatic external fun getFd(fd: FileDescriptor?): Int - - @JvmStatic - external fun mapFd(fd: Int, capability: Long): ByteBuffer? - - @JvmStatic - external fun unmapDirectByteBuffer(buffer: ByteBuffer) } diff --git a/gradle.properties b/gradle.properties index d929bc346..f40354e6d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,5 +21,3 @@ org.gradle.caching=true android.useAndroidX=true android.enableAppCompileTimeRClass=true android.experimental.enableNewResourceShrinker.preciseShrinking=true -kotlin.mpp.androidGradlePluginCompatibility.nowarn=true -kotlin.mpp.androidSourceSetLayoutVersion=2 diff --git a/native/.gitignore b/native/.gitignore deleted file mode 100644 index 42afabfd2..000000000 --- a/native/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/build \ No newline at end of file diff --git a/native/build.gradle.kts b/native/build.gradle.kts deleted file mode 100644 index 576542be4..000000000 --- a/native/build.gradle.kts +++ /dev/null @@ -1,91 +0,0 @@ -import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget -import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType -import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink -import org.jetbrains.kotlin.konan.target.KonanTarget.* - -plugins { - kotlin("multiplatform") - id("com.android.library") -} - -val jniLibDir = File(project.buildDir, arrayOf("generated", "jniLibs").joinToString(File.separator)) - -val sharedLib_name_prefix = "ehviewer_kotlin_native" - -kotlin { - android() - - val nativeConfigure: KotlinNativeTarget.() -> Unit = { - binaries { - sharedLib(sharedLib_name_prefix) { - freeCompilerArgs = mutableListOf("-Xallocator=std") - - linkTask.doLast { - copy { - from(outputFile) - - val typeName = if (buildType == NativeBuildType.DEBUG) "Debug" else "Release" - val abiDirName = when(target.konanTarget) { - ANDROID_ARM32 -> "armeabi-v7a" - ANDROID_ARM64 -> "arm64-v8a" - ANDROID_X86 -> "x86" - ANDROID_X64 -> "x86_64" - else -> "unknown" - } - - into(file("$jniLibDir/$typeName/$abiDirName")) - } - } - - afterEvaluate { - val preBuild by tasks.getting - preBuild.dependsOn(linkTask) - } - } - } - } - - androidNativeArm32(configure = nativeConfigure) - androidNativeArm64(configure = nativeConfigure) - androidNativeX86(configure = nativeConfigure) - androidNativeX64(configure = nativeConfigure) - - sourceSets { - val androidNativeArm32Main by getting - val androidNativeArm64Main by getting - val androidNativeX86Main by getting - val androidNativeX64Main by getting - - val nativeMain by creating { - androidNativeArm32Main.dependsOn(this) - androidNativeArm64Main.dependsOn(this) - androidNativeX86Main.dependsOn(this) - androidNativeX64Main.dependsOn(this) - } - } - -} - -android { - compileSdk = 33 - buildToolsVersion = "33.0.1" - - defaultConfig { - minSdk = 28 - } - - sourceSets { - getByName("debug").jniLibs.srcDirs("$jniLibDir/Debug") - getByName("release").jniLibs.srcDirs("$jniLibDir/Release") - } - namespace = "moe.tarsin.ehviewer" -} - -tasks.withType(KotlinNativeLink::class.java) { - outputs.cacheIf { false } -} - -repositories { - mavenCentral() - google() -} diff --git a/native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt deleted file mode 100644 index c109357e7..000000000 --- a/native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt +++ /dev/null @@ -1,10 +0,0 @@ -package moe.tarsin.ehviewer - -import platform.posix.size_t - -private val MAX_ALLOWED_CONVERT_VALUE = size_t.MAX_VALUE.toLong() - -actual fun Long.tosize_t(): size_t { - check(this <= MAX_ALLOWED_CONVERT_VALUE) - return toUInt() -} diff --git a/native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt deleted file mode 100644 index a627f3b21..000000000 --- a/native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt +++ /dev/null @@ -1,7 +0,0 @@ -package moe.tarsin.ehviewer - -import platform.posix.size_t - -actual fun Long.tosize_t(): size_t { - return toULong() -} diff --git a/native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt deleted file mode 100644 index a627f3b21..000000000 --- a/native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt +++ /dev/null @@ -1,7 +0,0 @@ -package moe.tarsin.ehviewer - -import platform.posix.size_t - -actual fun Long.tosize_t(): size_t { - return toULong() -} diff --git a/native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt deleted file mode 100644 index c109357e7..000000000 --- a/native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt +++ /dev/null @@ -1,10 +0,0 @@ -package moe.tarsin.ehviewer - -import platform.posix.size_t - -private val MAX_ALLOWED_CONVERT_VALUE = size_t.MAX_VALUE.toLong() - -actual fun Long.tosize_t(): size_t { - check(this <= MAX_ALLOWED_CONVERT_VALUE) - return toUInt() -} diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt deleted file mode 100644 index dea930010..000000000 --- a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt +++ /dev/null @@ -1,25 +0,0 @@ -package moe.tarsin.ehviewer - -import kotlinx.cinterop.COpaquePointer -import kotlinx.cinterop.CPointer -import kotlinx.cinterop.invoke -import kotlinx.cinterop.pointed -import platform.android.JNIEnvVar -import platform.android.JNINativeInterface -import platform.android.jobject - -fun CPointer.getNativeInterface(): JNINativeInterface { - return pointed.pointed!! -} - -fun CPointer.getDirectBufferAddress(buffer: jobject): COpaquePointer { - return getNativeInterface().GetDirectBufferAddress!!.invoke(this, buffer)!! -} - -fun CPointer.getDirectBufferCapacity(buffer: jobject): Long { - return getNativeInterface().GetDirectBufferCapacity!!.invoke(this, buffer) -} - -fun CPointer.newDirectByteBuffer(addr: COpaquePointer, capability: Long): jobject { - return getNativeInterface().NewDirectByteBuffer!!.invoke(this, addr, capability)!! -} diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/LogUtils.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/LogUtils.kt deleted file mode 100644 index fbc3549dd..000000000 --- a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/LogUtils.kt +++ /dev/null @@ -1,10 +0,0 @@ -package moe.tarsin.ehviewer - -import platform.android.ANDROID_LOG_WARN -import platform.android.__android_log_print - -private val KNI_NATIVE_TAG = "KNI" - -fun Warn(message: String) { - __android_log_print(ANDROID_LOG_WARN.toInt(), KNI_NATIVE_TAG, "%s", message) -} diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt deleted file mode 100644 index db57e9267..000000000 --- a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt +++ /dev/null @@ -1,5 +0,0 @@ -package moe.tarsin.ehviewer - -import platform.posix.size_t - -expect fun Long.tosize_t(): size_t diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt deleted file mode 100644 index 8772877f3..000000000 --- a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt +++ /dev/null @@ -1,13 +0,0 @@ -package moe.tarsin.ehviewer - -import kotlinx.cinterop.CPointer -import platform.android.JNIEnvVar -import platform.android.jclass -import platform.android.jobject - -@Suppress("UNUSED_PARAMETER") -@CName("Java_com_hippo_Native_rewriteGif") -fun rewriteGif(env: CPointer, clazz: jclass, buffer: jobject) { - val ptr = env.getDirectBufferAddress(buffer) - val size = env.getDirectBufferCapacity(buffer) -} diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt deleted file mode 100644 index a2531e4e2..000000000 --- a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt +++ /dev/null @@ -1,37 +0,0 @@ -package moe.tarsin.ehviewer - -import kotlinx.cinterop.CPointer -import kotlinx.cinterop.UnsafeNumber -import kotlinx.cinterop.interpretCPointer -import kotlinx.cinterop.nativeNullPtr -import kotlinx.cinterop.value -import platform.android.JNIEnvVar -import platform.android.jclass -import platform.android.jint -import platform.android.jlong -import platform.android.jobject -import platform.posix.MAP_PRIVATE -import platform.posix.PROT_READ -import platform.posix.PROT_WRITE -import platform.posix.errno -import platform.posix.mmap -import platform.posix.munmap - -@OptIn(UnsafeNumber::class) -@Suppress("UNUSED_PARAMETER") -@CName("Java_com_hippo_Native_mapFd") -fun mapFd(env: CPointer, clazz: jclass, fd: jint, capability: jlong): jobject { - val result = mmap(null, capability.tosize_t(), PROT_READ or PROT_WRITE, MAP_PRIVATE, fd, 0) - result ?: Warn("mmap call with fd:${fd} capability:${capability} failed!, errno:${errno}") - return result?.let { env.newDirectByteBuffer(result, capability) } ?: interpretCPointer(nativeNullPtr)!! -} - -@Suppress("UNUSED_PARAMETER") -@OptIn(UnsafeNumber::class) -@CName("Java_com_hippo_Native_unmapDirectByteBuffer") -fun unmapFd(env: CPointer, clazz: jclass, buffer: jobject) { - val addr = env.getDirectBufferAddress(buffer) - val size = env.getDirectBufferCapacity(buffer) - val result = munmap(addr, size.tosize_t()) - if (result != 0) Warn("munmap call with addr:${addr} size:${size} failed!, errno:${errno}") -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 19013eace..4f3839b36 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -8,4 +8,4 @@ pluginManagement { enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") rootProject.name = "EhViewer" -include(":app", ":native") +include(":app")