Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution failed for task ':app:externalNativeBuildDebug' #310

Closed
falbertp opened this issue Oct 20, 2016 · 17 comments
Closed

Execution failed for task ':app:externalNativeBuildDebug' #310

falbertp opened this issue Oct 20, 2016 · 17 comments

Comments

@falbertp
Copy link

falbertp commented Oct 20, 2016

Hi, I'm learning to use NDK,

  1. I've created a new project
  2. check the c++ support
  3. use c++ 11 option
    The generated project run fine and show the "hello from c++".
    Then I tried to change the code at native-lib.cpp from sample code of bitmap plasma:
#include <jni.h>
#include <string>
#include <android/bitmap.h>
#include <android/log.h>
#define  LOG_TAG    "cardscanner"
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

extern "C"
jstring
Java_com_aprisma_cardscanner_cardscanner_MainActivity_stringFromJNI( JNIEnv *env,  jobject /* this */, jobject bitmap) {
    AndroidBitmapInfo  info;
    void*              pixels;
    int                ret;
    static int         init;
    if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return NULL;
    }
    if (info.format != ANDROID_BITMAP_FORMAT_RGB_565) {
        LOGE("Bitmap format is not RGB_565 !");
        return NULL;
    }
    if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }
    std::string hello = "Hello sjfosfosjf";
    return env->NewStringUTF(hello.c_str());
}

and change MainActivity.java :

package com.aprisma.cardscanner.cardscanner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.graphics.Bitmap;

public class MainActivity extends AppCompatActivity {
    static {System.loadLibrary("native-lib");}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI(null));
    }
    public native String stringFromJNI(Bitmap  bitmap);
}

Got error:

