调用两个库

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的更多相关文章

  1. Android JNI编程(八)——体验AS2.2.2编写Jni程序、Java调C、C调Java函数、将C代码中的Log打印至Logcat

    版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 不得不说在AS2.2以上的版本进行开发就一个字——爽,在2.0上使用jni出 ...

  2. JNI技术基础(2)——从零开始编写JNI代码

    书接上文: <JNI技术基础(1)——从零开始编写JNI代码> 2.编译源程序HelloWorld.java并生成HelloWorld.class 3.生成头文件HelloWorld.h ...

  3. 技术转载:Jni学习四:如何编写jni方法

    转载:http://blog.chinaunix.net/u1/38994/showart_1099528.html 一.概述: 在这篇文章中将会简单介绍如何编制一些简单的JNI 方法.我们都知道JN ...

  4. 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口(老罗学习笔记4)

    在上两篇文章中,我们介绍了如何为Android系统的硬件编写驱动程序,包括如何在Linux内核空间实现内核驱动程序和在用户空间实现硬件抽象层接口.实现这两者的目的是为了向更上一层提供硬件访问接口,即为 ...

  5. 为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口

    在上两篇文章中,我们介绍了如何为Android系统的硬件编写驱动程序,包括如何在Linux内核空间实现内核驱动程序和在用户空间实现硬件抽象层接 口.实现这两者的目的是为了向更上一层提供硬件访问接口,即 ...

  6. Android Studio 编写 JNI

    之前一直都不知怎么编写JNI,今天刚好学习一下,感谢梦真的指教,以及提供的文档. 参考链接 http://blog.csdn.net/u011168565/article/details/518781 ...

  7. JNI技术基础(1)——从零开始编写JNI代码

    众所周知,Java程序的最大特点就是其跨平台的特性,编写的上层应用程序可以不加任何修改甚至不用重新编译而运行于不同的平台上,然而,Java本身也存着这一个弊端,那就是性能上相对要差一些,在对性能要求比 ...

  8. 【转载】cmake编写

    Cmake的输入是在源码目录下的CMakeLists.txt文件.这个文件可以用include或者 add_subdirectory 命令增加入其它的输入文件. 语法 CMakeList.txt文件是 ...

  9. Android(java)学习笔记261:JNI之编写jni程序适配所有处理器型号

    1. 还是以"02_两个数相加"为例,你会发现这个jni程序只能在ARM处理器下运行,如下:  如果我们让上面的程序运行在x86模拟器上,处理平台不对应,报如下错误: 03-29 ...

随机推荐

  1. Layui:前后端分离之Form表单

    页面效果图: <div style="display: none;" id="formContainer"> <form class=&quo ...

  2. go语言的null值问题

    关于go语言数据库存储和显示null值的问题困扰了我很久,并且也和群友讨论过这个问题,但是都没有得到相对满意和全面的答案.最近FQ找了几篇相对详细和权威的文章,分享给大家,希望和大家一起进步,go g ...

  3. 使用node_redis进行redis数据库crud操作

    正在学习使用pomelo开发游戏服务器,碰到node.js操作redis,记录一下 假设应用场景是操作一个用户表的数据 引入node_redis库,创建客户端 var redis = require( ...

  4. LD算法的C++实现(基于编辑距离的文本比较算法)

    算法看这里: http://www.cnblogs.com/grenet/archive/2010/06/01/1748448.html 用数组实现: #include <iostream> ...

  5. Android Source 源码已下载但 Android Studio 找不到的解决办法

    Android Studio 2.1 reporting in: solved the issue by resetting SDK. Preferences -> Appearance &am ...

  6. Day 8 集合与文件的操作

    一.创建集合两种方式. 二.添加元素的方式(add.update"属于迭代添加") 一.集合# 1. 集合是无序的,不能重复的.# 2.集合内元素必须是可哈希的.# 3.集合不能更 ...

  7. 深入学习c++--左值引用和右值引用

    #include <iostream> #include <string> #include <vector> using namespace std; int m ...

  8. Organising the Organisation(uva10766)(生成树计数)

    Input Output Sample Input 5 5 2 3 1 3 4 4 5 1 4 5 3 4 1 1 1 4 3 0 2 Sample Output 3 8 3 题意: 有一张图上有\( ...

  9. js处理想要得到数据结构

    例1 var arr = [ { date: "2018-01-10", time: "11:00" }, { date: "2018-01-10&q ...

  10. zoj2893 Evolution(矩阵快速幂)

    题意:就是说物种进化,有N种物种,编号是0——N-1,M次进化后,问你编号为N-1的物种有多少数量:其中要注意的就是i物种进化到j物种的概率是p:(那么剩下的不要忘了):所以单位矩阵初始化对角线的值为 ...