android studio搭建NDK环境,使用JNI (Java 调 C) 二 通过CMake

在Java文件中写native方法
复制代码
package com.example.myapplication;

public class JNI {

    //加载so库
    static {
        System.loadLibrary("FirstJni");
    }

    //native方法
    public static native String sayHello();

}
在Termial控制台中cd到项目目录中的源码目录下,并执行 javac -encoding utf-8 -h . JNI.java 命令生成.h文件
在项目main目录下创建jni文件夹,将.h文件和.class文件copy到jni文件夹中

在 JNI 文件夹下新建 hello.c 文件,把 .h 文件内容拷贝过来,并实现函数体,如下所示,在c文件内返回一个字符串 JNI HELLO WORLD

.h头文件中Java_com_example_myapplication_JNI_sayHello 格式为 Java_包名_类名_方法名

在 C/C++ 文件中实现 Java_com_example_myapplication_JNI_sayHello 方法

复制代码
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_myapplication_JNI */

#ifndef _Included_com_example_myapplication_JNI
#define _Included_com_example_myapplication_JNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_myapplication_JNI
 * Method:    sayHello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_myapplication_JNI_sayHello
        (JNIEnv *env, jclass){
    return (*env)->NewStringUTF(env, "JNI HELLO WORLD");
}

#ifdef __cplusplus
}
#endif
#endif
开始配置gradle文件
复制代码
    externalNativeBuild {
        cmake {
            path ("CMakeLists.txt")
        }
    }
    ndkVersion = "28.0.12916984"
同时在 app 文件夹下新建 CMakeLists.txt
复制代码
cmake_minimum_required(VERSION 3.4.1)
add_library(
        FirstJni
        SHARED
        src/main/jni/hello.c)
find_library(
        log-lib
        log)
target_link_libraries(
        FirstJni
        ${log-lib})
rebuild项目,生成so库

so库地址:

app\build\intermediates\merged_native_libs\debug\mergeDebugNativeLibs\out\lib\

到此为止,JNI的配置完成

使用JNI

复制代码
package com.example.myapplication;

import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText(JNI.sayHello());
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
如果想在c文件中打印日志
在 C 文件中引入 android/log.h 头文件,并定义 Log 的宏:
复制代码
#include <jni.h>
#include <android/log.h> // 引入 Android Log 头文件

// 定义 Log 的 TAG 和宏
#define LOG_TAG "JNI_LOG"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
  • LOGI:用于打印 INFO 级别的日志。

  • LOGE:用于打印 ERROR 级别的日志。

相关推荐
笨鸭先游1 小时前
前台--Android开发
android
fareast_mzh1 小时前
Lightweight App Alternatives
android
小杜-coding2 小时前
黑马点评day04(分布式锁-setnx)
java·spring boot·redis·分布式·spring·java-ee·mybatis
caihuayuan53 小时前
升级element-ui步骤
java·大数据·spring boot·后端·课程设计
佩奇的技术笔记4 小时前
Java学习手册:单体架构到微服务演进
java·微服务·架构
zm4 小时前
服务器多客户端连接核心要点(1)
java·开发语言
FuckPatience5 小时前
关于C#项目中 服务层使用接口的问题
java·开发语言·c#
天上掉下来个程小白5 小时前
缓存套餐-01.Spring Cache介绍和常用注解
java·redis·spring·缓存·spring cache·苍穹外卖
pq113_65 小时前
OrangePi Zero 3学习笔记(Android篇)4 - eudev编译(获取libudev.so)
android·笔记·学习
揣晓丹5 小时前
JAVA实战开源项目:健身房管理系统 (Vue+SpringBoot) 附源码
java·vue.js·spring boot·后端·开源