网上很多其他人的说法,都不是很正确,因为现有的android studio 3.x 都不在继续支持ndk-build的方式进行调试,
该种方式请参考
android studio ndk 调试
而且ndk-build的方式非常麻烦,我们这里讲的方法是按照android studio官方的说法,利用CMakeLists.txt的方式,来编译c或者c++的JNI的接口,形成可以调用的.so库,然后进行调用调试。
好吧,我们开始吧,建立一个空Activity项目
然后让其自动配置及编译一下。当然前提是你设置好了JDK 及 SDK等。
需要设置安装NDK 我的NDK安装方法是 在网上下载的ndk的压缩包,然后将其配置到Project Structure... ==>SDK Location==>Android NDK location中
我的NDK的路径是D:\android-ndk-r14b
然后将项目的app路径下,放入一个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.
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 )
# 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
# Links the target library to the log library
# included in the NDK.
${log-lib} )
这里说明一下,其中我们项目要调用的动态库为native-lib,编译后会形成libnative-lib.so,然后在java中进行调用。
然后我们需要在app下的build.gradle中进行其他的设置,我们需要在defaultConfig节点中加入
externalNativeBuild {
cmake {
cppFlags ""
}
}
ndk {
moduleName "native-lib"
abiFilters 'armeabi', 'armeabi-v7a'
}
在android节点中加入
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
然后在app/src/main下建立cpp目录,然后在其中放入我们的native-lib.cpp文件
文件内容是
#include
#include
extern "C" JNIEXPORT jstring JNICALL Java_com_rk3399pro_myapplication_MainActivity_stringFromJNI(JNIEnv *env,jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
然后需要将AndroidManifest.xml中加入
android:debuggable="true"
然后将MainActivity中加入如下代码,
static { System.loadLibrary("native-lib"); } public native String stringFromJNI();
和
stringFromJNI();
然后点击Settings...==>Android SDK==>SDK Platforms下先检查SDK的版本是否对,是否安装了SDK Tools下是否安装了LLDB及CMake工具,如果没有安装,现在就安装一下
点击Run==>Edit Configurations...
我们发现缺少Debugger选项
其实是因为android studio 3.x以上版本不默认安装Android NDK Support造成的,
如果这时看cpp的代码,也是没有颜色,意味着没有识别到
我们安装一下Android NDK Support,在Settings...==>Plugins下,打勾进行安装及重启android studio
我们看到安装完成以后,cpp的代码,已经变成有颜色的了,可以进行识别了,但此时还调试不了,需要设置Debuger
点击Run==>Edit Configurations...
我们加入一个名称为native的调试
Debugger中的类型选择Auto
然后我们在java中调用的时候加入断点,在c++的接口函数中加入断点,此时已经可以进行断点调试了。