android studio jni gradle,详解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅

1.首先什么是jni呢?

jni——(java native interface),他是java平台的特性,不是安卓系统提供的。他定义了一些jni函数,来让开发者可以通过调用这些函数来实现java代码调用c/c++代码。

2.如何使用jni呢?

我们先将写好的c/c++代码编译成对应平台的动态库(windows是.dll文件,linux是.so文件)。

下面我们来举个栗子:使用androidstudio来实现jni

3.要实现jni先下载ndk,那么ndk又是什么呢?(面试宝典来了,赶紧掏出小本本)

ndk是一系列工具的集合

ndk提供了一份稳定、功能有限的api头文件声明

ndk的发布,使“java+c”的开发方式终于转正,成为官方支持的开发方式

ndk将使android平台支持c开发的开端

好,那接下来我们来下载ndk,有俩种方式:

google官方下载ndk:

通过sdkmanager来下载ndk:

3b0c3620c7819466b9f950538a5f5d30.png

4.下来我们new一个新工程:这个工程只包含一个mainactivity

d699c1d97cf4fa8351983877e9e84f45.png

5.我们来检查一下ndk下载好了没有,怎么检查呢?如下:

检查sdk location里面的ndk路径:

129dd4b993a861a54996c6dcfe6fd25a.png

检查local.properties文件里面有没有ndk路径:

a20989699a9fa5189f1e922aff3a7c02.png

6.下来我们要编写jni接口啦,如下:

jni接口需要用native关键字修饰,我们会看到方法名报红,没关系,我们继续

87d7153c128374cee89fcfe7c6a66b2a.png

7.我们先build一下工程,检查myjniutils.java编译后有没有生成class文件,在这个位置下:

androidjnitest/app/build/intermediates/classes/debug/com/kissdream/androidjnitest/myjniutils.class

65d85d0d2c4aa06c8cef88628214e7c9.png

8.使用javah生成.h头文件,具体如下:

打开terminal,输入命令进入到debug目录下,命令如下:

cd/users/apple/desktop/androidjnitest/app/build/intermediates/classes/debug

然后使用javah+包名+文件路径来生成头文件,命令如下:

javah com.kissdream.androidjnitest.myjniutils

0877ba6fafcd927ceff40e370d65726d.png

检查头文件有没有生成:

我们发现这个路径下多了个.h文件androidjnitest/app/build/intermediates/classes/debug/com/kissdream

哈哈,没错这个就是我们要生成的头文件

1c9d4f72b9306d78bf09248aa86192a1.png

9.生成了.h文件还不行,只是声明了方法,我们还需要去实现它,那么如何去实现他呢,如下:

-我们在main下新建一个jni文件夹,如图:

01ce32cf7d94d17801406011f505df0e.png

bf84a37c5a2356dc8197f53f9b642906.png

.h文件内容如下:

/* do not edit this file - it is machine generated */

#include

/* header for class com_kissdream_androidjnitest_myjniutils */

#ifndef _included_com_kissdream_androidjnitest_myjniutils

#define _included_com_kissdream_androidjnitest_myjniutils

#ifdef __cplusplus

