Android Studio CMake Error: Build Command failed
Asked Answered
I

12

18

I'm running into an Error when I open a new project in Android Studio from the Code Samples (Hello JIN). When the Project is opened the following:

Build command failed.
Error while executing process /opt/android-sdk/cmake/3.6.4111459/bin/cmake with arguments {-H/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/src/main/cpp -B/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a -GAndroid Gradle - Ninja -DANDROID_ABI=arm64-v8a -DANDROID_NDK=/opt/android-sdk/ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/build/intermediates/cmake/arm8/release/obj/arm64-v8a -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=/opt/android-sdk/cmake/3.6.4111459/bin/ninja -DCMAKE_TOOLCHAIN_FILE=/opt/android-sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-23 -DANDROID_TOOLCHAIN=clang}
-- Check for working C compiler: /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- broken
-- Configuring incomplete, errors occurred!
See also "/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeOutput.log".
See also "/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeError.log".
CMake Error at /opt/android-sdk/cmake/3.6.4111459/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler
  "/opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
  is not able to compile a simple test program.
  It fails with the following output:
   Change Dir: /home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeTmp
  Run Build Command:"/opt/android-sdk/cmake/3.6.4111459/bin/ninja"
  "cmTC_0053d"
  [1/2] Building C object CMakeFiles/cmTC_0053d.dir/testCCompiler.c.o
  FAILED:
  /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
  --target=aarch64-none-linux-android
  --gcc-toolchain=/opt/android-sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64
  --sysroot=/opt/android-sdk/ndk-bundle/sysroot -isystem
  /opt/android-sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android
  -D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables
  -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat
  -Werror=format-security -fPIE -o
  CMakeFiles/cmTC_0053d.dir/testCCompiler.c.o -c
  /home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeTmp/testCCompiler.c
  /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang:
  error while loading shared libraries: libncurses.so.5: cannot open shared
  object file: No such file or directory
  ninja: build stopped: subcommand failed.
  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt

This Error drops mutliple times in the IDE

I'm using Arch-Linux 64x

Cmake.txt:

    cmake_minimum_required(VERSION 3.4.1)

add_library(hello-jni SHARED
            hello-jni.c)

# Include libraries needed for hello-jni lib
target_link_libraries(hello-jni
                      android
                      log)
Ics answered 20/7, 2017 at 15:25 Comment(5)
I had this same issue, could you please post your CMakeLists.txt?Munn
ok I edited the PostIcs
What version of the NDK are you using? Some versions have limited support for the clang compiler.Munn
I use 15.1.4119093Ics
@Munn I am facing same issue. can you please help me on my question.#51100611.Sailcloth
I
9

@rpurohit was nearly right, Clang isn't working properly. But to change the Compiler you need to change build.gradle, in my build.gradle it was Line 12:

apply plugin: 'com.android.application'

