AS 配置使用NDK

1.工具准备

Android Studio 3.0.1
SDK Tools中安装SDK版本:16.1
安装:LLDB,c/c++调试器
安装:CMake 编译C/C++生成.so的构建工具

2.新建一个Android工程

在Greate Android Project时勾选Include C++ support
在Customize C++ Support时选择默认的Toolchain Default
会自动生成两个文件
cpp/native-lib.cpp cpp目录是放置c/c++代码
CMakeLists.txt:cmake插件的配置文件,用来指导c/c++如何编译成so文件
在eclipse中是用Android.mk来配置的
官方主推:CMake + CMakeLists.txt来构建库
1)定义库的名称,类型及要编译的源代码

add_library( # 编译生成的库的名称,最终会在前面加上lib libnative-lib.so
             native-lib

             # 生成的库的类型,有SHARED,STATIC,MODULE
             SHARED

             # 要编译的源代码文件多个文件用空格分开
             src/main/cpp/native-lib.cpp )

2)包含要编译源码的头文件

# 注意这里指定的是头文件的目录
include_directories(src/main/cpp/include/)

3)使用NDK里的api,链接动态预编译好的库

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 )
#链接到native-lib库中
target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

3.编译运行项目

在编译运行项目后在project视图下可以看到在build下多了一个android-profile目录
profile-xxxx.json编译的版本信息
运行后在TextView中出现Hello from C++
以上是模板生成的
生成的库文件在
./app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so
./app/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so
同样的程序代码为了兼容不同的CPU,需要为不同的ABI构建不同的库文件
armeabi设备只兼容armeabi;
armeabi-v7a设备兼容armeabi-v7a、armeabi;
arm64-v8a设备兼容arm64-v8a、armeabi-v7a、armeabi;
X86设备兼容X86、armeabi;
X86_64设备兼容X86_64、X86、armeabi;
mips64设备兼容mips64、mips;
mips只兼容mips;

4.自定义一个java文件生成所需要的接口函数

1)MathTool.java 在java文件中建立native方法和加载库
public class MathTool {
    static {
        System.loadLibrary("math-lib");
    }

    public static native int add(int i, int j);
    public static native int sub(int i, int j);

}
通过alt+enter键在native-lib.cpp中自动生对应的方法使编译能通过
2)native-lib.cpp
extern "C"
JNIEXPORT jint JNICALL
Java_com_bshui_ndktest_MathTool_add(JNIEnv *env, jclass type, jint i, jint j) {
   //实现相关的代码
    return i+j;

}extern "C"
JNIEXPORT jint JNICALL
Java_com_bshui_ndktest_MathTool_sub(JNIEnv *env, jclass type, jint i, jint j) {
    jint ret = 0;
    ret = i-j;
    return ret;

}

3)修改CMakeLIsts.txt
add_library( # Sets the name of the library.
             math-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )
target_link_libraries( # Specifies the target library.
                       math-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
4)Android 应用测试引用
 TextView tv = (TextView) findViewById(R.id.sample_text);
        int sum,sub;
        sum = MathTool.add(5,10);
        sub = MathTool.sub(5,10);
        tv.setText("sum(5,10)="+sum+" sub(5,10)="+sub);

5.其它项目引用

只需提供.so和.jar

1)在libs目录下新建arm64-v8a把对应的libmath-lib.so导入
2)把classes.jar放入libs目录下
导出class.jar, 原工程New Module-->Android Library
注意包名为com.bshui.ndktest,与生成native的java同包名
编译后生成class.jar包
3)build.gradle app的android中加入
sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }
4)在工程中调用api
tv = (TextView)findViewById(R.id.tv);
int sum = MathTool.add(10,20);
int sub = MathTool.sub(20,10);
tv.setText("sum(10,20)="+sum+" sub(20,10)="+sub);

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio中配置NDK的步骤如下: 1.下载NDK:打开Android Studio,依次点击File -> Project Structure -> SDK Location,在SDK Location窗口中找到Android NDK location,点击Download按钮下载NDK。 2.配置NDK路径:下载完成后,回到SDK Location窗口,将下载好的NDK路径复制到Android NDK location中。 3.配置build.gradle文件:在app目录下的build.gradle文件中添加以下代码: ```gradle android { ... defaultConfig { ... externalNativeBuild { cmake { cppFlags "" abiFilters "" } } } ... externalNativeBuild { cmake { path "CMakeLists.txt" } } } ``` 4.配置CMakeLists.txt文件:在项目根目录下创建CMakeLists.txt文件,并添加以下代码: ```cmake cmake_minimum_required(VERSION 3.4.1) 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 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. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) ``` 5.编写C++代码:在app/src/main/cpp目录下创建native-lib.cpp文件,并添加以下代码: ```c++ #include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_example_ndkdemo_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } ``` 6.运行程序:点击Android Studio中的Run按钮,即可在模拟器或真机上运行程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值