1. 準備
1.1.版本要求
(這個配置會解決Android Studio在開發C/C++的兼容性問題)
(1) Android Studio 2.2以上;
(2) Cmake3.6以上:是用來編譯C/C++成so的構建工具,創建項目爲C++的時候會自動導入這個插件;
(3) Gradle2.5以上:從2.5開始支持C/C++,在本次選擇2.8;
(4) ndk-r-10以上:本次選擇13;
1.2.這些版本的下載
(1)打開鏡像網站:www.androiddevtools.cn
(2)然後打開大連東軟信息學院鏡像服務器地址:http://mirrors.neusoft.edu.cn/
裏面有Android的同步文件,打開後有Android SDK需要的所有文件:
1.3安裝
1.Android Studio
1)在安裝Android Studio 完成的時候不要勾選start Android Studio,因爲無法連接到Google的服務器,下載進度爲0;
解決方式:打開Android Studio安裝目錄下的bin文件下的idea.prcperties文件,然後再最後面加入:disable.android.first.run=true 這個目的是讓Android Studio
第二次打開,防止第一次打開的時候一直無法連接到服務器。
2)配置jdk
配置自己安裝的Sdk和jdk
2.安裝LLDB,在SDK管理的SDK Tools中
2. 開發
2.1.創建項目
1.需要勾選Include C++ support
2.創建中出現C++則表示這個工程是一個C++工程
3.在工程的目錄結構中,會比普通的工程多cpp文件和CMakeLists文件
4.配置NDK環境:
在上一個步驟之後,這個時候項目會報錯:
Error:NDK not configured.
Download it with SDK manager.)
原因是沒有配置ndk。
下載ndk,可以通過Android Studio下載也可以在鏡像中下載。(建議另外下載)
然後配置:
5.配置CMake:
配置好NDK之後項目會提醒配置CMake:
點擊下載安裝,安裝完成之後會在sdk目錄下多一個cmake文件
2.2.配置CMake文件
在工程中的app目錄下會有一個CMakeLists.txt文件,它是cmake插件的配置文件,主要用來指導工程中的C/C++如何編譯成so文件。以前是用Android.mk來配置的,現在用cmke的語法來配置。語法類似marm語法。其中的配置爲:
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
MESSAGE("------>開始構建")
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 it 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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp src/main/cpp/demo.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
fenqilemoa-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
1.cmake的最小版本
cmake_minimum_required(VERSION 3.4.1)
在sdk目錄下的cmak目錄下有對應的版本:
2.設置編譯結果文件名和指定的編譯文件
add_library(
# Sets the name of the library.
native-lib
# file are automatically included.
src/main/cpp/native-lib.cpp )
如果是多cpp文件,用空格分開:
src/main/cpp/native-lib.cpp src/main/cpp/demo.cpp
3.連接動態預編譯的庫
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 )
可以在這個文件的最上面用:
MESSAGE("------>開始構建")
來進行對編譯的時候打印日誌。
2.3.CMake的編譯流程
在創建了C++工程後,會在build.gradle文件中生成:
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
運行項目時生成一個externalNativeBuild目錄。前提是項目的gradle必須是2.5以上
裏面會有各個版本的配置文件,每一個版本當中會有:android_gradle_build.json文件。這個文件是json語法格式:主要的步驟:
1.編譯的cmake文件
"buildFiles" : [ "D:/MyCode/MOA/app/CMakeLists.txt" ]
2.執行清楚命令
"cleanCommands"
3.Cpp文件類型
"cppFileExtensions" : [ "cpp" ]
4.庫的配置
"libraries" :
5.工具連接
"toolchains" :
2.4.Cpp文件
在創建一個C++的Android工程之後,會在項目中生成cpp目錄和默認的cpp文件。
在native-lib.cpp文件中:
#include <jni.h>
#include <string>
extern "C"
jstring
Java_com_test_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
2.4.1引入文件
在cpp文件中引入其他的依賴文件:如jni.h、string,或者自己創建的h文件。可以通過Ctro和鼠標鍵進入依賴的文件中,然後查看路徑等。Jni的引入路徑是由android_gradle_build.json中的flags配置決定的。
可能遇到的坑:
1. 如果不能進入或者無法自動提示代碼,通過重新引入build,再次編譯就可以了。
2. 如果項目中只有一個cpp文件,並且報錯了,就建一個空的cpp,可以解決報錯問題。
2.4.2自動創建cpp方法
在Java類中建立native方法:
public class JNIUtils {
static{
System.loadLibrary("native-lib");
}
public static native int caculator(int i,int j);
}
會報錯,因爲找不到cpp中的方法,通過alt+enter鍵創建cpp的對應方法:
#include <jni.h>
#include <string>
JNIEXPORT jint JNICALL
Java_com_test_JNIUtils_caculator(JNIEnv *env, jclass type, jint i, jint j) {
// TODO
}
extern "C"
jstring
Java_com_test_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++s";
return env->NewStringUTF(hello.c_str());
}
2.4.3.統一
將所有的native方法放在一個類中,對應生成cpp方法。
Java:
public class JNIUtils {
static{
System.loadLibrary("native-lib");
}
public static native int caculator(int i,int j);
public static native String stringFromJNI();
}
Cpp:
#include <jni.h>
#include <string>
#include "Util.cpp"
extern "C" {
JNIEXPORT jint JNICALL
Java_com_fenqile_moa_jni_JNIUtils_caculator(JNIEnv *env, jclass type, jint i, jint j) {
// TODO
int k=i+j;
return k;
}
JNIEXPORT jstring JNICALL
Java_com_fenqile_moa_jni_JNIUtils_stringFromJNI(JNIEnv *env, jclass type) {
std::string hello = "Hello from C++ssss";
return env->NewStringUTF(hello.c_str());
}
}
2.4.4.斷點
需要安裝LLDB,在代碼中打上斷點,然後用debug運行,就會運行到斷點位置: