jni步骤开发:
NDK和CMake 的下载和安装
local.properties文件, 在里面添加:
<span style="color:#000000"><span style="color:#cccccc"><span style="color:#404040"><code class="language-cpp"><span style="color:#999999">//后面改成自己下载后解压的路径名</span>
ndk.dir<span style="color:#67cdcc">=</span>C\<span style="color:#67cdcc">:</span>\\Users\\Lulu\\AppData\\Local\\Android\\android<span style="color:#67cdcc">-</span>ndk<span style="color:#67cdcc">-</span>r13
</code></span></span></span>
NDK两种开发模式
- ndk-build 形式; Android Studio 2.2之前的模式
- CMake 形式: CLion C/C++编辑器; AS2.2之后整合了CLion代码, AS就支持了CMake形式的NDK开发
1.直接建立项目,include C++
2.项目配置NDK
ndk.dir=/Users/mac/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/mac/Library/Android/sdk
3.cMakeList:定义cmakeListd的路径
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
4.点击Link C++ Project with Gradle
右击jni目录,选择Link C++ Project with Gradle标签
5.修改生成so库都类型
externalNativeBuild {
cmake {
cppFlags ""
abiFilters "armeabi-v7a"
}
}
添加java方法,studio自动提示,会写好方法名。
都是自动生成文件
注意:类名和方法名不要乱改,否则会找不到。
CMakeLists.txt文件解析:
<span style="color:#000000"><span style="color:#cccccc"><code class="language-bash"># 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)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI})
# 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})</code>
</span></span>
1.cmake_minimum_required(VERSION 3.4.1)
用来声明编译本地库文件时需要的Cmake最小版本,在AndroidStudio构建版本时会自动生成;
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY {ANDROID_ABI})
将libxx.so生成到jniLibs文件夹下
用来声明编译本地库文件时需要的Cmake最小版本,在AndroidStudio构建版本时会自动生成;
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY {ANDROID_ABI})
将libxx.so生成到jniLibs文件夹下
2.add_library()
native-lib : 编译本地本地lib生成的库文件的名称
SHARED : (生成的库种类)SHARED表示编译生成的是动态链接库
src/main/cpp/native-lib.cpp : 表示编译文件的相对路径,这里可以是一个文件的路径也可以是多个文件的路径
SHARED : (生成的库种类)SHARED表示编译生成的是动态链接库
src/main/cpp/native-lib.cpp : 表示编译文件的相对路径,这里可以是一个文件的路径也可以是多个文件的路径
3.find_library()
这个的作用是声明编译本地库的时候,需要用的到一些依赖库.
log-lib 是这个库的别名,方便我们以后引用
log 是我们调试的时候打印log的一个库
log-lib 是这个库的别名,方便我们以后引用
log 是我们调试的时候打印log的一个库
4.target_link_libraries()
<span style="color:#000000"><span style="color:#cccccc"><span style="color:#404040"><code class="language-bash">target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})</code></span></span></span>
目的是用来关联我们本地的库跟第三方的库.这里就是把native-lib库和log库关联起来.
cmake主要是:
修改native-lib.cpp
这时候我们修改下native-lib.cpp
,native-lib.cpp
内容如下:
第二种方式:mk 形式:
步骤如下:
-
用命令编译
-
先进入jni目录下
-
找到studo里面的loacal.perpert
/Users/ex_wljr_peng/Library/Android/sdk/ndk-bundle/ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk
参考博客:
https://blog.csdn.net/meiliangdeng1990/article/details/50784553
ndk-build使用的编译器是我们下载的ndk包里的
Android.mk和Application.mk
通过ndk-build命令会生成2个文件夹,一个是debug,另外一个是release