將原有Android項(xiàng)目轉(zhuǎn)成ndk項(xiàng)目
本文所描述的是3.0版本及以上Android Studio
首先3.0之后新增了cmake插件,所以我們需要的是新增CMakeLists.txt

然后我們需要的是關(guān)聯(lián),在相應(yīng)的模塊包的build.gradle下面添加以下代碼:
externalNativeBuild {
????????cmake {
????????????path "src/main/cpp/CMakeLists.txt"http://這里填自己新建的路徑
????????????version "3.10.2"
?????? }
}

但是現(xiàn)在會(huì)出現(xiàn)一個(gè)問(wèn)題,就是我們沒(méi)有指定c++的版本還有我們本地并沒(méi)有相關(guān)ndk的頭文件和jni的頭文件,怎么辦?
首先我們可以指定c++的版本,還是在相應(yīng)模塊下的build.gradle中添加:
externalNativeBuild {
????????????cmake {
????????????????cppFlags ""
????????????}
????????}
不過(guò)這次的位置不是和上次一樣的,上次是在Android的父級(jí),這個(gè)是defaultConfig父級(jí),cppFlags后面的引號(hào)不寫(xiě)的話就是自適應(yīng)的版本,后面像指定版本需要添加:-std=c++14,
我的完整build.gradle

這樣build.project的配置就完成了
現(xiàn)在的問(wèn)題就是我們需要添加相應(yīng)的ndk頭文件和實(shí)現(xiàn)文件
需要在cmake文件里面添加以下代碼:
# 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(
??????????????#你可以自定義的動(dòng)態(tài)庫(kù)名字
?????????????native-lib
?????????????# Sets the library as a shared library.
?????????????SHARED
?????????????#你寫(xiě)好的cpp文件
????????????main.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.
???????????????????????#你想添加的動(dòng)態(tài)庫(kù)名字
????????????????????????native-lib
???????????????????????# Links the target library to the log library
???????????????????????# included in the NDK.
???????????????????????${log-lib} )
Make一下即可