AndroidStudio JNI開發,通過Cmake編譯so文件

官方文檔:https://developer.android.com/ndk/guides/cmake

1、創建項目,在首界面勾選上Include C++ support,然後點擊next,直至創建完成

2、項目會默認生成jni事例代碼,觀察項目結構,在app/src/main下有個cpp文件夾,我們要寫或者要添加的c/c++文件就寫在該文件夾下。

3、打開MainActivity.java,在裏面可以看到這段代碼

 // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

其中native-lib就是我們要生成的so文件的名稱,在後續配置文件中還會用到,stringFromJNI就是調用c/c++的方法,我們打開app/src/main/cpp/native-lib.cpp文件,就會在其中發現有個Java_com_lianluo_jnitest_MainActivity_stringFromJNI方法,就是與之對應的。現在我們自己寫個native方法,public native String helloWorld();此時我們發現helloWorld方法名是紅色的,這是把鼠標指針移上去,然後點擊alt+enter鍵,選擇create function Java_com_lianluo_jnitest_MainActivity_helloWorld,這時會在native-lib.cpp文件中自動生成對應的c/c++方法。

4、打開app下的CMakeLists.txt文件,這個是進行配置cmake的,詳細的配置請看官網,這裏簡單的寫一下要注意的地方

# 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.
            //這裏要寫上我們要生成的so文件名稱,要與MainActivity.java裏的名稱對應
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             //這裏要寫上我們放在項目裏的c/c++文件
             src/main/cpp/native-lib.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 )

# 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.
                        //這裏要寫上我們要生成的so文件名稱,要與上面的名稱對應
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

4、打開app下build.gradle,將Cmake配置到gradle中
要在

android {
    compileSdkVersion 27
    defaultConfig {

        ...

        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }

    ...

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

5、編譯so文件,當寫好代碼後,我們clean項目,然後在工具欄build裏面選擇Make Project,這是在app的build/intermediates/cmake/debug(release)/obj文件夾下生成對應的so文件

注意:使用cmake編譯so文件時,絕對不能有中文路徑,否則會報這個錯誤

Build command failed.
Error while executing process /Users/krubo/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/krubo/Documents/工作/項目代碼/AndroidStudioProjects/JniTest/app/.externalNativeBuild/cmake/debug/x86_64 --target native-lib}
ninja: warning: premature end of file; recovering
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
ninja: build stopped: Error writing to deps log: No such file or directory.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章