From 123ffbeafa124c7cac51cffe28b0bdd9004cbf69 Mon Sep 17 00:00:00 2001 From: infactum Date: Thu, 10 Sep 2020 23:26:24 +0300 Subject: [PATCH] android support --- CMakeLists.txt | 20 +++++++++++++++-- include/IAndroidComponentHelper.h | 24 +++++++++++++++++++++ src/exports.cpp | 5 ++--- src/jnienv.cpp | 36 +++++++++++++++++++++++++++++++ src/jnienv.h | 8 +++++++ 5 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 include/IAndroidComponentHelper.h create mode 100644 src/jnienv.cpp create mode 100644 src/jnienv.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d05858..bc521ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.12) +cmake_minimum_required(VERSION 3.10) project(SampleAddIn) set(CMAKE_CXX_STANDARD 17) @@ -8,7 +8,7 @@ option(CASE_INSENSITIVE "Case insensitive method names" OFF) option(STATIC_CRT "Static CRT linkage" OFF) option(OUT_PARAMS "Support output parameters" OFF) -add_library(${TARGET} SHARED +list(APPEND SOURCES src/addin.def src/stdafx.h src/dllmain.cpp @@ -18,6 +18,15 @@ add_library(${TARGET} SHARED src/SampleAddIn.cpp src/SampleAddIn.h) +if (ANDROID) + list(APPEND SOURCES + src/jnienv.cpp + src/jnienv.h) +endif () + +add_library(${TARGET} SHARED + ${SOURCES}) + target_compile_definitions(${TARGET} PRIVATE UNICODE _UNICODE) @@ -53,4 +62,11 @@ if (UNIX) set(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}") set(CMAKE_CXX_FLAGS "-m32 ${CMAKE_CXX_FLAGS}") endif () +endif () + +if (ANDROID) + if (CMAKE_BUILD_TYPE STREQUAL Release) + add_custom_command(TARGET ${TARGET} POST_BUILD + COMMAND ${CMAKE_STRIP} ${CMAKE_SHARED_LIBRARY_PREFIX}${TARGET}${CMAKE_SHARED_LIBRARY_SUFFIX}) + endif () endif () \ No newline at end of file diff --git a/include/IAndroidComponentHelper.h b/include/IAndroidComponentHelper.h new file mode 100644 index 0000000..0807406 --- /dev/null +++ b/include/IAndroidComponentHelper.h @@ -0,0 +1,24 @@ +/* + * Warning!!! + * DO NOT ALTER THIS FILE! + */ + +#ifndef __IANDROIDCOMPONENTHELPER_H__ +#define __IANDROIDCOMPONENTHELPER_H__ + +#include "types.h" + +#if defined(__ANDROID__) +#include + +struct IAndroidComponentHelper : + public IInterface +{ + virtual jobject ADDIN_API GetActivity() = 0; + + virtual jclass ADDIN_API FindClass(const WCHAR_T* className) = 0; +}; + +#endif //__ANDROID__ + +#endif //__IANDROIDCOMPONENTHELPER_H__ diff --git a/src/exports.cpp b/src/exports.cpp index 874ea5e..f315392 100644 --- a/src/exports.cpp +++ b/src/exports.cpp @@ -27,7 +27,8 @@ #endif const WCHAR_T *GetClassNames() { - static char16_t cls_names[] = u"Sample|SampleAlias"; + // Might contain multiple class names seperated by | + static char16_t cls_names[] = u"Sample"; return reinterpret_cast(cls_names); } @@ -36,8 +37,6 @@ long GetClassObject(const WCHAR_T *clsName, IComponentBase **pInterface) { auto cls_name = std::u16string(reinterpret_cast(clsName)); if (cls_name == u"Sample") { *pInterface = new SampleAddIn; - } else if (cls_name == u"SampleAlias") { - *pInterface = new SampleAddIn; } return (long) *pInterface; } diff --git a/src/jnienv.cpp b/src/jnienv.cpp new file mode 100644 index 0000000..581a421 --- /dev/null +++ b/src/jnienv.cpp @@ -0,0 +1,36 @@ +#include +#include "jnienv.h" + +static JavaVM *sJavaVM = nullptr; + +JNIEnv *getJniEnv() { + JNIEnv *env = nullptr; + + switch (sJavaVM->GetEnv((void **) &env, JNI_VERSION_1_6)) { + case JNI_OK: + return env; + + case JNI_EDETACHED: { + JavaVMAttachArgs args; + args.name = nullptr; + args.group = nullptr; + args.version = JNI_VERSION_1_6; + + if (!sJavaVM->AttachCurrentThreadAsDaemon(&env, &args)) { + return env; + } + break; + } + } + + return nullptr; +}; + +extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *aJavaVM, void *aReserved) { + sJavaVM = aJavaVM; + return JNI_VERSION_1_6; +} + +extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *aJavaVM, void *aReserved) { + sJavaVM = nullptr; +} \ No newline at end of file diff --git a/src/jnienv.h b/src/jnienv.h new file mode 100644 index 0000000..6b121af --- /dev/null +++ b/src/jnienv.h @@ -0,0 +1,8 @@ +#ifndef JNIENV_H +#define JNIENV_H + +#include + +JNIEnv *getJniEnv(); + +#endif //JNIENV_H \ No newline at end of file