Cmake编写JNI
调用两个库
CMakeLists.txt
//把那种大段的注释去掉了
cmake_minimum_required(VERSION 3.4.)
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).
src/main/cpp/native-lib.cpp ) add_library( # Sets the name of the library.
nativeSecond-lib # Sets the library as a shared library.
SHARED # Provides a relative path to your source file(s).
src/main/cpp/nativeSecond-lib.cpp )
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 )
target_link_libraries( # Specifies the target library.
native-lib # Links the target library to the log library
# included in the NDK.
${log-lib} ) target_link_libraries( # Specifies the target library.
nativeSecond-lib # Links the target library to the log library
# included in the NDK.
${log-lib} )
native-lib.cpp
#include <jni.h>
#include <string> extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_aplex_cantest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
nativeSecond-lib.cpp
#include <jni.h>
#include <string> extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_aplex_cantest_MainActivity_stringFromJNI22(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello Second from C++";
return env->NewStringUTF(hello.c_str());
}
MainActivity.java
package com.example.aplex.cantest; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
System.loadLibrary("nativeSecond-lib");
}
Button bt;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Example of a call to a native method
tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI()); bt = (Button)findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setText(stringFromJNI22());
}
});
} /**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String stringFromJNI22();
}




Cmake编写JNI的更多相关文章
- Android JNI编程(八)——体验AS2.2.2编写Jni程序、Java调C、C调Java函数、将C代码中的Log打印至Logcat
版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 不得不说在AS2.2以上的版本进行开发就一个字——爽,在2.0上使用jni出 ...
- JNI技术基础(2)——从零开始编写JNI代码
书接上文: <JNI技术基础(1)——从零开始编写JNI代码> 2.编译源程序HelloWorld.java并生成HelloWorld.class 3.生成头文件HelloWorld.h ...
- 技术转载:Jni学习四:如何编写jni方法
转载:http://blog.chinaunix.net/u1/38994/showart_1099528.html 一.概述: 在这篇文章中将会简单介绍如何编制一些简单的JNI 方法.我们都知道JN ...
- 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口(老罗学习笔记4)
在上两篇文章中,我们介绍了如何为Android系统的硬件编写驱动程序,包括如何在Linux内核空间实现内核驱动程序和在用户空间实现硬件抽象层接口.实现这两者的目的是为了向更上一层提供硬件访问接口,即为 ...
- 为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
在上两篇文章中,我们介绍了如何为Android系统的硬件编写驱动程序,包括如何在Linux内核空间实现内核驱动程序和在用户空间实现硬件抽象层接 口.实现这两者的目的是为了向更上一层提供硬件访问接口,即 ...
- Android Studio 编写 JNI
之前一直都不知怎么编写JNI,今天刚好学习一下,感谢梦真的指教,以及提供的文档. 参考链接 http://blog.csdn.net/u011168565/article/details/518781 ...
- JNI技术基础(1)——从零开始编写JNI代码
众所周知,Java程序的最大特点就是其跨平台的特性,编写的上层应用程序可以不加任何修改甚至不用重新编译而运行于不同的平台上,然而,Java本身也存着这一个弊端,那就是性能上相对要差一些,在对性能要求比 ...
- 【转载】cmake编写
Cmake的输入是在源码目录下的CMakeLists.txt文件.这个文件可以用include或者 add_subdirectory 命令增加入其它的输入文件. 语法 CMakeList.txt文件是 ...
- Android(java)学习笔记261:JNI之编写jni程序适配所有处理器型号
1. 还是以"02_两个数相加"为例,你会发现这个jni程序只能在ARM处理器下运行,如下: 如果我们让上面的程序运行在x86模拟器上,处理平台不对应,报如下错误: 03-29 ...
随机推荐
- WPF 最简单的TextBox水印
最简单的TextBox加水印的方法,但是不具有很强的通用性. 如果你只是使用一次,或者用的不多,偷偷懒可以使用. 因为此方法只需要修改TextBox的Template,而不用重写何任代码. 注意: 1 ...
- Sending Email In .NET Core 2.0
Consider the following written in .NET Core 2.0. SmtpClient client = ) { UseDefaultCredentials = tru ...
- 【转一篇出处不明的文章】 Windows多线程通信方式
多线程通信的方法主要有以下三种: 1.全局变量 进程中的线程间内存共享,这是比较常用的通信方式和交互方式.注:定义全局变量时最好使用volatile来定义,以防编译器对此变量进行优化. 2.Messa ...
- 项目笔记---事半功倍之GhostDoc(二)
前言 前一篇文章<项目笔记---事半功倍之StyleCop(一)>提到如何约束代码,规范代码风格,这一节,我们将了解如何快速生成符合规则的代码注释---GhostDoc 一.安装Ghost ...
- winform之combobox
绑定键值对: ArrayList mylist = new ArrayList(); mylist.Add(new DictionaryEntry("WinFormURL", &q ...
- asp.net 网站监控方案
前言:监控web网站方法有很多种,这篇文章说一下对windows服务器 asp.net网站的监控 采用的方案,Powershell + Influxdb + Grafana 1.PowerShell ...
- JEECG(一) 如何配置自己的业务包
使用自己的业务包开发,不在com.jeecg业务包下 请首先阅读这篇文章[官网] http://www.jeecg.org/forum.php?mod=viewthread&tid=1832& ...
- 敏捷开发-代码提交流程& 安装gerrit
- Day 39 管道 、数据共享与地址池
参考张磊同学的博客 http://www.cnblogs.com/chongdongxiaoyu/p/8658379.html 一.管道 #创建管道的类: Pipe([duplex]):在进程之间创建 ...
- Day 31 面向对象考试题 第四次考试.
一 基础知识和函数: 1.文件操作有哪些模式?请简述各模式的作用 r ,只读模式[默认模式,文件必须存在,不存在则抛出异常] w,只写模式[不可读:不存在则创建:存在则清空内容] x, 只写模式[不可 ...