extern "c" {

#endif

/*

* class: com_kissdream_androidjnitest_myjniutils

* method: getname

* signature: ()ljava/lang/string;

*/

jniexport jstring jnicall java_com_kissdream_androidjnitest_myjniutils_getname

(jnienv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

把生成的.h文件拷贝到jni文件夹下

在jni文件夹下,新建一个.c(c语言)或者.cpp(c++)的文件,来实现.h文件里声明的方法:

把.h文件里面声明的方法拷贝到新建的c++文件里面,然后在文件里面引入.h文件:

引入.h文件#include "com_kissdream_androidjnitest_myjniutils.h"

#include "com_kissdream_androidjnitest_myjniutils.h"

jniexport jstring jnicall java_com_kissdream_androidjnitest_myjniutils_getname

(jnienv * env, jobject obj){

//如果是用c语言格式就用这种方式

// return (*env)->newstringutf(env,"kiss dream");

//如果是用c语言格式就用这种方式

return env->newstringutf((char *)"kiss dream");

}

b8f81f9a3479b930caa659019bedeee0.png

到这里我们的方法就实现完毕了

10.方法我们实现了,但是我们如何调用呢,不要着急,follow me:

首先引入动态库:

public class myjniutils {

static {

//名字注意,需要跟你的build.gradle ndk节点下面的名字一样

system.loadlibrary("nameprovider");

}

//jni接口需要用native关键字修饰

public native string getname();

}

nameprovider就是你要生成d的.so文件的文件名

下面我们来调用它

2c926693098acfbe486df4d530e3c988.png

11.最重要的一步来了,生成so文件:

这个小编也不会,于是就去百度了下,得到结果:

在根目录gradle.properties下面加上:

android.usedeprecatedndk=true意思就是允许使用低版本的ndk

在module下面的build.gradle下面加上ndk节点如下图:

ndk {

modulename "nameprovider"

}

nameprovider注意这个名字要跟你引入动态库的名字一样

需要这俩步就可以运行生成so文件了

然儿,并没有想象的那么顺利,报错了,我顿时心中飞过一万只草泥玛,上log:

error:execution failed for task ':app:compiledebugndk'.

> error: flag android.usedeprecatedndk is no longer supported and will be removed in the next version of android studio. please switch to a supported build system.

consider using cmake or ndk-build integration. for more information, go to:

https://d.android.com/r/studio-ui/add-native-code.html#ndkcompile

to get started, you can use the sample ndk-build script the android

plugin generated for you at:

/users/apple/desktop/androidjnitest/app/build/intermediates/ndk/debug/android.mk

alternatively, you can use the experimental plugin:

https://developer.android.com/r/tools/experimental-plugin.html

to continue using the deprecated ndk compile for another 60 days, set

android.deprecatedndkcompilelease=1512283120054 in gradle.properties

百思不得其姐啊,百度的答案大家都是这样做啊,为什么人家可以我的就不行呢,我的代码和他的一模一样啊

为什么人家可以我的就不行呢,我的代码和他的一模一样啊这句话作为程序员的我们很熟悉!难到我要放弃吗?no no no,作为程序员的我怎么能轻言放弃呢!每个人都有这样的经历,蓝瘦过、香菇过,到最后我们都找到我们的错误

来我们仔细看下log,大概意思就是说:

android.usedeprecatedndk不再支持了

让使用cmake or ndk-build

然后还有链接

考虑使用cmake或ndk构建集成。要了解更多信息,请访问:

首先,您可以使用android的ndk构建脚本示例插件为您生成:

/users/apple/desktop/androidjnitest/app/build/intermediates/ndk/debug/android.mk

或者,你可以使用实验插件:

继续使用已弃用的ndk编译60天,设置

在gradle.properties

android.deprecatedndkcompilelease = 1512283120054(这个测试不起作用)

经过各种查资料,发现原来在gradle3.0以上以前这种方法不在支持

学习过程就不详细描述了,直接上结果:

先通过sdkmanager下载:cmake和lldb

f1c43d19835d7a79d128ad207620bc1a.png

在build.gradle的defaultconfig节点下加入:

// 使用cmake工具

externalnativebuild {

cmake {

cppflags ""

//生成多个版本的so文件

abifilters 'arm64-v8a','armeabi-v7a','x86','x86_64'

}

}

在build.gradle的android节点下加入:

// 配置cmakelists.txt路径

externalnativebuild {

cmake {

path "cmakelists.txt" // 设置所要编写的c源码位置,以及编译后so文件的名字

}

}

42eaef07eb377fc3d388bb185169abb6.png

添加cmakelists.txt文件到build.gradle文件同级目录下,具体内容如下:

# 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.

#cmakelists.txt

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.

# 设置so文件名称.

nameprovider

# sets the library as a shared library.

shared

# 设置这个so文件为共享.

# provides a relative path to your source file(s).

# 设置这个so文件为共享.

src/main/jni/getname.cpp)

# 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

# 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.

# 制定目标库.

nameprovider

# links the target library to the log library

# included in the ndk.

${log-lib} )

09ae787a871814923be71c364e5d177f.png

至此,我们所有的流程都做完了,下面来检查一下我们的成果,见证奇迹的时候到了:

4f3588c22419f8f72da9bfc2412697e8.png

可以看到我们已经成功生成so文件,再来上个效果图:

9010a0c975d3bb2d7992b76cfa88e00b.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值