JNI实战(一):JNI HelloWorld
使用最新Android Studio的Cmake,创建一个Native C++项目后,我们就可以看到JNI的Hello World的项目及示例代码了。
JNI的项目代码,分为三层:Java层,C++层,CMake层。
其中Java层定义了调用C++的方法,C++层实现这个方法,CMake定义了如何将C++层的内容打包为一个动态库。
1. Java 层:
package com.renhui.jnihelloworld; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
} /**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
2. C++层:
#include <jni.h>
#include <string> extern "C" // 指示编译器这部分代码按C语言的进行编译
JNIEXPORT jstring JNICALL Java_com_renhui_jnihelloworld_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
3. CMake编译脚本:
CMakeList.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 // 生成的库的名字 # Sets the library as a shared library.
SHARED // 动态库 # Provides a relative path to your source file(s).
native-lib.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. // 将哪些库链接到一起
native-lib // 目标库 # Links the target library to the log library
# included in the NDK.
${log-lib}) // 要链接的库
JNI实战(一):JNI HelloWorld的更多相关文章
- 鸿蒙手机版JNI实战(JNI开发、SO库生成、SO库使用)
鸿蒙JNI开发现状 现阶段,不仅鸿蒙JNI相关的开发资料较少,而且Phone相关的JNI开发资料几乎没有,这对于新入行的鸿蒙开发者们来说,非常不友好. 也可能会给Android工程(使用了SO库的工程 ...
- JNI实战(二):Java 调用 C
1. JNI Env 和 Java VM 关系说明 JNIEnv 是 Java的本地化环境,是Java与C的交互的重要桥梁. 在Android上,一个进程对应一个JavaVM,也就是一个app对应一个 ...
- JNI/NDK开发指南(一)—— JNI开发流程及HelloWorld
转载请注明出处:http://blog.csdn.net/xyang81/article/details/41777471 JNI全称是Java Native Interface(Java本地接口)单 ...
- JNI实战(三):JNI 数据类型映射
在JNI实战(二):Java 调用 C 我们了解了JNI的静态注册和动态注册.也知道我们应该使用动态注册来进行JNI函数与Java方法之间的映射. 示例的映射表的数组为如下: static JNINa ...
- JNI开发流程-JNI/NDK【转】
本文转载自:http://wiki.jikexueyuan.com/project/jni-ndk-developer-guide/workflow.html 开发流程 JNI 全称是 Java Na ...
- Android JNI学习(四)——JNI的常用方法的中文API
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- JNI 实战全面解析
项目决定移植一款C++开源项目到Android平台,开始对JNI深入研究. JNI是什么? JNI(JavaNative Interface)意为Java本地调用,它允许Java代码和其他语言写的代码 ...
- JNI实战(四):C 调用 Java
在前面我们讲了一下如何使用Java调用C,本文我们讲一下如何使用JNI提供的接口来反射得到Java方法进行调用. 主要步骤如下: FindClass (找到Java层的类) GetMethodID / ...
- (转)JNI入门教程之HelloWorld篇 .
转: http://blog.csdn.net/mingjava/article/details/180946 本文讲述如何使用JNI技术实现HelloWorld,目的是让读者熟悉JNI的机制并编写第 ...
随机推荐
- javascript中new Date()存在的兼容性问题
问题:通过new Date()创建的时间对象在Chrome能正常工作,但在IE浏览器却显示NaN 代码: var time = new Date(date + ' 00:00:00'); //NaN ...
- centos搭建git服务
一.服务器yum -y install git git init --bare test.gitcd test.gitpwd //打印当前目录,假设是:/home/root/git/test.gitg ...
- 生活日历NABCD需求分析
这次我们团队要开发一个生活日历APP,对于这个APP的NABCD的需求分析,我对此作出其中的一小部分介绍. N(Need)需求 目前市场上有很多的日历程序,每个手机自带的功能中必然有日历程序.但是对于 ...
- 写交互式脚本时,遇到到报错:not a regular file
场景:在写两台ubuntu之间需要交互式登录操作shell脚本.就是在ubantu A上,做点什么事,然后远程到ubuntu B上进行操作,shell脚本存放在ubuntu A上. 我遇到这个报错:n ...
- CocosCreator检测动作执行完毕的方法~之一吧,应该= =
解决方案是利用动作序列,在动作后面跟一个回调函数,然后再利用之前设置好的动作执行完毕标志,即可完成动作结束的判断!Bingo!
- CyclicBarrier介绍
应用场景 在某种需求中,比如一个大型的任务,常常需要分配好多子任务去执行,只有当所有子任务都执行完成时候,才能执行主任务,这时候,就可以选择CyclicBarrier了. 实例分析 我们需要统计全国的 ...
- [sublime] 利用sublime搭建C/C++编译器
gcc/g++配置 先去下载TDM-GCC安装包,这里附下载地址(可能会有弹出界面,不用管他). 现在c盘中建立文件夹 g++,然后以管理员运行,点击Create傻瓜式安装, 这里要改一下安装路径,保 ...
- SQL server 查询出现:---“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析---
最近用select进行数据筛选,碰到下面的这个错误: ---子查询返回的值不止一个.当子查询跟随在 =.!=.<.<=.>.>= 之后,或子查询用作表达式时,这种情况是不允许的 ...
- 展开被 SpringBoot 玩的日子 《 二 》 WEB 开发
上篇文章介绍了Spring boot初级教程 :< spring boot(一):入门篇 >,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spr ...
- 英语知识积累-D01-body+animal
My body What's your name? Here are lots of children playing. Are they happy or sad? Who's waving at ...