Android Studio 的NDK开发环境搭建

来源:互联网 发布:excel重复数据筛选函数 编辑:程序博客网 时间:2024/04/25 18:54

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文件

Paste_Image.png

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"jstringJava_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 JNICALLJava_com_test_JNIUtils_caculator(JNIEnv *env, jclass type, jint i, jint j) {    // TODO}extern "C"jstringJava_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 JNICALLJava_com_fenqile_moa_jni_JNIUtils_caculator(JNIEnv *env, jclass type, jint i, jint j) {    // TODO    int k=i+j;    return k;}JNIEXPORT jstring JNICALLJava_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运行,就会运行到断点位置:

0 0