"Reportez-vous au module C ++ dans Android Studio (Java / kotlin)" https://qiita.com/sanoh/items/484e61729ab69f250fec
Mémorandum de procédure pour faire référence au module créé à partir d'autres projets
Créez un projet à partir de "Fichier" "Nouveau" "Nouveau projet".
Sélectionnez "Native C ++" puis appuyez sur "Suivant".
Cette fois, le nom est "test03"
L'emplacement d'enregistrement spécifie le répertoire de travail
Le langage spécifie "Java" (Kotklin est très bien)
Après avoir terminé les paramètres, cliquez sur «Suivant» pour passer à l'écran suivant.
"Terminer" termine la création du projet.
Modifiez settings.gradle pour référencer le module externe.
settings.gradle
include ':app', ':native-module'
project(':native-module').projectDir = new File(settingsDir, '../test02/test02module')
Veuillez ajuster la position du module test02 auquel vous souhaitez vous référer dans ce projet
Si vous appuyez sur "Synchroniser maintenant" ici
Vous pouvez voir qu'il a été ajouté au projet.
Puis modifiez le build.gradle de l'application (Module: app).
build.gradle
android {
:
:
}
dependencies {
:
:
implementation project(':native-module')
}
Maintenant que nous avons ajouté le module natif, il peut être référencé par l'application.
Inscrivez-vous dans CmakeLists.txt.
python
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-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})
Enfin, à partir de native-lib.cpp, reportez-vous à la classe dans le module.
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());
}