From ad00aeac4a7d48ef7c34372be972bd2290faf0f6 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Wed, 30 Nov 2022 11:48:41 +1100 Subject: [PATCH] fix: use the build static instead of build shared, add a free which works question mark(?) --- .../libsession_util/ExampleInstrumentedTest.kt | 13 +++++++++++++ libsession-util/src/main/cpp/CMakeLists.txt | 4 ++-- libsession-util/src/main/cpp/session_util.cpp | 16 ++++++++++++++++ .../loki/messenger/libsession_util/Config.kt | 14 +++++++------- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt index 6c3d67014e..b76380f616 100644 --- a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt +++ b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt @@ -24,6 +24,19 @@ class ExampleInstrumentedTest { fun jni_accessible() { val userProfile = UserProfile.newInstance() assertNotNull(userProfile) + userProfile.free() + } + + @Test + fun jni_setting_getting() { + val userProfile = UserProfile.newInstance() + val newName = "test" + println("Name being set via JNI call: $newName") + userProfile.setName(newName) + val nameFromNative = userProfile.getName() + assertEquals(newName, nameFromNative) + println("Name received by JNI call: $nameFromNative") + userProfile.free() } } \ No newline at end of file diff --git a/libsession-util/src/main/cpp/CMakeLists.txt b/libsession-util/src/main/cpp/CMakeLists.txt index 468dc05bc8..753c99e8e9 100644 --- a/libsession-util/src/main/cpp/CMakeLists.txt +++ b/libsession-util/src/main/cpp/CMakeLists.txt @@ -13,8 +13,8 @@ project("session_util") # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. - -set(BUILD_SHARED_LIBS ON CACHE BOOL "") +set(STATIC_BUNDLE ON) +#set(BUILD_SHARED_LIBS ON CACHE BOOL "") add_subdirectory(../../../libsession-util libsession) add_library( # Sets the name of the library. diff --git a/libsession-util/src/main/cpp/session_util.cpp b/libsession-util/src/main/cpp/session_util.cpp index 1476994ab7..5b8d6fb251 100644 --- a/libsession-util/src/main/cpp/session_util.cpp +++ b/libsession-util/src/main/cpp/session_util.cpp @@ -35,4 +35,20 @@ Java_network_loki_messenger_libsession_1util_UserProfile_setName( jstring newName) { auto profile = ptrToProfile(env, obj); profile->set_name(env->GetStringUTFChars(newName, nullptr)); +} + +extern "C" +JNIEXPORT jstring JNICALL +Java_network_loki_messenger_libsession_1util_UserProfile_getName(JNIEnv *env, jobject obj) { + auto profile = ptrToProfile(env, obj); + auto name = profile->get_name(); + jstring returnString = env->NewStringUTF(name->c_str()); + return returnString; +} + +extern "C" +JNIEXPORT void JNICALL +Java_network_loki_messenger_libsession_1util_UserProfile_free(JNIEnv *env, jobject obj) { + auto profile = ptrToProfile(env, obj); + delete profile; } \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt index 97a3a232cc..a101549e2b 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt @@ -2,22 +2,22 @@ package network.loki.messenger.libsession_util sealed class ConfigBase(protected val /* yucky */ pointer: Long) { - + companion object { + init { + System.loadLibrary("session_util") + } + } } class UserProfile(pointer: Long): ConfigBase(pointer) { - companion object { init { System.loadLibrary("session_util") } external fun newInstance(): UserProfile } - - var lastError: String? = null - external fun setName(newName: String) - - + external fun getName(): String + external fun free() } \ No newline at end of file