Skip to content

Commit

Permalink
C++ wrapper library as a part of the project
Browse files Browse the repository at this point in the history
  • Loading branch information
retifrav committed Feb 9, 2024
1 parent 0997203 commit 6579271
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
17 changes: 9 additions & 8 deletions android/app/build.gradle.kts
Expand Up @@ -19,6 +19,12 @@ android {
abiFilters.add("arm64-v8a")
}

// externalNativeBuild {
// cmake {
// arguments += "-DCMAKE_BUILD_TYPE=Release"
// }
// }

vectorDrawables {
useSupportLibrary = true
}
Expand Down Expand Up @@ -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"
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions android/app/jni/Android.mk

This file was deleted.

Empty file.
25 changes: 25 additions & 0 deletions 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
# )
12 changes: 12 additions & 0 deletions android/app/src/main/cpp/wrapper.cpp
@@ -0,0 +1,12 @@
#include <jni.h>
#include <string>

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());
}
13 changes: 8 additions & 5 deletions android/app/src/main/java/com/example/some/MainActivity.kt
Expand Up @@ -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")
}
}
}
Expand Down

0 comments on commit 6579271

Please sign in to comment.