"Reference C ++ in Android Studio module (Java / kotlin)" https://qiita.com/sanoh/items/484e61729ab69f250fec
Memorandum of procedure to refer to the module created in step 3 from other projects
Create a project from "File" "New" "New Project". Select Native C ++ and then press Next. This time the name is "test03" Save location specifies the working directory Language specifies "Java" (Kotklin is fine) After completing the settings, click "Next" to go to the next screen. "Finish" completes the project creation.
Edit settings.gradle to reference the external module.
settings.gradle
include ':app', ':native-module'
project(':native-module').projectDir = new File(settingsDir, '../test02/test02module')
Please adjust the position of the test02 module you want to refer to in this project If you press "Sync Now" here You can see that it has been added to the project.
Then edit the application's build.gradle (Module: app).
build.gradle
android {
:
:
}
dependencies {
:
:
implementation project(':native-module')
}
Now that we have added the native-module, it can be referenced by the application.
Register in CmakeLists.txt.
python
CmakeList.txt
![2019-05-30 (7).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/122121/556e419a-8a91-2280-f71a-d034063919f0.png)
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
include_directories(../../../../../test02/test02module/src/main/cpp)
add_library( native-module
SHARED
IMPORTED )
set_target_properties( native-module
PROPERTIES IMPORTED_LOCATION
../../../../../../test02/test02module/src/lib/${ANDROID_ABI}/libnative-module.so)
# 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
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
native-module
# Links the target library to the log library
# included in the NDK.
${log-lib})
Finally, refer to the class in the module from native-lib.cpp.
native-lib.cpp
#include <jni.h>
#include <string>
#include "subTest.h"
extern "C" JNIEXPORT jstring JNICALL
Java_l_toox_test03_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
subTest ss;
ss.Hoge(32);
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
Recommended Posts