我创建了一个包含 C++ NDK 的新项目。所以它自动创建了一个CMakeLists.txt
文件和一个cpp
目录。但是当我创建一个名为 jni
的新目录并尝试放入 test.c
、Android.mk
和 Application.mk
文件,当我想同步我的项目时收到此错误消息:
This file is not part of the proejct.please include it to appropriaten build file
安卓.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := test.c
应用程序.mk:
APP_ABI := all
测试.c:
#include <jni.h>
#include <string.h>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_m_ndktest_MainActivity_testJNI(JNIEnv *env, jobject instance)
{
return env->NewStringUTF(hello.c_str());
}
JNIEXPORT jstring JNICALL
Java_com_example_m_ndktest_MainActivity_testJNI2(JNIEnv *env, jobject instance)
{
return (*env)->NewStringUTF(env, "hellooooo");
}
我应该如何将我的 .mk
文件包含在 CMakeLists.txt
中?
CMakeLists.txt:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# 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.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/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.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
***Also I have a question, where can I find the tutorial about the STRANGE syntax that we should write in Android-ndk? (I mean the test.c syntax that I copied from the default
native-lib.c
)
最佳答案
Android Studio 有外部原生构建的概念。对于这些外部 native 构建,您有 two选项:
- ndk 构建
- CMake
CMake 似乎是更现代的,因此可能更适合 future 。我建议使用那个。
这意味着您编写了一个 CMakeLists.txt
文件(通常在您的 jni/
目录中。)
您不再需要任何 .mk 文件,它们由 ndk-build 外部原生构建使用,而不是 CMake 构建。
要指示您使用的是哪个外部 native 构建系统,您可以使用应用的 gradle 文件。
android {
defaultConfig {
externalNativeBuild {
cmake {
cppFlags "..."
arguments "..."
}
}
}
}
在 CMakeLists.txt 文件中,您描述了如何构建原生 .so 库。这包括列出构建它所需的所有 .c
和 .cpp
源文件。这是通过 CMake 的 add_library() 完成的。宏。
总而言之,您的新项目定义将包含 gradle 文件和一个(或多个)CMakeLists.txt 文件,但您不再需要任何 .mk 文件。
关于c++ - 如何将 "jni/*.c/cpp"文件添加到 CMakeLists.txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49297792/