今天介绍一下Android Studio 2.2 下 NDK开发 ,那叫一个顺溜----纵享丝滑!
虽然现在AS 2.2 之后,jni开发配置相当方便,但是还是建议大家从我的第一篇文章看起,从基础知识入手,并且要了解之前是如何配置NDK工程的,这是一个循序渐进的过程。

今天主要介绍一下如何分别在新工程和老工程中创建最新NDK项目。
#新工程创建Ndk Project
##创建

创建时候勾选 include C++ support。

在AS 2.2 之后,当我们创建工程的时候会在下面多出一个勾选框,是否支持C++,这里我们勾选,然后一路next。

然后最后一步我们会看到如下界面:

这里不一定所有的同学都能跑起来,是因为我在之前的博客
http://blog.csdn.net/github_33304260/article/details/62891083
里面配置过NDK,这里就不相信讲解了,大家可以参考之前的文章,如果有问题可以留言。

好啦,就这么简单创建成功啦,已经可以运行啦,我们跑起来看一下。

就是这么纵享丝滑,不过很多童鞋对于新的结构可能不熟悉 那么接下来就行详细讲解一下新的结构和文件。

##项目一览
###1.目录结构
勾选了include C++ support 就会多出以下几个目录及文件。

  • .externalNativeBuild —> cmake 编译好的文件, 显示支持的各种硬件等信息 存放 .so 文件等Cmake相关文件
  • cpp —> 众所周知 JNI Folder 。C 语言程序的逻辑部分, native-lib.cpp 文件名可自行修改
  • CMakeLists.txt —> CMake 脚本配置的文件, 具体可查阅 CMake官网的资料

###2.build.gradle中的Cmake

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

Tips: 在Gradle里面多了以上两部分内容。当然这里默认的配置信息比较少,配置配置指令具体可查阅
CMake 官网。

###3.CMakeLists.txt文件中的具体配置

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html# 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( # Sets the name of the library.native-lib  # 这个是jni编译生产的so库的名字# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).src/main/cpp/native-lib.cpp ) # 要编译的c/c++文件列表 文件路径想对于cmake文件路径# 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 # 依赖的系统so库# 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.native-lib# Links the target library to the log library# included in the NDK.${log-lib} )

###4·MainAvtivity中新增的方法

1、在java代码中增加引用so库的代码,使代码生效

 // Used to load the 'native-lib' library on application startup.static {//此处的form库的名称需要和CMakeLists.txt中配置的相同System.loadLibrary("native-lib");}

2、定义方法

 /*** A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.*/public native String stringFromJNI();

###5· native-lib.cpp
里面可以直接的创建cpp源代码,和ndkBuild一样,用C/C++所写的源代码中的方法名称必须是全路径的方法名,然后以Java开头,分割使用下划线.

#include <jni.h>
#include <string>extern "C"
JNIEXPORT jstring JNICALL
Java_pressure_libin_com_pressure_MainActivity_stringFromJNI(JNIEnv *env,jobject /* this */) {std::string hello = "Hello from C++";return env->NewStringUTF(hello.c_str());
}

extern "C" : 暴露给外部使用。
确保所有Java需要调用的C方法都放在extern "C"中,这样CMake才会帮我们正确编译。

好啦 到这里 新鲜东西就已经都讲完了。
接下来看看如何在旧工程中添加最新NDK Project。

#老工程导入Ndk Project

1 选择app—> 右键 New —> Folder —> JNI Folder 。

选中 Change Folder Location
将JNI更换成cpp (为了统一 ,系统自动生成的是cpp文件 CMakeLists.txt里面配置也是cpp路径 )

cpp文件夹 这个文件夹颜色和java一样 说明你创建对了。


2 选择 jni —> New —> C/C++Source File


3 完善native-lib.cpp文件

#include <jni.h>
#include <string>extern "C"}

4 在APP目录下添加 CMakeLists.txt 文件

代码和上面创建后生成的一样,具体功能自行添加。
切记: jni编译生产的so库的名字和路径要正确

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html# 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( # Sets the name of the library.native-lib  # 这个是jni编译生产的so库的名字# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).src/main/cpp/native-lib.cpp ) # 要编译的c/c++文件列表 文件路径想对于cmake文件路径# 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 # 依赖的系统so库# 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.native-lib# Links the target library to the log library# included in the NDK.${log-lib} )

5 make Project


6 关联CMakeLists.txt
app — > Link C++ Project with Gradle

再次查看Gradle 发现 多了如下代码:

externalNativeBuild {cmake {path 'CMakeLists.txt'}}

或者手动在Gradle添加上面代码。


7 引用so库

在MainActivity中引用:

public class MainActivity extends AppCompatActivity {//引用so库static {System.loadLibrary("native-lib");}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}

8 编写第一个jni方法

public native String stringFromJNI();
extern "C"
JNIEXPORT jstring JNICALL
Java_pressure_libin_com_pressure_MainActivity_stringFromJNI(JNIEnv *env,jobject /* this */) {std::string hello = "Hello from C++";return env->NewStringUTF(hello.c_str());
}

#NDK工程下Debug
debug是一个非常重要的功能,在2.2之后,我们直接就可以对native代码进行debug。

在edit中可以对debug模式进行设置:

如图:

扫码关注公众号“伟大程序猿的诞生“,更多干货新鲜文章等着你~

公众号回复“资料获取”,获取更多干货哦~

有问题添加本人微信号“fenghuokeji996” 或扫描博客导航栏本人二维码

Android的JNI【实战教程】5⃣️---Android Studio 2.2 以上 NDK开发相关推荐

