From 6579271439676c6b8f3a3abd978351ef91c1c947 Mon Sep 17 00:00:00 2001 From: retif Date: Fri, 9 Feb 2024 20:09:58 +0100 Subject: [PATCH] C++ wrapper library as a part of the project --- android/app/build.gradle.kts | 17 +++++++------ android/app/jni/Android.mk | 6 ----- android/app/jni/libs/arm64-v8a/.gitkeep | 0 android/app/src/main/cpp/CMakeLists.txt | 25 +++++++++++++++++++ android/app/src/main/cpp/wrapper.cpp | 12 +++++++++ .../java/com/example/some/MainActivity.kt | 13 ++++++---- 6 files changed, 54 insertions(+), 19 deletions(-) delete mode 100644 android/app/jni/Android.mk delete mode 100644 android/app/jni/libs/arm64-v8a/.gitkeep create mode 100644 android/app/src/main/cpp/CMakeLists.txt create mode 100644 android/app/src/main/cpp/wrapper.cpp diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 3eb4e56..8e11186 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -19,6 +19,12 @@ android { abiFilters.add("arm64-v8a") } +// externalNativeBuild { +// cmake { +// arguments += "-DCMAKE_BUILD_TYPE=Release" +// } +// } + vectorDrawables { useSupportLibrary = true } @@ -50,14 +56,9 @@ android { } externalNativeBuild { - ndkBuild { - path = file("./jni/Android.mk") - } - } - - sourceSets { - this.getByName("main") { - jniLibs.srcDirs("./jni/libs") + cmake { + path = file("./src/main/cpp/CMakeLists.txt") + version = "3.22.1" } } } diff --git a/android/app/jni/Android.mk b/android/app/jni/Android.mk deleted file mode 100644 index 1904d26..0000000 --- a/android/app/jni/Android.mk +++ /dev/null @@ -1,6 +0,0 @@ -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) -LOCAL_MODULE := thingy -LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libThingy.so -include $(PREBUILT_SHARED_LIBRARY) diff --git a/android/app/jni/libs/arm64-v8a/.gitkeep b/android/app/jni/libs/arm64-v8a/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/android/app/src/main/cpp/CMakeLists.txt b/android/app/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..31a5222 --- /dev/null +++ b/android/app/src/main/cpp/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.22.1) # it wants an exact version, so 3.22 isn't precise enough + +project("cpp-wrapper") + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED 1) + +# if you set CMAKE_DEBUG_POSTFIX, then you might need to hardcode the build type to Release, +# otherwise you'll need to add the postfix value to the library name in `System.loadLibrary()` +#set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Filename postfix for libraries under DEBUG configuration") +#set(CMAKE_BUILD_TYPE "Release") + +add_library(${CMAKE_PROJECT_NAME} SHARED) # it probably makes sense to hardcode it to SHARED right away + +target_sources(${CMAKE_PROJECT_NAME} + PRIVATE + wrapper.cpp +) + +# target_link_libraries(${CMAKE_PROJECT_NAME} +# PRIVATE +# # these might not be needed +# #android +# #log +# ) diff --git a/android/app/src/main/cpp/wrapper.cpp b/android/app/src/main/cpp/wrapper.cpp new file mode 100644 index 0000000..cfd3dfd --- /dev/null +++ b/android/app/src/main/cpp/wrapper.cpp @@ -0,0 +1,12 @@ +#include +#include + +extern "C" JNIEXPORT jstring JNICALL +Java_com_example_some_MainActivity_doThingy( + JNIEnv *env, + jobject +) +{ + std::string some = "some string of text from wrapper"; + return env->NewStringUTF(some.c_str()); +} diff --git a/android/app/src/main/java/com/example/some/MainActivity.kt b/android/app/src/main/java/com/example/some/MainActivity.kt index c3ee8b0..10a3234 100644 --- a/android/app/src/main/java/com/example/some/MainActivity.kt +++ b/android/app/src/main/java/com/example/some/MainActivity.kt @@ -72,11 +72,14 @@ class MainActivity : ComponentActivity() companion object { init { - // why the fuck does it need to be a part of the file name, what's the point then - // of declaring LOCAL_MODULE in Android.mk? - // and by the way, if you'll need to load a Debug variant (libThingyd.so), - // then the name here would need to be Thingyd - System.loadLibrary("Thingy") + // if you, like a normal person, defined CMAKE_DEBUG_POSTFIX (to `d` value), + // then this thing here will fail to find the `libcpp-wrapperd.so` file, + // so you'll need to: + // - either undefine CMAKE_DEBUG_POSTFIX; + // - or change the name here to a d-postfixed variant; + // - or hardcode your wrapper library build to `-DCMAKE_BUILD_TYPE=Release`, + // either in `build.gradle.kts` or in CMakeLists.txt + System.loadLibrary("cpp-wrapper") } } }