【Android-NDK】(一)cmake构建项目

本文详细介绍了如何在Android项目中集成NDK,包括设置CMakeLists.txt、链接外部库、使用JNI进行Java与C++交互,以及处理常见问题和集成过程。作者还分享了如何创建C++库和Java接口,以及如何在实际项目中应用这些技术。
摘要由CSDN通过智能技术生成

completing its build.

从系统查找依赖库

find_library( # Sets the name of the path variable.
#log日志库
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.
native-lib

Links the target library to the log library

included in the NDK.

${log-lib} )

  1. native-lib.cpp

#include <jni.h>
#include

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

  1. build.gradle


android {
defaultConfig {
externalNativeBuild {
cmake {
cppFlags “” // 命令参数,非必须
}
}
}

externalNativeBuild {
cmake {
path “src/main/cpp/CMakeLists.txt” // 指定了该文件的路径,必须
version “3.10.2” // 支持的最低版本,非必须
}
}
}

5.构建

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值