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

Android Studio中使用cmake开发JNI实战

JNI学习大纲

一、JNI编程入门

二、Android Studio中使用cmake开发JNI实战

第一章节我们介绍了JNI的开发步骤,那这一章节我们就开始在Android Studio中实战一下吧,Let's Start。

1. Android Studio中安装CMake插件

  • AS中菜单栏选择Tools>SDK Manager
  • 在Android SDK中选择SDK Tools,安装CMake和NDK。

2. JNI开发

2.1 编写JNI代码

在项目工程下的src/main创建cpp目录,编写native-lib.cpp(JNI代码实现文件)和对应的CMakeLists.txt(JNI代码编译配置)。

// native-lib.cpp#include <jni.h>
#include <android/log.h>
#include <string>extern "C" JNIEXPORT jstring JNICALL
Java_com_jni_test_JNITestService_stringByJNI(JNIEnv *env, jobject /* this */) {std::string hello = "hello JNI from C++";return env->NewStringUTF(hello.c_str());
}
// CMakeLists.txt# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)# Declares and names the project.
project("jnitest")# 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.
set(project_root_dir ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
include_directories(${project_root_dir}/common)add_library( # Sets the name of the library.jnitest# 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.jnitest# Links the target library to the log library included in the NDK.${log-lib}
)

2.2 gradle中编译配置

android {...defaultConfig {...externalNativeBuild {// 设置生成so的arm架构cmake {cppFlags ''abiFilters 'arm64-v8a'}}}externalNativeBuild {cmake {// 编译path file('src/main/cpp/CMakeLists.txt')version '3.10.2'}}
}

http://www.lryc.cn/news/111921.html

相关文章:

  • 第七章 图论
  • IEEE SystemVerilog Chapter13 : Tasks and functions (subroutines)
  • day39反转字符串总结
  • 使用Socket实现TCP版的回显服务器
  • 【Nacos篇】Nacos基本操作及配置
  • Dockerfile构建Tomcat镜像
  • k8s的介绍
  • mysql sql语句 需要使用like 场景,解决方案
  • 通过C语言设计的贪吃蛇游戏(控制台终端)
  • c++实现Qt信号和槽机制
  • 【Linux】五、进程
  • 使用 OpenCV 和 Python 卡通化图像-附源码
  • GitLab不同角色对应的权限
  • 手写一个简易的布隆过滤器
  • 阿里云快速部署开发环境 (Apache + Mysql8.0)
  • 侧边栏的打开与收起
  • 贝叶斯学习
  • Java并发系列之六:CountDownLatch
  • 24数据结构-图的基本概念与存储结构
  • 自然语言处理学习笔记(三)————HanLP安装与使用
  • CS 144 Lab Five -- the network interface
  • Mecha
  • Apache RocketMQ之集成RocketMQ_MQTT 安装部署协议
  • Oracle多行数据合并为一行数据,并将列数据转为字段名
  • MySQL5.7 与 MariaDB10.1 审计插件兼容性验证
  • PyTorch Lightning教程五:Debug调试
  • 末流211无科研保研经验分享
  • 日期选择器多选换行
  • NodeJS原型链污染ctfshow_nodejs
  • 18. SpringBoot 如何在 POM 中引入本地 JAR 包