问题描述
我正在使用android studio中的android ndk开发简单的ndk示例示例.在运行我的App Studio时,显示以下错误.
I am developing simple ndk sample example using android ndk in android studio. While running my app studio shows below errors.
Build command failed.
Error while executing process D:\Sdk\cmake\3.6.4111459\bin\cmake.exe with
arguments {--build D:\Android Studio\Workspace\NDKSample\app\.externalNativeBuild\cmake\debug\arm64-v8a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.oFAILED: D:\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android --gcc-toolchain=D:/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=D:/Sdk/ndk-bundle/sysroot -Dnative_lib_EXPORTS -isystem D:/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem D:/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/include -isystem D:/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -isystem D:/Sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles\native-lib.dir\src\main\cpp\native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c "D:\Android Studio\Workspace\NDKSample\app\src\main\cpp\native-lib.cpp"
error: error reading 'D:\Android Studio\Workspace\NDKSample\app\src\main\cpp\native-lib.cpp'
1 error generated.ninja: build stopped: subcommand failed.
我搜索了很多东西,但找不到正确的答案,这就是为什么我要在这里问问题.我还卸载了所有组件并重新安装在我的工作室上,仍然有错误.
I have search lot but not able to find proper answer that why I am asking here. I have also unintsall all the components and re-install on my studio still error is there.
另一件事,如果我将文件扩展名从.CPP更改为.C,则整个项目将被编译,甚至可以正常运行.我不知道为什么它不能用于.CPP文件.
Another thing If I change file extension to .C from .CPP whole project gets complied and even runs properly. I don't know why Its not working for .CPP file.
我正在使用的组件.
Android Stuido-3.1.3
Android Stuido - 3.1.3
等级-3.1.0
cmake-3.6.4111459
cmake - 3.6.4111459
Android ndk-17.1.4828580
Android ndk - 17.1.4828580
lldb -3.1
lldb -3.1
native-lib.cpp
native-lib.cpp
#include <jni.h>#include <string>extern "C" JNIEXPORT jstringJNICALLJava_com_mastek_ndksample_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());}
build.gradle
build.gradle
apply plugin: 'com.android.application'android { compileSdkVersion 27
defaultConfig {
applicationId "com.example.ndksample"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}}dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}
任何人都知道,请帮助我解决此问题.预先感谢.
Anyone know please help me to solve this problem. Thanks in advance.
推荐答案
经过github Google家伙的大量试验和帮助,我能够解决.要解决问题,请按照以下步骤操作.
After lot of trials and help from github google guy I am able to solve. To resolve problem I have follow below steps.
1.go进入项目目录.
1.go to project directory.
2.deleted .gradle,app/.externalNativeBuild和app/build.
2.deleted .gradle ,app/.externalNativeBuild and app/build.
3.将local.properties ndk路径更改为外部ndk-r16b.
3.changed local.properties ndk path to external ndk-r16b.
4.在Android Studio中运行.
4.Run through Android studio.
5.最后生成apk.
5.Finally apk is generate.
这篇关于如何解决Android ndk build命令失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!