使用最新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的更多相关文章

  1. 鸿蒙手机版JNI实战(JNI开发、SO库生成、SO库使用)

    鸿蒙JNI开发现状 现阶段,不仅鸿蒙JNI相关的开发资料较少,而且Phone相关的JNI开发资料几乎没有,这对于新入行的鸿蒙开发者们来说,非常不友好. 也可能会给Android工程(使用了SO库的工程 ...

  2. JNI实战(二):Java 调用 C

    1. JNI Env 和 Java VM 关系说明 JNIEnv 是 Java的本地化环境,是Java与C的交互的重要桥梁. 在Android上,一个进程对应一个JavaVM,也就是一个app对应一个 ...

  3. JNI/NDK开发指南(一)—— JNI开发流程及HelloWorld

    转载请注明出处:http://blog.csdn.net/xyang81/article/details/41777471 JNI全称是Java Native Interface(Java本地接口)单 ...

  4. JNI实战(三):JNI 数据类型映射

    在JNI实战(二):Java 调用 C 我们了解了JNI的静态注册和动态注册.也知道我们应该使用动态注册来进行JNI函数与Java方法之间的映射. 示例的映射表的数组为如下: static JNINa ...

  5. JNI开发流程-JNI/NDK【转】

    本文转载自:http://wiki.jikexueyuan.com/project/jni-ndk-developer-guide/workflow.html 开发流程 JNI 全称是 Java Na ...

  6. Android JNI学习(四)——JNI的常用方法的中文API

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  7. JNI 实战全面解析

    项目决定移植一款C++开源项目到Android平台,开始对JNI深入研究. JNI是什么? JNI(JavaNative Interface)意为Java本地调用,它允许Java代码和其他语言写的代码 ...

  8. JNI实战(四):C 调用 Java

    在前面我们讲了一下如何使用Java调用C,本文我们讲一下如何使用JNI提供的接口来反射得到Java方法进行调用. 主要步骤如下: FindClass (找到Java层的类) GetMethodID / ...

  9. (转)JNI入门教程之HelloWorld篇 .

    转: http://blog.csdn.net/mingjava/article/details/180946 本文讲述如何使用JNI技术实现HelloWorld,目的是让读者熟悉JNI的机制并编写第 ...

随机推荐

  1. Java软件工程的弹幕调试原则

    日期:2019.4.25 博客期:054 星期四 今天是把很久之前的那个相关程序——一维数组的最大和的子数组的求取信息,我们今天的任务就是把每一步的信息都要进行输出查看! 如下图: package p ...

  2. How hacker do IT: Tricks Tools and Techniques (翻译)

    本资料是 Alex Noordergraaf 企业产品的说明书   现在整理如下: 第一部分: How hackers Do It : Tricks   Tools  and Techniques 本 ...

  3. insert into

    1. INSERT INTO t1(field1,field2) VALUE(v001,v002);            // 明确只插入一条Value 2. INSERT INTO t1(fiel ...

  4. html中div使用CSS实现水平/垂直居中的多种方式

    CSS中的居中,在工作中,会经常遇到.它可以分为水平居中和垂直居中,以下是几种实现居中的方式. git 查看源码 配合在线预览,效果更佳 以下例子中,涉及到的CSS属性值. .parent-frame ...

  5. org.json.JSONObject的getString和optString使用注意事项

    结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会 /** * Returns the value mapped by {@co ...

  6. SQL查询时,根据日期范围查询周

    周数据会有weekbegin和weekend两个字段,用来描述周的开始日期和结束日期. 现在项目中的日历控件不支持周查询,只支持日期查询,所以目前面临的问题是,根据时间范围,查询周. 最终sql如下: ...

  7. C语言--第四次作业--数组

    1.本章学习总结 1.1 思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 不知不觉都快学习C语言结束了,自从开始了数组的学习就感觉难度瞬间几何级上升鸭(让人头大,感觉到了各种绝望), ...

  8. 在VUE-CLI 3下的第一个Element-ui项目(菜鸟专用)

    vue-cli3.0使用及配置 (https://www.cnblogs.com/xzqyun/p/10779891.html  ) 以上是  vue-cli3.0使用及配置   这里我们来引用基于v ...

  9. D3---01基础的柱状图制作(转)

    ---文章转自 http://d3.decembercafe.org/index.html  ,Created by 十二月咖啡馆. 一个完整的柱形图包含三部分:矩形.文字.坐标轴. 首先要布置一个大 ...

  10. Java对象转换成Map

    需求总是千奇百怪,对象转成map,看似没必要,但就是有这个需求,所以记录下来 首先是Bean package tools; import lombok.Data; /** * 车辆实体类 */ @Da ...