Android NDK Android studio 3.2 項目中使用cmake調用、調試 C/C++

使用

環境:win10/Android studio 3.2

1.環境配置

在SDK Tools里選擇 CMAKE/LLDB/NDK點擊OK 安裝這些插件.


image.png

2.創建CMakeLists.txt文件

在Project 目錄下,右鍵app,點擊新建File文件,命名為CMakeLists.txt


image.png

3.配置文件

在CMakeLists.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.
             test_lib #.so庫名 可自定義

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/jni/test_lib.c ) #源文件所在目錄
# 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.
                       test_lib #.so庫名 可自定義
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

4.創建一個新的java類

image.png

類中代碼

package com.example.myndkdemo;


public class Test_lib {
    static {
        System.loadLibrary("test_lib");//加載.so庫
    }

    public static native String getStr(String str);//調用C/C++接口函數

}

5.在main下面創建jni目錄,創建test_lib.c文件,名字必須與CMakeLists.txt文件的源文件所在目錄一致

image.png

image.png

6.右鍵app,點擊Link C++ Project with Gradle

image.png

顯示如下,選擇CMakeLists.txt文件所在路徑,點擊ok,等待構建完成.


image.png

構建完成后,build.gradle文件會自動生成一些配置,如下圖:


image.png

7.回到Test_lib.java文件,選中getStr()函數,按下Alt+Enter,點擊Create function...,如下圖。

此時會在test_lib.c文件里自動生成C/C++函數。接著就可以在.c文件里編寫C/C++接口函數了。


image.png

image.png

此處有一個關于returnValue的報錯,不用理會,把它換掉就行。


image.png

然后Make Project成功后,會在如下目錄生成.so文件.此時.so庫生成成功,可隨時調用了!
image.png

8.在其他java類中調用C/C++函數

在要調用的java類中導入Test_lib包:


image.png

大功告成!

調試

如果 C 或 C++ 那邊代碼出問題了,調試起來要命的,之前只能 log,現在 Android Studio 提供了打斷點來 debug C 或 C++ 代碼,需要使用 LLDB 來檢測到 C 或 C++ 代碼中的斷點,這里要使用 externalNativeBuild 方式自動編譯 C/C++ 代碼,才能進入斷點進行調試。


image.png

Native 模式
選擇 run 下拉 item,選 Edit Configurations,彈出配置窗體,1、添加;2、Name: app-native;3、Module 還是選擇 app;4、Debugger 選擇 Native,只想使用 LLDB 來調試代碼,忽略 Java 代碼中的斷點。


image.png

感謝:
在Android studio 3.2 版項目中使用cmake調用C/C++
Android NDK 入門與實踐

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。