  1. 小白都能看懂的实战教程 手把手教你Python Web全栈开发(DAY 3)

    小白都能看懂的实战教程 手把手教你Python Web全栈开发 Flask(Python Web)实战系列之在线论坛系统 第三讲 这是小白都能看懂的实战教程 手把手教你Python Web全栈开发 的 ...

  2. 小白都能看懂的实战教程 手把手教你Python Web全栈开发(DAY 1)

    小白都能看懂的实战教程 手把手教你Python Web全栈开发 Flask(Python Web)实战系列之在线论坛系统 第一讲 博主博客文章内容导航(实时更新) 更多优质文章推荐: 收藏!最详细的P ...

  3. 《Android 移动应用基础教程(Android Studio)(第2版)》【课本客观题】+【学习通2023春】【参考答案】

    文章目录 超星学习通智能终端软件开发(基于Android Studio环境)章节作业(39) 一 二 三 四 五 六 课本一 课本二 课本三 课本四 课本五 课本六(无) 课本七 课本八 课本九 课本 ...

  4. android 回收站设计,【教程】Android系统建个回收站帮你找回误删文件

    亚风下载最新Android手机教程:Android系统可以帮你找回误删的资源文件.要怎么弄才可以找回被你删除掉的文件呢?下面一起跟亚风下载小编看看: 手机里失手误删的经历谁都有过,又或者某时刻的冲动- ...

  5. Android移动开发之【Android企业级项目实战教程】DAY1-图表库HelloCharts

    使用HelloCharts开源框架搭建一系列炫酷图表,柱形图,折线图,饼状图和动画特效,抽丝剥茧带你认识图表之美 实际开发中经常会使用第三方的图标库显示数据,其中HelloCharts就是一款非常优秀 ...

  6. Android属性动画实战教程开篇

    本系列博客会分俩篇 本篇博客主要是会介绍属性动画代码使用和xml中使用 关于View动画和属性动画的区别不做过多的介绍,当然涉及到的地方会简单的提一下. 好了废话不多说,直接上内容 首先介绍代码中使用 ...

  7. android studio 手把手叫你NDK开发环境搭建及基础使用

    一.准备工作 下载NDK: http://dl.google.com/android/repository/android-ndk-r12b-windows-x86_64.zip 找不到请访问: ht ...

  8. android移动应用基础教程源代码,Android移动应用基础教程 【程序活动单元Activity】...

    本章目录 一.Activity的生命周期 1.生命周期状态 2 .生命周期方法 3.横竖屏切换时的生命周期 二.Activity的创建配置和关闭 1.Activity的创建 2.配置Activity ...

  9. Android移动应用基础教程【Android事件处理】

    本章目录 一.事件处理概述 二.基于回调机制的事件处理 三.基于监听接口机制的事件处理 四.手势 1.手势简介 2.手势检测 3.使用GestureLibrary类添加手势 4.使用Gestures ...

  10. android nanohttp,002.实战nanoHTTPD嵌入android app(1)

    其实这一篇跟其他人写的也差不多,所以如果你对nanoHTTPD了解的话,可以先第二篇开始看. 想没想过在自己的android app里内嵌一个http server,这样咱们就可以从PC和手机上的浏览 ...

最新文章

  1. 一文告诉你,为什么要研究JVM原理
  2. Vector 把一个vector追加到另一个vector
  3. QWaiteCondition思考3
  4. 如何系统地自学python~知乎_经验分享 | 如何系统地自学 Python?
  5. 51单片机怎么显示当前时间_(进阶篇)51单片机之按键控制蜂鸣器、数码管、按键值移位显示...
  6. rabbitmq监控queue中message数量
  7. #3120. 「CTS2019 | CTSC2019」珍珠
  8. php设置加载动画,如何用CSS3制作页面圆圈加载动画(附代码)
  9. C# WinForm开发系列 - Form/Window
  10. 极客大学架构师训练营 系统架构 分布式数据库 数据分片 业务分库 CAP ACID BASE 第11课 听课总结
  11. Low-Code is Low—— 低代码的使用
  12. 搞个气氛 用MATLAB画一棵精致的圣诞树
  13. 量子计算(1)量子力学基本理论(上)
  14. unity 导出 ios 项目运行在模拟器
  15. 理解计算:从√2到AlphaGo——第2季 神经计算的历史背景
  16. Windows下读取Linux分区的免费工具——WinAllFS http://www.lirui.name/post/63.html
  17. C++实现OTSU算法(大津法)
  18. HW1 游戏分类与热点探索
  19. vscode 中如何删除空行
  20. Golang helloWord

热门文章

  1. linux zmq编译pgm,czmq交叉编译
  2. 电脑基本快捷键,你知道多少?
  3. 支持中国西安申办ICCV2025,见证计算机视觉蓬勃发展的20年| Vote for ICCV2025 Xi'an China...
  4. #ifndef #define #endif用法理解
  5. Notes配置初始化和重新设置(不卸载)
  6. 【SQL Server 优化性能的几个方面】(转)
  7. 0917 词法分析程序(java版)
  8. Oracle instr用法
  9. Android Editable
  10. JavaScript中String的replace函数