CmakeLists.txtまたはAndroid.mkでAndroidエンジニアリングにc++コードを追加
4934 ワード
一、Cmake Build/main/.Java->main/.cpp->app/CMakeLists.txt->app/build.gradle
1.java側インタフェースファイルの作成
これらのnativeメソッドを後でjavaメソッドとして使用することを目的とし,java側はこれらのメソッドのみに関心を持つ.
2....hファイルの関数命名規則に従ってcppファイルを作成する
3.javaインタフェースファイルの対応する.hファイルをコンパイルし、.hファイルの関数名に基づいて対応するcppファイルを実現し、cppファイルはmainフォルダの下のcppフォルダの下に置くのが一般的である.
Javaメソッドに基づいてJNIの.hヘッダファイルをコンパイルする.コマンドラインはjavaコードディレクトリ、すなわちapp/src/main/javaに入り、次のコマンドを入力します.
javah -jni xxxx.com.jnitech.JniInterface
JniInterfaceはclassのクラス名で、javaファイルに対応するディレクトリの下で対応する.hファイルを生成します.
4.appフォルダの下にCmakeLists.txtファイルを新規作成ソースファイル依存関係を追加
4.1ソース依存性の追加:
4.2オリジナルAPIおよびライブラリ依存を追加します.たとえば、以下では、オリジナルlogライブラリを依存としてlog-lib変数に保存します.
ネイティブライブラリ依存度:
ネイティブAPI依存:
4.3コンパイル済み.soライブラリ依存性の追加
4.5自己の.soライブラリが上記の依存APIまたはライブラリを使用している場合、自己ライブラリに関連付ける必要がある
appのbuild.gradleにCmakeLists.txtを関連付ける
デフォルトでは、NDKでサポートされているABIはすべてAPKにパッケージ化されています.次の構成では、指定したABIタイプの.soライブラリをAPKにパッケージ化できます.
二、Ndk Build main/.Java->main/jni/.cpp->PROJECT_PATH/jni/Android.mk->PROJECT_PATH/Application/mk
1.javaインタフェースファイルを作成する.
2.mainディレクトリの下で右クリック->New->Folder->JNI Folderをクリックし、上記の2点目のようにcppファイルを作成します.
3.プロジェクトディレクトリの下にjniフォルダを作成してから、Application.mkを作成します.
4.appディレクトリまたは上記jniディレクトリにAndroid.mkディレクトリを作成します.次のようになります.
5.app/build.gradleに次の項目を追加します.
1.java側インタフェースファイルの作成
これらのnativeメソッドを後でjavaメソッドとして使用することを目的とし,java側はこれらのメソッドのみに関心を持つ.
publicclassTestJNI{
// .so :
//1. ,sayHello so cpp ,
static{
System.loadLibrary("TestJNI");
}
Public static native String sayHello();
}
2....hファイルの関数命名規則に従ってcppファイルを作成する
3.javaインタフェースファイルの対応する.hファイルをコンパイルし、.hファイルの関数名に基づいて対応するcppファイルを実現し、cppファイルはmainフォルダの下のcppフォルダの下に置くのが一般的である.
Javaメソッドに基づいてJNIの.hヘッダファイルをコンパイルする.コマンドラインはjavaコードディレクトリ、すなわちapp/src/main/javaに入り、次のコマンドを入力します.
javah -jni xxxx.com.jnitech.JniInterface
JniInterfaceはclassのクラス名で、javaファイルに対応するディレクトリの下で対応する.hファイルを生成します.
4.appフォルダの下にCmakeLists.txtファイルを新規作成ソースファイル依存関係を追加
4.1ソース依存性の追加:
add_library( # Specifies the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
4.2オリジナルAPIおよびライブラリ依存を追加します.たとえば、以下では、オリジナルlogライブラリを依存としてlog-lib変数に保存します.
ネイティブライブラリ依存度:
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the log library to the target library.
${log-lib} )
ネイティブAPI依存:
add_library( app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
# You need to link static libraries against your shared native library.
target_link_libraries( native-lib app-glue )
4.3コンパイル済み.soライブラリ依存性の追加
add_library( imported-lib
SHARED
IMPORTED )
set_target_properties( # Specifies the target library.
imported-lib
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
imported-lib/src/${ANDROID_ABI}/libimported-lib.so )
include_directories( imported-lib/include/ ) #
4.5自己の.soライブラリが上記の依存APIまたはライブラリを使用している場合、自己ライブラリに関連付ける必要がある
target_link_libraries( # cpp .so
native-lib
#
imported-lib #
app-glue # API
${log-lib} ) #
appのbuild.gradleにCmakeLists.txtを関連付ける
// Encapsulates your external native build configurations.
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}
デフォルトでは、NDKでサポートされているABIはすべてAPKにパッケージ化されています.次の構成では、指定したABIタイプの.soライブラリをAPKにパッケージ化できます.
defaultConfig {
...
externalNativeBuild {
cmake {...}
// or ndkBuild {...}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a',
'arm64-v8a'
}
}
二、Ndk Build main/.Java->main/jni/.cpp->PROJECT_PATH/jni/Android.mk->PROJECT_PATH/Application/mk
1.javaインタフェースファイルを作成する.
2.mainディレクトリの下で右クリック->New->Folder->JNI Folderをクリックし、上記の2点目のようにcppファイルを作成します.
Java_com_huawei_hicode_ndk_JniUtils_getStringFromC(
JNIEnv *env,
jclass type) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
3.プロジェクトディレクトリの下にjniフォルダを作成してから、Application.mkを作成します.
APP_STL := c++_static # , c++ , string\vector\iostream
4.appディレクトリまたは上記jniディレクトリにAndroid.mkディレクトリを作成します.次のようになります.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := TestJNI
LOCAL_SRC_FILES := $(LOCAL_PATH)/../app/src/main/jni/com_huawei_hicode_ndk_JniUtils.cpp
include $(BUILD_SHARED_LIBRARY)
5.app/build.gradleに次の項目を追加します.
externalNativeBuild {
ndkBuild {
path '../jni/Android.mk'
}
}