From 0d2f52469cb33d8c7fc406f78305c5478c19d878 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Wed, 8 Nov 2023 04:07:04 +0700 Subject: [PATCH] android: prevent UnsatisfiedLinkError on Android <9 --- .../common/platform/AppCommon.android.kt | 7 +++++-- .../src/commonMain/cpp/android/CMakeLists.txt | 19 +++++++++++++++++++ .../common/src/commonMain/cpp/android/patch.c | 6 ++++++ .../src/commonMain/cpp/android/simplex-api.c | 4 ++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 apps/multiplatform/common/src/commonMain/cpp/android/patch.c diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/AppCommon.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/AppCommon.android.kt index 192f3dcc29..24e5ab0a58 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/AppCommon.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/AppCommon.android.kt @@ -61,8 +61,11 @@ fun initHaskell() { } } - System.loadLibrary("app-lib") - + try { + System.loadLibrary("app-lib") + } catch (e: UnsatisfiedLinkError) { + System.loadLibrary("apppatched-lib") + } s.acquire() pipeStdOutToSocket(socketName) diff --git a/apps/multiplatform/common/src/commonMain/cpp/android/CMakeLists.txt b/apps/multiplatform/common/src/commonMain/cpp/android/CMakeLists.txt index 44cb31d424..cdd5dc1123 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/android/CMakeLists.txt +++ b/apps/multiplatform/common/src/commonMain/cpp/android/CMakeLists.txt @@ -23,6 +23,15 @@ add_library( # Sets the name of the library. # Provides a relative path to your source file(s). simplex-api.c) +add_library( # Sets the name of the library. + apppatched-lib + + # Sets the library as a shared library. + SHARED + + # Provides a relative path to your source file(s). + patch.c simplex-api.c) + # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library @@ -65,3 +74,13 @@ target_link_libraries( # Specifies the target library. # Links the target library to the log library # included in the NDK. ${log-lib}) + + +target_link_libraries( # Specifies the target library. + apppatched-lib + + simplex support + + # Links the target library to the log library + # included in the NDK. + ${log-lib}) diff --git a/apps/multiplatform/common/src/commonMain/cpp/android/patch.c b/apps/multiplatform/common/src/commonMain/cpp/android/patch.c new file mode 100644 index 0000000000..2145a6950a --- /dev/null +++ b/apps/multiplatform/common/src/commonMain/cpp/android/patch.c @@ -0,0 +1,6 @@ +#include + +extern int getentropy(void* __buffer, size_t __buffer_size) +{ + return 1; +} diff --git a/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c b/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c index 351ed93c97..cbe1899691 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c +++ b/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c @@ -19,6 +19,8 @@ extern void __strcat_chk_generic(void){}; extern void __libc_globals(void){}; extern void __rel_iplt_start(void){}; +int getentropy(void* __buffer, size_t __buffer_size); + // Android 9 only, not 13 extern void reallocarray(void){}; @@ -32,6 +34,8 @@ Java_chat_simplex_common_platform_CoreKt_pipeStdOutToSocket(JNIEnv *env, __unuse JNIEXPORT void JNICALL Java_chat_simplex_common_platform_CoreKt_initHS(__unused JNIEnv *env, __unused jclass clazz) { + //char tmp; + //__android_log_print(ANDROID_LOG_ERROR, "simplex", "entropy %d\n", getentropy(&tmp, sizeof(tmp))); hs_init(NULL, NULL); setLineBuffering(); }