AndroidStudio 工程添加NDK支持

内容纲要

本文默认已配置好ndk开发环境,网上很多详细的配置文章,不在此赘述。直接进入正题:

开发环境

  • Ubuntu 14.04 (64位)

  • AndroidStudio 3.0.1

具体实现

方法一: 新建项目时勾选 Include C++ Support

1. File>new Project ,如图(见高亮部分): 
这里写图片描述
2. 一路Next下去到最后选择到C++ 的支持环境 
如图,详情请参考[] 
这里写图片描述
具体参数释义:官方文档Add C and C++ Code to Your Project

In the Customize C++ Support section of the wizard, you can customize your project with the following options:

    C++ Standard: use the drop-down list to select which standardization of C++ you want to use. Selecting Toolchain Default uses the default CMake setting.
    Exceptions Support: check this box if you want to enable support for C++ exception handling. If enabled, Android Studio adds the -fexceptions flag to cppFlags in your module-level build.gradle file, which Gradle passes to CMake.
    Runtime Type Information Support: check this box if you want support for RTTI. If enabled, Android Studio adds the -frtti flag to cppFlags in your module-level build.gradle file, which Gradle passes to CMake.123456

3 . Finish 完成;

方法二 为已有项目添加ndk支持

在已有项目的基础上通过新建module的方式添加ndk支持,如果不想创建module 忽略新建module的步骤即可,其他一致。 
1. 新建一个AndroidStudio 工程(未勾选C++支持,哈哈 废话了) 
File》new 一路下去就好了~,就不浪费篇幅介绍如何创建了 
2. 给上步创建的工程添加一个module 
A:File > new Module> Android Library ,如图

这里写图片描述

B:next,给创建的module 起个名字

这里写图片描述

C:Finsh,工程结构如下: 
这里写图片描述

3. 将module 添加至工程依赖 
A:File> Project Structure > 右边tab 切换到dependencies 
B: 点击 “+” 好,选择 3.module dependencies(如图) 
这里写图片描述

C:选择第二步创建的module ,完成; 之后工程会自动配置一些项目信息。 
至此,准备工作完成,下详细说明如何添加NDK支持。

4. 在module 中新建一个 Java文件 MyLib.java,并创建一个native方法 add();

/**
 * Created by bdliang on 17-12-12.
 */public class MyLib {
    static {        //加载so 文件
        System.loadLibrary("math");
    }    public native int add(int para1, int para2);

}123456789101112131415

5. 通过javah 命令生成相应的头文件, 
左侧导航窗口MyLib.java 上,右键 > External Tools > javah 
PS: External tools 中的javah 需要自己配置,详见javah配置步骤,步骤差不多,我把自己 Ubuntu的javah 参数 信息放下,仅供参考 
这里写图片描述

6. 创建 C++ 文件 
javah 命令会自动生成 jni的头文件 以及相应的目录文件,需要注意的是 AndroidStudio中显示的目录为cpp 但是在文件系统中实际是jni目录(新建工程时勾选C++支持的区别)。后续CMakeList.txt中需要特别注意路径。 
cpp目录下右键 新建C++ sourceFile,新建一个Math.cpp的C++ 文件。

7. 在MyLib的main 目录下创建CMakeList.txt文件(跟src 同级目录) 
内容如图 
这里写图片描述

cmake_minimum_required(VERSION 3.4.1)add_library( # Sets the name of the library. so库名字
             math             # Sets the library as a shared library.
             SHARED             # 具体的C++ 文件
            src/main/jni/Math.cpp

            )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 )target_link_libraries( # Specifies the target library.
                     math                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )123456789101112131415161718192021222324252627282930

8. 修改MyLib的build.gradle 配置文件,添加ndk支持; 
如图:图片高亮部分为修改的具体内容:

defaultConfig节点下增加:

  externalNativeBuild {
            cmake {
                cppFlags " -frtti -fexceptions -std=c++11  "

                abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
            }
        }1234567

Android 节点下:

 externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }12345

这里写图片描述

9 . 在Math.cpp中 具体实现生成的com_bdliang_mylib_MyLib.h头文件的中的定义的方法ava_com_bdliang_mylib_MyLib_add

#include "com_bdliang_mylib_MyLib.h"/*
 * Class:     com_bdliang_mylib_MyLib
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_bdliang_mylib_MyLib_add
        (JNIEnv *, jobject, jint para1, jint para2) {
    return para1 + para2;
}123456789101112

10 .

修改布局文件,添加一个TextView用于展示结果数据; 
在部分内容不在详细说明~文章最后会附上本文完整的代码。 
11 . Run app ,撒花结束/// 
这里写图片描述

发表回复