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代码编译配置)。

cpp 复制代码
// 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());
}
bash 复制代码
// 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中编译配置

bash 复制代码
android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            // 设置生成so的arm架构
            cmake {
                cppFlags ''
                abiFilters 'arm64-v8a'
            }
        }
    }

    externalNativeBuild {
        cmake {
            // 编译
            path file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'
        }
    }
}
相关推荐
木头没有瓜13 分钟前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp
键盘侠00715 分钟前
springboot 上传图片 转存成webp
android·spring boot·okhttp
江上清风山间明月41 分钟前
flutter bottomSheet 控件详解
android·flutter·底部导航·bottomsheet
羑悻的小杀马特1 小时前
【AIGC篇】畅谈游戏开发设计中AIGC所发挥的不可或缺的作用
c++·人工智能·aigc·游戏开发
闻缺陷则喜何志丹1 小时前
【C++动态规划】1105. 填充书架|2104
c++·算法·动态规划·力扣·高度·最小·书架
初学者丶一起加油2 小时前
C语言基础:指针(数组指针与指针数组)
linux·c语言·开发语言·数据结构·c++·算法·visual studio
CodeClimb2 小时前
【华为OD-E卷-租车骑绿道 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
Crossoads3 小时前
【汇编语言】外中断(一)—— 外中断的魔法:PC机键盘如何触发计算机响应
android·开发语言·数据库·深度学习·机器学习·计算机外设·汇编语言
易码智能3 小时前
【RealTimeCallBack】- KRTS C++示例精讲(4)
c++·定时器·kithara·windows 实时套件·krts
小王爱吃月亮糖3 小时前
QT-QVariant类应用
开发语言·c++·笔记·qt·visual studio