本人JNI小白一枚,之前用ndk编译成so之后进行使用,现在发现cmake这个好东西,在写完c代码后,可以直接运行,省去了编译ndk编译的时间。还发现在android studio中c/c++的代码也可以自动补全了,写起来非常的方便。
我现在手中的代码大多都是不带cmake的项目,想在这些项目中添加jni的支持,发现了网上好多人写的帖子已经过时了,还有的帖子上说,后添加jni的项目不支持自动补全
现在正好自己梳理一遍,发现后添加jni支持的项目也是可以进行自动补全的
在android studio3.3以上的版本上,创建对c++新项目的时候位置进行了改变
没有了对c++支持的选项 在这里可以创建带native支持的android项目 自动生成cmake文件与简单的cpp文件 自动在gradle中添加了如上代码 并且可以补全c++的代码如果已经创建了不带jni支持的项目,想把项目转成支持jni的项目需要如下操作
创建或者复制cmake文件到src/main/cpp目录下 显示模式调成android,右键点击link c++ project with gradle选项 选择之前存放的cmake文件,之后点击ok在进行以上操作后,对项目进行make之后先运行一遍项目,发现在c++文件中也可以进行补全操作了
```
# 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).
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})
```
网友评论