Error:FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing 'C:\Users\admin\AppData\Local\Android\sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build D:\Frank\workspace\CardScanner\app\.externalNativeBuild\cmake\debug\mips64 --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ..\obj\mips64\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\admin\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  -target mips64el-none-linux-android -gcc-toolchain C:/Users/admin/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/admin/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -std=c++11 -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\obj\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  -llog -lm "C:/Users/admin/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ."
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function `Java_com_aprisma_cardscanner_cardscanner_MainActivity_stringFromJNI':
  D:\Frank\workspace\CardScanner\app\src\main\cpp/native-lib.cpp:19: undefined reference to `AndroidBitmap_getInfo'
  D:\Frank\workspace\CardScanner\app\src\main\cpp/native-lib.cpp:29: undefined reference to `AndroidBitmap_lockPixels'
  clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.

please help, thank you

@ggfan
Copy link
Contributor

ggfan commented Oct 20, 2016

check bitmap-plasma's CMakeLists.txt file, the lib needed is jniGraphics, add it into your CMakeLists.txt's target_link_libraries() would be fine.

you might check for null pointer: you did send down a null pointer from Java to native code right now.

Also for the generated prototype, you need add two modifiers to it, otherwise release build would be in trouble ( you native code would optimized away ):
extern "C" JNIEXPORT jstring JNICALL Java_.....()

later when you add more functions into Java side, right click to generate corresponding native prototype, the generated prototype need to add:
extern "C"

to all generated native code prototypes because you are using CPP file, not C file. Just bring it up ahead of the time, you will see it. Later android studio versions ( NOT the next release one ) should fix them someday.

@falbertp
Copy link
Author

@ggfan thank you so much 💯 my program now run again.
All your suggestion and solution is very important for me it help me learn several tricks to do NDK correctly, very quick and right to the point. 👍 👍 👍

eagerly waiting the generate extern soon :)

Thank you and best regards

@Ni30patel
Copy link

Ni30patel commented Jun 29, 2018

I am developing NDK Sample.

I have created fresh Project using android studio with C++ support also check for "-frtti -fexceptions". It is compile properly but while running it gives below error which makes build fail.

Another thing is that if I change file extendsions from .CPP to .C it will works properly. I don't know why it gives error while I change file extension.

native-lib.cpp

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring

JNICALL
Java_com_mastek_ndksample_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

Build.gradle

` apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.sample.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'
}
`

Got below error
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.o FAILED: 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.

for more you can check my question on stakoverflow from here

please help, Thanks in advance.

@ggfan
Copy link
Contributor

ggfan commented Jun 29, 2018

upload your code to somewhere please, including everything?. between c and C++ is the 'extern "C"' there for in your function prototype. also what version is your android studio ( anyhow use the latest one: even the older one has bug, it is not going to be fixed in those old versions )?

@ggfan
Copy link
Contributor

ggfan commented Jun 29, 2018

the latest Android Studio version, 3.1.2 seems ok with it. probably you could try menu:
Build > Refresh Linked C++ project
Build > Clean Project
Run > run app

or remove build and .externalNativeBuild manually and re-try. It is a guess, nothing I could see through

@Ni30patel
Copy link

Ni30patel commented Jul 2, 2018

@ggfan Thanks for quick reply. As per your suggestion I have updated studio version to 3.1.3 and also follow steps given by you but still its not able to run it successfully.

Even, I have tried to create .SO file from command Line by given set of commands

  1. D:\TestNDk\native-lib\jni>set PATH=$PATH:D:\android-ndk-r17b
  2. D:\TestNDk\native-lib\jni>set SYSROOT=D:\android-ndk-r17b\platforms\android-15\arch-arm
    3 D:\TestNDk\native-lib\jni>D:\android-ndk-r17b\build\ndk-build

I find these from here

Do you think that Any chance of Windows version effect it? why I am asking is because It is works fine on my home laptop windows 7 version using Android Studio and Command Line but It is not working on Windows 10 which I have in my office.

I got same error while create so file from command line. It says CMAKE is not able to generate any obj file native-lib.o as below.
sofile_error

I can see [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o error in my error msg.

Can you look into these and give me proper solution for these if possible.

@ggfan
Copy link
Contributor

ggfan commented Jul 2, 2018

I tried on my windows 10, it is ok with it. Got to be with something else: you do not have cmake installed on your own system, do you? Please upload your whole project to somewhere, I will download it to try. something to do with environment setting, could not see it right now

BTW, the CXX_FLAGS is to be set inside CMakeLists.txt, not on command line; standard could be -std=c++11 or c++14.

@Ni30patel
Copy link

@ggfan Yes, I have installed cmake its already there within my sdk folder along with lldb. I am just building sample project with C++ support nothing else still for you I have uploaded my code here.

I found something in app/.externalNativeBuild/cmake/debug/arm64-v8a/cmake_build_output.txt Detecting CXX compiler ABI info - failed. Does it mean its not able to find Compiler for C++ file becuase I see in that file it gets compile for C file.

Do I need to have some changes in toolchain file? Is android studio toolchain and cmake chain files are different? Some people say clang is still not proper so I need to compile it with GCC. Is that true?

@ggfan
Copy link
Contributor

ggfan commented Jul 3, 2018

this problem is more related to the build machine setting, unlikely to be anything related to compiler type ( gcc or clang ). Unfortunately I could not spot it: your program could build on my system of windows 10.

I am not sure how to debug it either: could only guess. If you go to command line ( NOT under your sdk's cmake directory, type cmake.exe, you could not find the program, correct?) If you do find your own cmake.exe ( not sdk's ), remove it from your PATH( you could add it later after you get this simple one working )

Then, go to your project directory, remove your local.properties file;

- find file gradlew.bat; run it just as "gradlew.bat"
- if you are happy there, good -- done and means your system is good.
- otherwise
   --- you would see complaint about ANDROID_HOME env variable
        set it to your SDK's directory in window advanced system properties}
  --- re-open a dos box, see your ANDROID_HOME is really set up with "set"
  --- then re-run "gradlew.bat clean assembleDebug"
  --- it would succeed (great)
  --- if it complaint about ANDROID_NDK_HOME, set to your NDK dir ( your-sdk/ndk-bundle )
  --- make sure both ANDROID_HOME and ANDROID_NDK_HOME are set correctly
  --- re-run gradlew.bat clean assembleDebug

if this could not get you through, just uninstall Android Studio, SDK and do it over from the fresh ( it saves time that way ): the re-install Android Studio, let it manage all SDK and NDK download etc.

if you have lots of env variables set up for android previously, remove them all before you try. do not use external CMake ( your separate installation yet ). something simple, I just do not know what it is.

@Ni30patel
Copy link

okay. Thanks for support. I will check and let you know the result. Hope it works ,if not then I will come back to you..;)

@Ni30patel
Copy link

As per your reply I have tried below things but still getting same error.

  1. Clean all the env varibales related to Android and also others.
  2. Download android studio 3.1.3 from developer site also new SDK & NDK.
  3. created fresh sample project as I have new setup and run but unfortunately its failed again.
  4. Run gradlew.bat under my project directory its successful.
  5. gradlew.bat iD which is fail with same error as studio.

Even I tried to build it manually by sdk's cmake but it gives me same error. I have not installed any external CMKAE.
cmake_error
Now is there anyway I can run it successfully?

@ggfan
Copy link
Contributor

ggfan commented Jul 5, 2018

the build command is

gradlew.bat

that what android does. gradle will call cmake. If you have to use cmake directly and not using gradle, you would need to look at the command line parameters that gradle uses, it is in file cmake_build_command.txt file in your %project-dir%/app/.externalNativeBuild/cmake/...

hope that helps.

@Ni30patel
Copy link

Yeah. I know build command. I don't want to use cmake directly that was just testing whether its working directly or not.I taught if gradle is not able to build it lets try to build using cmake.exe nothing else.
I have used gradlew.bat to build and get BUILD SUCCESSFUL.

While I Run(app) on my device or on emulator it gets failed with above error and that is build command fail. I don't know what is the problem? I have deleted everything and re-installed but android studio gives same error.
Is there any proper solution to resolve this?

@ggfan
Copy link
Contributor

ggfan commented Jul 6, 2018

while once gradlew could build, your environment is not of a problem. you could do the full cycle to the end on the command line:

  • gradlew.bat assembleDebug
  • adb install -r app/build/outputs/.../your-apk.apk
  • adb shell am -n your-package/your-activity ( or you go to your device and start it there )
    ( use for \ for windows probably )

that should clear up any doubt of your system. If that works and your Android Studio IDE does not work, I would check for android studio's SDK settings and make sure it is pointing to the same one you are using for command line.

you experience a unique problem unfortunately I could not see what the problem is.

@Ni30patel
Copy link

I think simple gradlew.bat make build successful but gradlew.bat clean assembleDebug gives same error. I am not able to complete commands given by you because only build gradle build is successful after that going to Run(app) it gives me Build command fail above error from android studio as well as command line. Will gradlew.bat generate app.apk? No right? So how can I perfrom adb install -r app/build/outputs/.../your-apk.apk. If apk is generated then my problem get resolve but that is the main thing where I am facing problem.

SDK is same while I am doing gradlew.bat from command line.

Even I am also thinking that this is a unique problem for me after lot of search but I need to have solve these or try any other simple way but that I don't know to generate SO file from c++ file. Do you know any other way guide me so I can try and generate SO file?

@ggfan
Copy link
Contributor

ggfan commented Jul 6, 2018

Oh, ok. if gradlew.bat assembleDebug, there is no way you have apk. It would be odd to generate so file and zip it you manually for apk for this thing: gradle is the way to go.

@Ni30patel
Copy link

Thanks you so much Gerry for your help. After lot of try and error we are able to solve. To resolve problem I have follow below steps.

  1. go to project directory.
  2. deleted .gradle ,app/.externalNativeBuild and app/build.
  3. changed local.properties ndk path to external ndk-r16b.
  4. Run through Android studio.
  5. Finally apk is generate...:) Happy

@ggfan I appreciate all the effort given buy you. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants