1
0
mirror of https://github.com/alkoleft/yaxunit-addin.git synced 2024-11-30 08:06:40 +02:00

android support

This commit is contained in:
infactum 2020-09-10 23:26:24 +03:00
parent 5583bd5280
commit 123ffbeafa
5 changed files with 88 additions and 5 deletions

View File

@ -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 ()

View File

@ -0,0 +1,24 @@
/*
* Warning!!!
* DO NOT ALTER THIS FILE!
*/
#ifndef __IANDROIDCOMPONENTHELPER_H__
#define __IANDROIDCOMPONENTHELPER_H__
#include "types.h"
#if defined(__ANDROID__)
#include <jni.h>
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__

View File

@ -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<WCHAR_T *>(cls_names);
}
@ -36,8 +37,6 @@ long GetClassObject(const WCHAR_T *clsName, IComponentBase **pInterface) {
auto cls_name = std::u16string(reinterpret_cast<const char16_t *>(clsName));
if (cls_name == u"Sample") {
*pInterface = new SampleAddIn;
} else if (cls_name == u"SampleAlias") {
*pInterface = new SampleAddIn;
}
return (long) *pInterface;
}

36
src/jnienv.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <cstdlib>
#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;
}

8
src/jnienv.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef JNIENV_H
#define JNIENV_H
#include <jni.h>
JNIEnv *getJniEnv();
#endif //JNIENV_H