AndroidStudio 他のプロジェクトのモジュール内のC++を参照しよう(Java / kotlin)


「AndroidStudio モジュール内のC++を参照する(Java / kotlin)」
https://qiita.com/sanoh/items/484e61729ab69f250fec

で作成したモジュールを他のプロジェクトから参照する手順の覚書

■Step1:プロジェクト作成


「File」「New」「New Project」からプロジェクトを作成します。

「Native C++」を選択してから「Next」を押します。

今回のNameは「test03」で
Save locationは作業ディレクトリを指定
Languageは「Java」を指定します(Kotklinでも問題ないです)
設定が終われば「Next」で次の画面に

「Finish」でプロジェクト作成は終了です。

■Step2:他のプロジェクトのモジュールを参照

settings.gradleを編集して外部のモジュールを参照します。

settings.gradle
include ':app', ':native-module'
project(':native-module').projectDir = new File(settingsDir, '../test02/test02module')

参照したいtest02moduleの位置は、ここのプロジェクトで調整してください
ここで「Sync Now」を押せば

プロジェクトに追加されたことがわかると思います。

次にアプリケーションのbuild.gradle(Module: app)を編集します。

build.gradle
android {
  :
  :
}

dependencies {
  :
  :
    implementation project(':native-module')
}

native-moduleを追加したのでこれでアプリケーションから参照が可能になります。

■Step3:アプリケーションから参照

CmakeList.txtに登録します。

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})

最後に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());
}