我的ndk演示已经正确运行。但是当我包含一些其他文件时,则抛出构建错误。
的build.gradle:
ndk {
moduleName "addcomputer"
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86','x86_64' // <- only the supported ones
}
task ndkBuild(type:Exec,description:'Compile JNI source via NDK'){
commandLine "/Users/eilir/Downloads/android-ndk-r16-beta1/ndk-build",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLOCATION_MK=src/main/jni/Application.mk',
"V=1",
"APP_STL=stlport_shared",
"APP_CPPFLAGS = -fexceptions"
}......
cpp代码:
#include <string>
#include <jni.h>
#include <cstdlib>
#include <unistd.h>
//#include <sys/types.h>
//#include <elf.h>
//#include <sys/mman.h>
//#include <Android/log.h>
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jstring JNICALL
Java_com_BTC_ui_JNI_getString(JNIEnv *env, jobject instance)
{
string a = "ggg";
return env->NewStringUTF(a.c_str());
}
JNIEXPORT jint JNICALL
Java_com_BTC_ui_JNI_plus(JNIEnv *env, jobject instance, jint a, jint b)
{
return a+b;
}
#ifdef __cplusplus
}
#endif
Android.mk内容:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := addcomputer
LOCAL_SRC_FILES := addcomputer.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Application.mk内容:
APP_ABI:=armeabi-v7a, arm64-v8a, x86, x86_64
#指定C++运行库为微型系统C++运行库
#APP_STL:=stlport_shared
APP_STL:=gnustl_shared
#APP_STL:=gnustl_static
#APP_STL:=system
#要为整个应用启用异常处理支持
APP_CPPFLAGS += -fexceptions
我的gradle构建命令行:
/path/to/myndk/android-ndk-r16-beta1/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -MMD -MP -MF build/intermediates/ndk/obj/local/arm64-v8a/objs/addcomputer/addcomputer.o.d -gcc-toolchain /path/to/myndk/android-ndk-r16-beta1/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 -target aarch64-none-linux-android -ffunction-sections -funwind-tables -fstack-protector-strong -fpic -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes -g -fno-exceptions -fno-rtti -O2 -DNDEBUG -I/path/to/myndk/android-ndk-r16-beta1/sources/cxx-stl/stlport/stlport -I/path/to/myndk/android-ndk-r16-beta1/sources/cxx-stl//gabi++/include -Isrc/main/jni -fexceptions -DANDROID -D__ANDROID_API__=21 -Wa,--noexecstack -Wformat -Werror=format-security -frtti --sysroot /path/to/myndk/android-ndk-r16-beta1/sysroot -isystem /path/to/myndk/android-ndk-r16-beta1/sysroot/usr/include/aarch64-linux-android -c src/main/jni/addcomputer.cpp -o build/intermediates/ndk/obj/local/arm64-v8a/objs/addcomputer/addcomputer.o
我发现我的Application.mk可以在build.gradle中覆盖,但即使我配置APP_STL = stlport_shared,我也可以在路径“/ path / to / myndk / android-ndk-r16-中找到”string“文件beta1 / sources / cxx-stl / stlport / stlport“,我无法理解为什么编译器找不到”string“文件。有人可以帮帮我吗?
追加: 我刚刚发现gradle任务是多种多样的。如下所示:
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugResValues
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl
:app:processDebugAndroidTestManifest
:app:compileDebugAndroidTestRenderscript
:app:generateDebugAndroidTestBuildConfig
:app:generateDebugAndroidTestResValues
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:incrementalDebugJavaCompilationSafeguard
:app:javaPreCompileDebug
:app:ndkBuild
查看上面的最后一行:app:ndkBuild ,该任务运行良好,错误发生在下面的其他任务中:
:app:compileDebugNdk
Warning: Deprecated NDK integration enabled by useDeprecatedNdk flag in gradle.properties will be removed from Android Gradle plugin soon.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
https://developer.android.com/studio/build/experimental-plugin.html.
Android NDK: WARNING: Unsupported source file extensions in /path/to/project/app/build/intermediates/ndk/debug/Android.mk for module addcomputer
Android NDK: /path/to/project/app/src/main/jni/Android.mk /path/to/project/app/src/main/jni/Application.mk
Android NDK: WARNING: Unsupported source file extensions in /path/to/project/app/build/intermediates/ndk/debug/Android.mk for module addcomputer
Android NDK: /path/to/project/app/src/main/jni/Android.mk /path/to/project/app/src/main/jni/Application.mk
Android NDK: WARNING: Unsupported source file extensions in /path/to/project/app/build/intermediates/ndk/debug/Android.mk for module addcomputer
Android NDK: /path/to/project/app/src/main/jni/Android.mk /path/to/project/app/src/main/jni/Application.mk
Android NDK: WARNING: Unsupported source file extensions in /path/to/project/app/build/intermediates/ndk/debug/Android.mk for module addcomputer
Android NDK: /path/to/project/app/src/main/jni/Android.mk /path/to/project/app/src/main/jni/Application.mk
[armeabi-v7a] Compile++ thumb: addcomputer <= addcomputer.cpp
[armeabi-v7a] Compile++ thumb: addcomputer <= Socket.cpp
/path/to/project/app/src/main/jni/Socket.cpp:9:10: fatal error: 'iostream.h' file not found
#include <iostream.h>
^~~~~~~~~~~~
1 error generated.
:app:compileDebugNdk FAILED
任务 compileDebugNdk 而不是 ndkBuild 引发的错误。
答案 0 :(得分:0)
我已经将IDE更新到Android工作室3.1.2和gradle版本4.4,并将ndk-guild更改为CMake,将Android.mk和Application.mk更改为CMakeLists.txt,工作数小时后,我终于得到了正确的状态。找不到错误'string'文件。现在一切正常。谢谢大家!