-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share object between JNI and JVM in sample app
- Loading branch information
1 parent
048454b
commit 046f92d
Showing
15 changed files
with
124 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
sample/src/androidTest/java/io/github/landrynorris/sample/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
sample/src/commonMain/kotlin/io/github/landrynorris/jni/sample/SharedClass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.landrynorris.jni.sample | ||
|
||
class SharedClass(val x: Double, val dataHolder: DataHolder) | ||
|
||
data class DataHolder(val i: Int, val d: Double, val s: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
sample/src/ndkMain/kotlin/io/github/landrynorris/jni/sample/SharedClassJni.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.landrynorris.jni.sample | ||
|
||
import io.github.landrynorris.jniutils.* | ||
import kotlinx.cinterop.CPointer | ||
import platform.android.JNIEnvVar | ||
import platform.android.jobject | ||
|
||
fun jobject.toSharedClass(env: CPointer<JNIEnvVar>): SharedClass? { | ||
val clazz = env.findClass("io/github/landrynorris/jni/sample/SharedClass") ?: error("Unable to find class") | ||
if(!env.isInstanceOf(this, clazz)) return null | ||
val x = env.getDoubleField(this, "x", clazz) | ||
val holder = env.getObjectField(this, "dataHolder", clazz, signature<DataHolder>().toString()) | ||
return SharedClass(x, holder?.toDataHolder(env)!!) | ||
} | ||
|
||
fun jobject.toDataHolder(env: CPointer<JNIEnvVar>): DataHolder? { | ||
val clazz = env.findClass("io/github/landrynorris/jni/sample/DataHolder") ?: error("Unable to find class") | ||
if(!env.isInstanceOf(this, clazz)) return null | ||
val i = env.getIntField(this, "i", clazz) | ||
val d = env.getDoubleField(this, "d", clazz) | ||
val s = env.getStringField(this, "s", clazz) | ||
|
||
return DataHolder(i, d, s) | ||
} |