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

来源:互联网 发布:商业标书知乎 编辑:程序博客网 时间:2024/06/02 03:59

今天介绍一下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 JNICALLJava_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");    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

8 编写第一个jni方法

public native String stringFromJNI();
extern "C"JNIEXPORT jstring JNICALLJava_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模式进行设置:

这里写图片描述

这里写图片描述

如图:

这里写图片描述

6 0
原创粉丝点击