Create a project with "File", "New", and "New Project". Because it uses C ++. Select "Native C ++" and press "Next". This time the Name is "Test02" Save Location specifies the directory to develop Language is set in Java this time (Kotlin is okay) and then "Next" After that, just "Finish" If you see this screen, you can create a project.
Add the module to the project created by Setp1. Select "File" "New" "New Modile". This time, select "Android Library" and click "Next" Application / Library name is "test02module" this time. If you press "Finish" It looks like this, and you can see that "test02module" has been added to the project.
First, create a directory to put the C ++ source. [test02] +---[test02modile] +---[src] +---[main]
CMakeList.txt
# 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-module
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
subTest.cpp)
# 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)
set( LIB_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../.. )
set( OUTPUT_DIR ${LIB_ROOT}/lib/${ANDROID_ABI} )
set_target_properties( native-module
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR} )
# 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-module
# Links the target library to the log library
# included in the NDK.
${log-lib})
subTest.cpp
#include "subTest.h"
subTest::subTest() {
}
void subTest::Hoge(int in_no) {
int a = in_no;
}
subTest.h
#pragma once
class subTest{
public:
subTest();
public:
void Hoge(int in_no);
};
After adding these three files, edit build.gradle (Module: test02module).
build.gradle
android {
:
:
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
To add. Once you sync with this You can see that the file has been added to the Module.
Edit build.gradle (Module: app) on the app side.
android {
:
:
sourceSets {
main {
jniLibs.srcDir '../test02module/src/lib'
jni.srcDirs = []
}
}
}
Now you can reference the C ++ library so file. Edit "CMakeKists.txt" on the application side.
CMakeKists.txt
# 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(../../../../test02module/src/main/cpp)
add_library( native-module
SHARED
IMPORTED )
set_target_properties( native-module
PROPERTIES IMPORTED_LOCATION
../../../../../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})
Now that you can refer to it. C source of app native-lib.cpp
native-lib.cpp
#include <jni.h>
#include <string>
#include "subTest.h"
extern "C" JNIEXPORT jstring JNICALL
Java_l_toox_test02_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
subTest ss;
ss.Hoge(32);
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
You can refer to it at.
If you compile it as it is, an error may occur. Because it may not have the so file created If you select "Build" after selecting "test02 modile", "Make Moduke'test02module'" will appear, so if you run it and then run the app, there is no problem.
Recommended Posts