From a7554428d50ed123e0c38ce447d135b2cb381859 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Wed, 30 Nov 2022 11:06:37 +1100 Subject: [PATCH] fix: compile issues in session_util.cpp and trying to fix native android builds --- libsession-util/build.gradle | 6 ++--- libsession-util/libsession-util | 2 +- .../ExampleInstrumentedTest.kt | 9 ++++---- libsession-util/src/main/cpp/CMakeLists.txt | 2 -- libsession-util/src/main/cpp/session_util.cpp | 23 +++++++++++-------- .../loki/messenger/libsession_util/Config.kt | 13 +++++++++-- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/libsession-util/build.gradle b/libsession-util/build.gradle index 85d3a58a11..12cbb892ea 100644 --- a/libsession-util/build.gradle +++ b/libsession-util/build.gradle @@ -17,9 +17,9 @@ android { cppFlags "--std=c++17" } } - ndk { - abiFilters 'x86_64', 'arm64-v8a' // ,'x86', 'armeabi-v7a' TODO: remove after the native library works properly with targets - } +// ndk { +// abiFilters 'x86_64', 'arm64-v8a' ,'x86', 'armeabi-v7a' +// } } buildTypes { diff --git a/libsession-util/libsession-util b/libsession-util/libsession-util index e52fc314e5..e98c2d383c 160000 --- a/libsession-util/libsession-util +++ b/libsession-util/libsession-util @@ -1 +1 @@ -Subproject commit e52fc314e5a5659de7c2bb60bb29dc52b80b9ace +Subproject commit e98c2d383cf5e28f665b1b2c782791987eae0544 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 ac3ff38165..6c3d67014e 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 @@ -1,13 +1,11 @@ package network.loki.messenger.libsession_util -import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 - +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.* import org.junit.Test import org.junit.runner.RunWith -import org.junit.Assert.* - /** * Instrumented test, which will execute on an Android device. * @@ -24,7 +22,8 @@ class ExampleInstrumentedTest { @Test fun jni_accessible() { - assertEquals("Hello from C++", NativeLib().stringFromJNI()) + val userProfile = UserProfile.newInstance() + assertNotNull(userProfile) } } \ 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 4ba26e9f77..468dc05bc8 100644 --- a/libsession-util/src/main/cpp/CMakeLists.txt +++ b/libsession-util/src/main/cpp/CMakeLists.txt @@ -39,8 +39,6 @@ find_library( # Sets the name of the path variable. # you want CMake to locate. log) -# Add the libsession-util library here - # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. diff --git a/libsession-util/src/main/cpp/session_util.cpp b/libsession-util/src/main/cpp/session_util.cpp index 0b622337ab..1476994ab7 100644 --- a/libsession-util/src/main/cpp/session_util.cpp +++ b/libsession-util/src/main/cpp/session_util.cpp @@ -2,31 +2,34 @@ #include #include "session/config/user_profile.hpp" +session::config::ConfigBase* ptrToConfigBase(JNIEnv *env, jobject obj) { + jclass baseClass = env->FindClass("network/loki/messenger/libsession_util/ConfigBase"); + jfieldID pointerField = env->GetFieldID(baseClass, "pointer", "J"); + return (session::config::ConfigBase*) env->GetLongField(obj, pointerField); +} session::config::UserProfile* ptrToProfile(JNIEnv* env, jobject obj) { - jclass configClass = env->FindClass("network/loki/messenger/libsession_util/Config"); + jclass configClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile"); jfieldID pointerField = env->GetFieldID(configClass, "pointer", "J"); - return (session::config::UserProfile*) reinterpret_cast(env->GetLongField(obj, pointerField)); + return (session::config::UserProfile*) env->GetLongField(obj, pointerField); } extern "C" JNIEXPORT jobject JNICALL -Java_network_loki_messenger_libsession_1util_Config_00024Companion_newInstance( +Java_network_loki_messenger_libsession_1util_UserProfile_00024Companion_newInstance( JNIEnv* env, - jobject /*this*/) { + jobject) { auto* profile = new session::config::UserProfile(); - jclass configClass = env->FindClass("network/loki/messenger/libsession_util/Config"); - jobject newConfig = env->AllocObject(configClass); - - jfieldID pointerField = env->GetFieldID(configClass, "pointer", "J"); - env->SetLongField(newConfig, pointerField, reinterpret_cast(profile)); + jclass userClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile"); + jmethodID constructor = env->GetMethodID(userClass, "", "(J)V"); + jobject newConfig = env->NewObject(userClass, constructor, reinterpret_cast(profile)); return newConfig; } extern "C" JNIEXPORT void JNICALL -Java_network_loki_messenger_libsession_1util_Config_setName( +Java_network_loki_messenger_libsession_1util_UserProfile_setName( JNIEnv* env, jobject obj, jstring newName) { 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 9fb75f50ab..97a3a232cc 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 @@ -1,9 +1,18 @@ package network.loki.messenger.libsession_util -data class Config(private val /* yucky */ pointer: Long) { + +sealed class ConfigBase(protected val /* yucky */ pointer: Long) { + +} + + +class UserProfile(pointer: Long): ConfigBase(pointer) { companion object { - external fun newInstance(): Config + init { + System.loadLibrary("session_util") + } + external fun newInstance(): UserProfile } var lastError: String? = null