Nitish Patel
Nitish Patel

Reputation: 1026

How can I resolve Android NDK build command faild?

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?

Upvotes: 3

Views: 5905

Answers (2)

Nitish Patel
Nitish Patel

Reputation: 1026

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.

Upvotes: 1

Alex Cohn
Alex Cohn

Reputation: 57203

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

Upvotes: 5

Related Questions