How can I resolve Android NDK build command faild?
Asked Answered
S

2

3

I am developing a simple NDK sample example using the Android NDK in Android Studio. While running my app studio, it 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 searched lot, but not able to find proper answer. That is why I am asking here. I have also uninstall all the components and re-install in my studio instance , but still the error is there.

Another thing: If I change file extension to .C from .CPP whole project gets complied and even runs properly. I don't know why it’s not working for a .CPP file.

Components I am using.

Android Studio - 3.1.3

Gradle - 3.1.0

CMake - 3.6.4111459

Android NDK - 17.1.4828580

lldb - 3.1

File 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());
}

File 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'
}

How can I to solve this problem?

Saleable answered 29/6, 2018 at 10:54 Comment(3)
Please, fix missed newlines in your code (e.g. one between #include statements) and in the error messages (e.g. 1 error generated. should be on a separate line).Andry
@Andry Sorry I dont know how to do that.If you know you can edit my question.Thanks.Saleable
Missed newlines are result of incorrect pasting the text into the question post. I cannot fix them by formatting. You need to copy-paste the text again from your sources.Andry
Q
5

NDK build fails miserably when the path to project files contains spaces. To fix your build, copy the project to path without spaces.

Quadrant answered 3/7, 2018 at 19:49 Comment(6)
I have change project path to without spaces but still giving same error. Even I tried on another PC without space path same error build command fail.Saleable
Does it still fail with error reading 'D:\Android Studio\Workspace\NDKSample\app\src\main\cpp\native-lib.cpp"?Quadrant
yes. It still fail with same error. I think its coming becuase of CMAKE which ndk is using to compile c++ file but I not sure. Do you have any idea?Saleable
If the error shows a path that does not exist, this is probably a result of some intermediate files that were not cleaned up after moving the project. Delete the .extarnalNativeBuild directory, and run build again.Quadrant
Error does not says path not exist but still I have deleted .extarnalNativeBuild and build directory also but I got same error. Even I have deleted all setup including sdk,ndk,android studio along with ENV variables and re-install them but still I am getting same error. I don't know what problem is there.Saleable
My issue fixed by removing space in folder name. ThanksCheltenham
S
1

After a lot of trials and help from the GitHub Google guy, I am able to solve it. To resolve the problem I have the following below steps.

  1. went to the project directory.

  2. deleted .gradle ,app/.externalNativeBuild and app/build.

  3. changed the local.properties NDK path to the external ndk-r16b.

  4. Ran it through Android Studio.

  5. Finally an APK file was generated.

Saleable answered 9/7, 2018 at 9:58 Comment(1)
I have ndk path to C\:\\Users\\bhaskar\\AppData\\Local\\Android\\sdk\\ndk-bundle so what to do in step 3 that you have mentioned? Can you please explain?Outmoded

© 2022 - 2024 — McMap. All rights reserved.