1 android {
2    compileSdkVersion 25
3    buildToolsVersion '25.0.2'
4    defaultConfig {
5       applicationId 'com.example.hellojni'
6       minSdkVersion 23
7       targetSdkVersion 25
8       versionCode 1
9       versionName "1.0"
10      externalNativeBuild {
11          cmake {
12              arguments '-DANDROID_TOOLCHAIN=clang' --> gcc
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
    productFlavors {
        arm7 {
            // in the future, ndk.abiFilter might also work
            ndk {
                abiFilter 'armeabi-v7a'
            }
        }
        arm8 {
            ndk {
                abiFilters 'arm64-v8a'
            }
        }
        arm {
            ndk {
                abiFilter 'armeabi'
            }
        }
        x86 {
            ndk {
                abiFilter 'x86'
            }
        }
        x86_64 {
            ndk {
                abiFilter 'x86_64'
            }
        }
        mips {
            ndk {
                abiFilters 'mips', 'mips64'
            }
        }
        universal {
            ndk {
                abiFilters 'mips', 'mips64', 'x86', 'x86_64'
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
}
Ics answered 22/7, 2017 at 10:2 Comment(2)
In my case that gives "Could not get unknown property 'gcc' for object of type com.android.build.gradle.internal.dsl.ExternalNativeCmakeOptions." I can't believe how badly Google has gotten this.Semiotics
You are a life saver!Mockingbird
T
38

Build -> Refresh Linked C++ Projects resolved this error for me.

Tweak answered 10/5, 2018 at 20:32 Comment(3)
This is a life-saver. It also worked for me. My advice is to try this first, then other things.Unilocular
It didn't work for first time. But after downgrading NDK from version 17 to 15, downgrading kotlin 1.2.41 to 1.2.31 and clicking this option "Build -> Refresh Linked C++ Projects" works for me.Worldlywise
Thanks a lot! is the really life-saver. Worked for me in Windows 10.Forras
D
19

I found this Solution Online and this worked, however there was no explanation on how it worked: Remove the following block of code from you build.gradle file:

externalNativeBuild {
    cmake {
        path "src/main/cpp/CMakeLists.txt"
    }
}
Digiacomo answered 28/6, 2019 at 6:54 Comment(4)
apparently this thing seems to work ok. Not sure why...Kiefer
Thanks alot @Mohd Zaid Im searching for this two dyas now its working but I don't why 😆Debouchment
This isn't the solution, the project will run successfully but will not build the desired library, in my case it is wireguard, so after removing that part getting. dlopen failed: library "libwg-go.so" not foundOverrun
Your comment saved my life. For unknown reason, only this came to work. Thank you.Vachell
I
9

@rpurohit was nearly right, Clang isn't working properly. But to change the Compiler you need to change build.gradle, in my build.gradle it was Line 12:

apply plugin: 'com.android.application'

1 android {
2    compileSdkVersion 25
3    buildToolsVersion '25.0.2'
4    defaultConfig {
5       applicationId 'com.example.hellojni'
6       minSdkVersion 23
7       targetSdkVersion 25
8       versionCode 1
9       versionName "1.0"
10      externalNativeBuild {
11          cmake {
12              arguments '-DANDROID_TOOLCHAIN=clang' --> gcc
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
    productFlavors {
        arm7 {
            // in the future, ndk.abiFilter might also work
            ndk {
                abiFilter 'armeabi-v7a'
            }
        }
        arm8 {
            ndk {
                abiFilters 'arm64-v8a'
            }
        }
        arm {
            ndk {
                abiFilter 'armeabi'
            }
        }
        x86 {
            ndk {
                abiFilter 'x86'
            }
        }
        x86_64 {
            ndk {
                abiFilter 'x86_64'
            }
        }
        mips {
            ndk {
                abiFilters 'mips', 'mips64'
            }
        }
        universal {
            ndk {
                abiFilters 'mips', 'mips64', 'x86', 'x86_64'
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
}
Ics answered 22/7, 2017 at 10:2 Comment(2)
In my case that gives "Could not get unknown property 'gcc' for object of type com.android.build.gradle.internal.dsl.ExternalNativeCmakeOptions." I can't believe how badly Google has gotten this.Semiotics
You are a life saver!Mockingbird
M
5

I would recommend using GCC instead of clang for the time being because clang still does not contain all features. You can change your compiler by doing the following:

set(CMAKE_C_COMPILER /path-to-ndk/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin/aarch64-linux-android-gcc)

However, the darwin-x86_64 directory only exists if you are on a Mac. If you are on another platform, just use the directory that exists under the prebuilt folder.

Munn answered 20/7, 2017 at 16:0 Comment(4)
Try running it at the beginning of CMakeLists.txt.Munn
That dosen't worked for me The Error is still the Same so the command didn't worked :/Ics
@Biskit1943 How to can I change compiler from clang to GCC? can you explain proper steps becuase I am facing same error?Sailcloth
@Munn I think this answer is outdated nowCrucifix
F
1

I encountered this problem because I set the wrong path of native-lib.cpp. After changing

add_library(native-lib SHARED native-lib.cpp)

to

add_library(native-lib SHARED src/main/jni/native-lib.cpp)

it worked again.

By the way, this is part of my project's structure.

CMakeLists.txt
src
 |__main
      |___jni
           |___native-lib.cpp
Footloose answered 17/6, 2019 at 14:30 Comment(0)
A
0

In case previous answer doesn't work for you, as it happened to me, try to fix permissions in the bin folder of cmake and for ndk. In my case: C:\android-sdk\cmake\3.6.4111459\bin and c:\android-sdk\ndk-bundle

In my case Users group did had no permissions so Android Studio wasn't able to run cmake. Make sure it has Read and execution permissions.

Avar answered 10/10, 2017 at 8:51 Comment(0)
T
0

This error sometimes occurs when you upgrade gradle or other dependencies. a simple solution is Build > "Refresh linked C++ project" and after that rebuilding your project. everything goes right

Tabor answered 3/6, 2018 at 7:3 Comment(0)
M
0

I tried the solutions provided above but no luck. Then I changed the path of OpenCV_DIR which was set in the CMakeLists.txt file and it worked. My project was not pointing to the right path, which was causing the error. Make sure you have provided the right path in your CMakeLists.txt file.

For example :

set(OpenCV_DIR "...../OpenCV_Android/install/sdk/native/jni/abi-arm64-v8a")
Maurili answered 25/2, 2019 at 15:36 Comment(0)
O
0

In my case, I have upgraded my android project to api 33, so I have matched my NDK library compiled SDK version with the project compile SDK version from the File->Project Structure->modules. Also selected the NDK version, it was unselected before and it worked.

Overrun answered 2/3, 2023 at 23:10 Comment(0)
G
0

go to project structure then select you ndk vertion

Geriatric answered 22/11, 2023 at 11:23 Comment(0)
A
0
if you have tried all the possible solutions and still facing CXX[1429] or CXX[1405] for cMake

Make Sure to following C++ extensions in visual studio code
C/C++
C/C++ Theme 
C/C++ Extensions Pack
CMake
CMake Tools
Gradle for Java

Main Thing, install "Visual C++ Redistributable for Visual Studio 2015" from the following link:
[https://www.microsoft.com/en-us/download/confirmation.aspx?id=48145&6B49FDFB-8E5B-4B07-BC31-15695C5A2143=1][1]


Restart your vs code or your System
Azobenzene answered 3/12, 2023 at 18:13 Comment(1)
Please format your response for clarity, the entire response should not be in a code block if it isn't warrantedBondon
K
-1

I find this way can work:

build success here

not need delete below:

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

this is useful .and your project just less the ninja file .

you can fllow this way can build successful!

here is the result:

Make was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.

you should install ninja

  1. download ninja here ninja download file
  2. get the ninja file
  3. copy the ninja file to the /usrlocal/bin
  4. build again .the project run successfull!!
Kola answered 2/6, 2020 at 12:57 Comment(1)
Can you explain exactly how this solves the above problem?Digiacomo

© 2022 - 2024 — McMap. All rights reserved.