安卓开发(二):Native C++

com.example.myapplication.MainActivity.java

/**
 * A native method that is implemented by the 'myapplication' native library,
 * which is packaged with this application.
 */
public native String stringFromJNI();
#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

Android基于NDK将C/C++代码编译生成库,供上层的Java/Cotlin应用调用
官方文档:https://developer.android.com/ndk?hl=zh-cn

注意CMake编译时的各种参数

何为交叉编译 (cross compile)
https://www.jianshu.com/p/bebab7c12991#1678095848039

CMake编译时需要指定NDK路径,指定工具链
build_for_android.sh脚本文件

Todo: JNI (Jave Native Interface)
cpp <---> jni <---> java

posted @ 2024-01-03 09:15  达可奈特  阅读(130)  评论(0编辑  收藏  举报