2020最新asプロジェクトにjni------静態登録を追加

13807 ワード

ステップ1:静的コードブロックsoライブラリのインポート
static {
    System.loadLibrary("native-lib");
}

ステップ2:外部関数の書き込み
public native String getString();  //native  c\c++  

ステップ3:javahを使用して関数ヘッダを生成する
      src/main/java   
  javah -jni com.example.myapplication.MainActivity(  java     )

ステップ4:新しいC/C++ソースファイルを作成する
     C/C++             ,        :

                   cpp/   ,             :
  IDE       Project   ,           Project   。
   your-module > src,     main   ,       New > Directory。
   cpp       ,     OK。
     cpp/   ,       New > C/C++ Source File。
              ,   native-lib。
  Type      ,               ,   .cpp。
                 (   .cxx   .hxx),     Edit File Types       。     C/C++     ,  Source Extension   Header Extension                ,     OK。
           ,    Create an associated header    。
   OK。

ステップ5:CMakeの構成
1、IDEの左側からProjectペインを開き、ドロップダウンメニューからProjectビューを選択します.2、your-moduleのルートディレクトリを右クリックし、「New」>「File」の順に選択します.3、「CMakeLists.txt」をファイル名として入力し、OKをクリックします.これで、CMakeコマンドを追加して構築スクリプトを構成できます.CMakeが元のソースコードに基づいて元のライブラリを作成するように指示するには、構築スクリプトにcmake_を追加します.minimum_required()とadd_library()コマンド:

    cmake_minimum_required(VERSION 3.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 )
add_library()を使用してCMakeコンストラクションスクリプトにソースファイルまたはライブラリを追加すると、Android Studioはプロジェクトを同期した後、プロジェクトビューに関連するヘッダファイルを表示します.ただし、CMakeがコンパイル時にヘッダファイルを見つけるためには、CMake構築スクリプトにinclude_directories()コマンドを追加し、ヘッダファイルのパスを指定する必要があります.
 add_library(...)

    # Specifies a path to native header files.
    include_directories(src/main/cpp/include/)

Android NDKは、非常に実用的なオリジナルAPIとライブラリを提供しています.プロジェクトのCMakeLists.txtスクリプトファイルにNDKライブラリを含めることで、任意のAPIを使用できます.
Androidプラットフォームにはすでに構築済みのNDKライブラリが存在するため、構築したりAPKにパッケージ化したりする必要はありません.これらのNDKライブラリはすでにCMake検索パスに存在するため、ローカルにインストールされているNDKライブラリの場所を指定する必要はありません.CMakeに使用するライブラリの名前を指定し、独自のオリジナルライブラリに関連付けるだけでいいです.
CMakeコンストラクションスクリプトにfind_library()コマンドを追加して、NDKライブラリを見つけ、そのパスを変数として保存します.この変数を使用すると、構築スクリプトの他の部分でNDKライブラリを参照できます.次の例では、Android固有のログサポートライブラリを見つけ、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 )

オリジナルライブラリでlogライブラリの関数を呼び出すには、CMakeコンストラクションスクリプトのtarget_link_libraries()コマンドを使用してライブラリを関連付ける必要があります.
 find_library(...)

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

例:
 cmake_minimum_required(VERSION 3.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 )
    

    # Specifies a path to native header files.
    include_directories(src/main/cpp/include/)
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} )
                  

ステップ6:Gradleをオリジナルライブラリに関連付ける
自動設定:
  • 1.   IDE      **Project****Android**2.**app**   ),         **Link C++ Project with Gradle**43.       ,   CMake ndk-build。
    
       a.      **CMake****Project Path**           CMake      `CMakeLists.txt`     。
    
       b.      **ndk-build****Project Path**           ndk-build      [`Android.mk`](https://developer.android.com/ndk/guides/android_mk)[`Application.mk`](https://developer.android.com/ndk/guides/application_mk)       `Android.mk`          ,Android Studio        .
    
       4.  ok
    
    手動構成
            Gradle          ,     externalNativeBuild         build.gradle    ,    cmake   ndkBuild        :
      android {
          ...
          defaultConfig {...}
          buildTypes {...}
    
          // 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"
            }
          }
        }