当前位置: 首页 > news >正文

做网站就seo外包公司兴田德润

做网站就,seo外包公司兴田德润,vc 做网站源码,胶州网站建设 网络推广今天学习下如何编写第一个JNI代码,实现JNI需要先下载NDK,由于现在Android Studio使用CMake编译jni的C/C文件,所以还需要下载CMake和LLDB,如下图通过Android Studio来下载: 1.新建一个工程NDKSimple,如下图: (图中cp…

今天学习下如何编写第一个JNI代码,实现JNI需要先下载NDK,由于现在Android Studio使用CMake编译jni的C/C++文件,所以还需要下载CMake和LLDB,如下图通过Android Studio来下载:
这里写图片描述

1.新建一个工程NDKSimple,如下图:

这里写图片描述
(图中cpp文件的生成会在下面讲到)

2.检查下NDK环境配置好了没有。步骤如下:

检查SDK Location里面的NDK路径,右键点击app->open module Settings,查看SDK location的NDK路径:
这里写图片描述
检查local.properties文件里面有没有NDK路径:
这里写图片描述

3.编写JNI接口

如下图编写一个简单的获取字符串的JNI接口
这里写图片描述
这里我已经编译好了,所以getResult()不会变红,这里通过System.loadLibrary(“liuxin”);来加载库,这里库的名字需要跟你的build.gradle ndk节点下面的名字一样,这个后面还会讲到。

4.编写C/C++程序

然后build编译工程,检查NdkInterface.java编译后有没有生成class文件,文件位置如下图:
这里写图片描述
然后使用javah 生成头文件,打开Terminal,输入命令进入到debug目录下,命令如下:
CD C:\Users\Administrator\Desktop\NDKSimple\app\build\intermediates\classes\debug
然后使用javah+包名+文件路径来生成头文件,命令如下:
javah com.liu.xin.ndksimple.ndk.NdkInterface
这是我们会发现在debug目录下生成了一个.h文件,如下图:
这里写图片描述
有了头文件的声明还不行,我们需要定义实现这个方法
我们在main下新建一个jni文件夹,如图
这里写图片描述
然后把刚生成的头文件复制到这个目录并且编写.c/.cpp程序

//NDKInterface.c文件
#include "com_liuxin_ndksimple_ndk_NdkInterface.h"
JNIEXPORT jstring JNICALL Java_com_liuxin_ndksimple_ndk_NdkInterface_getResult(JNIEnv * jniEnv, jobject obj){return (*jniEnv)->NewStringUTF(jniEnv,"liuxin");}

5.使用CMake编译生成.so文件

在app的builde.grade文件中添加如下代码:在defaultConfig中

 ndk {    //这里的名称要和上面加载库中库名称一样moduleName "liuxin"}// 使用Cmake工具externalNativeBuild {cmake {cppFlags ""//生成多个版本的so文件abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'}}

在android{}中配置CMakeLists.txt路径

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

在在app的builde.grade文件新建CMakeLists.txt文件,添加如下配置信息:

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.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 it for you.
# Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.AndroidStudio开始支持Cmake了,ndk感觉挺费劲的,这个是不是好玩点,,这里是要生成的库的文件名 libtest.so#这里是liuxinliuxin# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).# Associated headers in the same location as their source# file are automatically included.对应的C文件的目录位置src/main/jni/NdkInterface.c)# 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.指定依赖库#这里是liuxinliuxin# Links the target library to the log library# included in the NDK.关联日志记录库文件,在ndk目录中${log-lib} )

如上文件中,主要注意add_library中配置你要生成so文件的名称,以及C/C++文件的路径;而target_link_libraries中指定依赖库的文件名

调用JNI,编译程序

如下图在MainActivity中调用JNI:
这里写图片描述
最后rebuild 工程,运行程序,如图:
这里写图片描述

到此就结束了。

http://www.wooajung.com/news/35210.html

相关文章:

  • 网站设计步骤详解推销
  • 深圳网站建设 贝尔利友情链接论坛
  • 开发网站流程seo专业培训机构
  • 郑州做网站的大公司韶关新闻最新今日头条
  • 室内设计做效果图可以接单的网站中文搜索引擎排名
  • 绍兴网站建设技术外包什么叫营销
  • 衡水移动端网站建设win7优化工具
  • 中华建设网算什么级别网站百度关键词优化系统
  • 最专业的房地产网站建设新乡网络推广外包
  • 《网站基础建设-首保》女教师网课入06654侵录屏
  • 平度好的建设网站web成品网站源码免费
  • 做问卷比较好的网站排名优化公司
  • 新乡网站建设公司广告平台网站有哪些
  • 怎么保证网站安全性竞价托管多少钱
  • 商丘企业做网站关键词上首页的有效方法
  • cms做的网站胡源代码推广营销app
  • 做个网站成功案例公众号怎么做文章推广
  • html5个人网站模板百度搜索竞价排名
  • 网站认证主体培训机构招生方案
  • 个人网站制作wordpress全面网络推广营销策划
  • 我们的服务项目类型正版seo搜索引擎
  • 吕梁建站公司windows优化大师官网
  • 网站开发的教学网站地推团队去哪里找
  • 做足球网站前景免费创建网站平台
  • 巴彦淖尔网站制作全自动引流推广软件下载
  • 软件用户界面设计成都高薪seo
  • 网站建设海外推广 香港企业网站建设门户
  • 普洱做网站的报价建站abc
  • 做网站的用什么主机好本地推广平台
  • 网站开发都做些什么站长工具网站查询