Android Studio JNI 使用模板:c/cpp源文件的集成编译,快速上手

一、前言

  • JNI 技术,使得Java可以调用C/CPP编写的代码库,也是老技术了,对于不想花时间研究的同学,可以照抄本文的编译模板。
  • JNI代码的AS编译,有两种途径,其一是NDK配置编译,其二是cmake的配置编译,本文采用第二种,也是AS直接支持创建的方式。

二、实现步骤

2.1 创建 cpp目录

  • tv-settings\app\src\main\cpp

2.2 创建jni文件

  1. CMakeList.txt 文件,负责处理编译
  2. 你的 c/cpp 源码文件,本文: JniMotor.c

图示如下:

2.3 编辑CMakeList.txt

bash 复制代码
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
# 备注:如直接采用AS创建的JNI项目,会将project name作为so库的名字
project("TvSettings")

# 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.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
# 备注:此处罗列你的 c / cpp 文件
add_library(jnimotor SHARED
        # List C/C++ source files with relative paths to this CMakeLists.txt.
        JniMotor.c)

# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
# 备注:此处第一个参数jnimotor,就是生产的库的名字:libjnimotor.so
target_link_libraries(jnimotor
        # List libraries link to the target library
        android
        log)

2.4 编辑 JniMotor.c

  • 以下为C源文件模板
  • 如采用CPP的同学,记得添加extern "C" 标记,因为CPP编译产生的符号与C不同,需要产生C的符号供JAVA调用。
c 复制代码
//略......
#include <jni.h>
#include <android/log.h>
#define TAG "JniMonitor"

#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)

//略......


static int motorioctl(int des, int step)
{
//略......
}

JNIEXPORT jint JNICALL Java_com_android_tv_settings_device_displaysound_aw_1displaysound_JniMotor_ioctl
  (JNIEnv *env, jclass clas, jint des, jint step){
    int i = des;
    int j = step;
    return motorioctl(i, j);
}

2.5 编辑build.gradle

  • 在 app/build.gradle 的android{} 增加如下配置,指明使用Native build
groovy 复制代码
android {
 ...... 略 ......
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.22.1'
        }
    }
 ...... 略 ......
}

三、执行编译

  • 如下图所示, 直接产生了个eabi所需的SO文件,大功告成

编译结果:图示一

编译结果:图示二

相关推荐
茜茜西西CeCe1 小时前
移动技术开发:登录注册界面
java·gitee·gradle·android studio·安卓·移动技术开发·原生安卓开发
Good_tea_h1 小时前
Android中的单例模式
android·单例模式
计算机源码社6 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
丶白泽6 小时前
重修设计模式-结构型-门面模式
android
晨春计8 小时前
【git】
android·linux·git
标标大人9 小时前
c语言中的局部跳转以及全局跳转
android·c语言·开发语言
竹林海中敲代码9 小时前
Qt安卓开发连接手机调试(红米K60为例)
android·qt·智能手机
木鬼与槐10 小时前
MySQL高阶1783-大满贯数量
android·数据库·mysql
iofomo10 小时前
【Abyss】Android 平台应用级系统调用拦截框架
android·开发工具·移动端
AirDroid_cn12 小时前
在家找不到手机?除了语音助手,还可以用远程控制!
android·智能手机·远程控制·手机使用技巧·远程控制手机