Android Studio ndk example build errors - android

I am new to using an ndk and was trying to use some of the sample projects that came in the android ndk10d folder. For example the hello-jni sample project. I keep getting this error:
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\Jonathon\android-ndk-r10d\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\Jonathon\AndroidStudioProjects\hello-jni2\app\build\intermediates\ndk\debug\Android.mk APP_PLATFORM=android-21 NDK_OUT=C:\Users\Jonathon\AndroidStudioProjects\hello-jni2\app\build\intermediates\ndk\debug\obj NDK_LIBS_OUT=C:\Users\Jonathon\AndroidStudioProjects\hello-jni2\app\build\intermediates\ndk\debug\lib APP_ABI=all
Error Code: 2 Output:
make.exe: *** No rule to make target `C:\Users\Jonathon\AndroidStudioProjects\hello-jni2\app\build\intermediates\ndk\debug\obj/local/arm64-v8a/objs/hello-jni/C_\Users\Jonathon\AndroidStudioProjects\hello-jni2\app\src\main\jni', needed by `C:\Users\Jonathon\AndroidStudioProjects\hello-jni2\app\build\intermediates\ndk\debug\obj/local/arm64-v8a/objs/hello-jni/C_\Users\Jonathon\AndroidStudioProjects\hello-jni2\app\src\main\jni\hello-jni.o'. Stop.
I have checked and the file: ndk-build.cmd is indeed in the location specified. When I try to open it command prompt says that it cannot find the application project directory. Please define the NDK_PROJECT_PATH variable to it
I re-installed and updated my whole environment from 0.8.9 to android studio 0.9.9 and now 1.01 and I get the same error. It seems it can't find the file or file directory and that must mean my configuration isn't right but I can't determine what to do to fix it.
local.properties
sdk.dir=C\:\\Users\\Jonathon\\AppData\\Local\\Android\\sdk
ndk.dir=C\:\\Users\\Jonathon\\android-ndk-r10d
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)

Check out this video for building simple NDK projects with Android studio
https://www.youtube.com/watch?v=kFtxo7rr2HQ&list=UUkbLy9aj5IBXfS6WqLy5Qmw
Alternatively you can also download Intel Native Developer Experience tool that comes with the NDK plugin for Android Studio

You can remove the Android.mk file: it will be ignored as a new one is generated on the fly from your gradle configuration (build.gradle file). You can get more information on this from my article on NDK support in Android Studio.
The bug you're experiencing is this one: https://code.google.com/p/android/issues/detail?id=66937&
A simple workaround is to create a empty .c file next to hello-jni.c.

If you have only one .c (or .cpp) file add a dummy .c (or .cpp) file and it works.

Related

Android studio - ndk_build_command failing on Clean task

I recently checked out a project originally built under Windows (using JNI) in my Studio on Ubuntu
When I run a clean task, I see an error when it tries to execute what seems to be a Windows ndk-clean command
org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'C:UsersTishuAppDataLocalAndroidandroid-ndk-r8e-windows-x86_64android-ndk-r8endk-build.cmd
I am not sure what triggers this command but it happens on this task: 'externalNativeBuildCleanDebug'
I have searched in my project for references to 'AppData' and found few in this file: project/.externalNativeBuilds/ndkbuild/debug/mips/ndkBuild_build_commands.txt
This file contains windows related information, starting with
Executable : C:\Users\Tishu\AppData\Local\Android\android-ndk-r8e-windows-x86_64\android-ndk-r8e\ndk-build.cmd
Problem is I cannot find this file in Files or in Studio. The .externalNativeBuilds folder doesn't show. I have this is my build.gradle
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
And the Android.mk file contains only
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ALLOW_UNDEFINED_SYMBOLS=false
LOCAL_MODULE := jpgexif
LOCAL_SRC_FILES := JpgExif.c
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
How do I disable this step, or update it to be relevant for my platform?
It sounds to me like either of these things have happened:
Someone checked the .externalNativeBuild directory into the repo, which they shouldn't have done. The build tools should generate that directory for you locally. If you can't see the directory in the file explorer, press Ctrl+H, or whatever the command is to show hidden files. Then delete the directory and trigger a gradle sync. Or just remove it from a terminal window.
Someone checked the local.properties file into the repo, and in it they set the ndk.dir property to a Windows path. Again, this file should probably not be part of your shared repository. If this is what happened you might be able to fix it by editing the file and specifying the correct NDK path for your machine, and then triggering a gradle sync.

Android NDK and .so.X extensions

I'm trying to use the Grantlee library in an Android NDK app.
I can compile Grantlee just fine, and I get libGrantlee_Templates.so, libGrantlee_Templates.so.5, and libGrantlee_Templates.so.5.0.0.
Of these three, I can only install the first one in my Android.mk:
include $(CLEAR_VARS)
LOCAL_MODULE := libgrantlee
LOCAL_SRC_FILES := $(GRANTLEE_PATH)/lib/libGrantlee_Templates.so
include $(PREBUILT_SHARED_LIBRARY)
Because if I try to install either of the other two, then I get:
Android NDK: ERROR:Android.mk:grantlee: LOCAL_SRC_FILES should point to a file ending with ".so"
Android NDK: The following file is unsupported: libGrantlee_Templates.so.5
But then, like a cruel joke, the .so.5 that I can't install is exactly the file that it looks for at runtime:
E/AndroidRuntime(14439): java.lang.UnsatisfiedLinkError: dlopen failed: could not load library "libGrantlee_Templates.so.5"; caused by library "libGrantlee_Templates.so.5" not found
How do I solve this?
When you build a native shared library with NDK standalone toolchain, you must change its make files to get rid of .version suffixes. See for example how this is done for ffmpeg.
I had the same problem when using an external library in my project.
You can also add -avoid-version to the libtool command. This trick saved my day...
If the library is built with an autotools/libtool based build system, update the bundled libtool to libtool 2.4.3 or newer. Those versions know how to deal with these details about android shared libraries, so if you build it with --host=arm-linux-androideabi or similar, it won't add any version numbers to the libraries.
To upgrade the libtool version within a source package, run e.g. autogen.sh or a similar script within the source package, or autoreconf -fi, assuming that you've got a new enough version of libtool installed in the surrounding system.

andengine compileReleaseNdk error

I want to use andengine in my android studio project but I have ndk error while building.
Error:Execution failed for task ':andEngine:compileReleaseNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
D:\Android\android-ndk-r9d\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=D:\Android\workspace\simpleclock\simple_clock_as\andEngine\build\intermediates\ndk\release\Android.mk APP_PLATFORM=android-19 NDK_OUT=D:\Android\workspace\simpleclock\simple_clock_as\andEngine\build\intermediates\ndk\release\obj NDK_LIBS_OUT=D:\Android\workspace\simpleclock\simple_clock_as\andEngine\build\intermediates\ndk\release\lib APP_ABI=all
Error Code:
2
Output:
D:/Android/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\Android\workspace\simpleclock\simple_clock_as\andEngine\build\intermediates\ndk\release\obj/local/armeabi-v7a/objs/andengine_shared/D_\Android\workspace\simpleclock\simple_clock_as\andEngine\src\main\jni\src\GLES20Fix.o: in function Java_org_andengine_opengl_GLES20Fix_glVertexAttribPointer:GLES20Fix.c(.text.Java_org_andengine_opengl_GLES20Fix_glVertexAttribPointer+0x40): error: undefined reference to 'glVertexAttribPointer'
D:/Android/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: D:\Android\workspace\simpleclock\simple_clock_as\andEngine\build\intermediates\ndk\release\obj/local/armeabi-v7a/objs/andengine_shared/D_\Android\workspace\simpleclock\simple_clock_as\andEngine\src\main\jni\src\GLES20Fix.o: in function Java_org_andengine_opengl_GLES20Fix_glDrawElements:GLES20Fix.c(.text.Java_org_andengine_opengl_GLES20Fix_glDrawElements+0x30): error: undefined reference to 'glDrawElements'
collect2: ld returned 1 exit status
make.exe: *** [D:\Android\workspace\simpleclock\simple_clock_as\andEngine\build\intermediates\ndk\release\obj/local/armeabi-v7a/libandengine_shared.so] Error 1
I suppose I'm missing some OpenGL files?
The Android Gradle plugin's NDK task does not actually use any Android.mk file that you may have provided in your jni/ folder. This was a great source of confusion for me until I figured that out.
It generates a intermediate Android.mk file during the build, based on parameters that you have set in your Gradle build script and on the content of your jni/ folder.
You can see this for yourself by inspecting the source for its NdkCompile task at https://android.googlesource.com/platform/tools/base/+/55aebda607efcc29b8d9d5e1a99d446e320ff288/build-system/gradle/src/main/groovy/com/android/build/gradle/tasks/NdkCompile.groovy.
Note the writeMakeFile(...) method on line 126.
This is why the error you are getting from the ndk-build command that runs as part of your Gradle build references the build script APP_BUILD_SCRIPT=D:\Android\workspace\simpleclock\simple_clock_as\andEngine\build\intermediates\ndk\release\Android.mk, not something like APP_BUILD_SCRIPT=D:\Android\workspace\simpleclock\simple_clock_as\andEngine\src\main\jni\Android.mk as you might expect and want it to.
There is no way of getting the Android Gradle plugin's NDK task to use your own Android.mk file (believe me if there was I would have found it!).
You have two options for getting your NDK code compiling as part of Gradle:
Figure out the correct configuration to put in your build.gradle so that the generated Android.mk file contains the required LOCAL_LDLIBS := -lGLESv2 line and any of the other lines from https://github.com/nicolasgramlich/AndEngine/blob/GLES2/jni/Android.mk that are required.
Write a custom NDK compile task that uses the AndEnginge's Android.mk file directly. I've recently had to do this myself for an NDK source set that requires more parameters than the Android Gradle plugin currently supports passing via Gradle, so it if comes to this I can provide help.
I think in this case option 1 is open and so of course the preferable solution.
Something like this added to the android defaultConfig block should work:
android {
defaultConfig {
ndk {
moduleName "myNDKModule"
stl "stlport_shared"
ldLibs "lGLESv2"
cFlags "-Werror"
}
}
}
Unfortunately I am very much not a C/native code expert (I know practically nothing) so cannot guess whether AndEngine needs LOCAL_MODULE_FILENAME and LOCAL_EXPORT_C_INCLUDES to be set to build correctly. If it does, you'll need to go with approach 2 (at least until if/when the Android Gradle NDK task supports configuring them). Though I have just checked out the AndEngine git repo and successfully run ndk-build after having removed them from its Android.mk file, which is promising.
(I found which NDK properties can be parametrised via inspection of https://android.googlesource.com/platform/tools/base/+/55aebda607efcc29b8d9d5e1a99d446e320ff288/build-system/gradle/src/main/groovy/com/android/build/gradle/internal/dsl/NdkConfigDsl.java).
I had a similar issue, and this video https://www.youtube.com/watch?v=0-rYK2oh8oo helped me to fix the build issues. Basically, you need to download (and extract) NDK from here: http://developer.android.com/ndk/downloads/index.html and specify the NDK location in Module Settings. Also, you need to alter the andEngine's build.gradle file to include
sourceSets{
main{
jni.srcDirs = []
}
}
Android.mk actually includes the line?
https://github.com/nicolasgramlich/AndEngine/blob/GLES2/jni/Android.mk#L10
LOCAL_LDLIBS := -lGLESv2
These errors indicate it.
error: undefined reference to 'glVertexAttribPointer'
error: undefined reference to 'glDrawElements'

Android NDK Include Path In Eclipse

I have an Android NDK project that builds fine in cygwin using ndk-build.
However, I wanted to have it build in eclipse, so I installed C/C++ Development tools into my ADT version of eclipse and added native support to the project in eclipse. However, after building, I get the following error:
fatal error: timer.h: No such file or directory
In my original Android.mk file, I have the following include that lets it work in cygwin:
LOCAL_C_INCLUDES := /cygdrive/c/ADT/includes/
I tried adding a similar include path (C:\ADT\includes) to Project->Properties->C/C++ General->Paths and Symbols, but still no luck. Any suggestions?
The fix was to use windows paths
LOCAL_C_INCLUDES := C:/ADT/includes/
Note that using the following works as well (per cpu2's answer)
LOCAL_CFLAGS := -IC:/ADT/includes/
Add -I/path/to/includes to your cflags.

Android NDK make file

i am trying to execute a sample in ndk,but getting the following error.
Android NDK: There is no Android.mk under mp3solutions/jni
Android NDK: If this is intentional please define APP_BUILD_SCRIPT to point
Android NDK: to a valid NDK build script.
android-ndk-r5b/build/core/add-application.mk:1
26: *** Android NDK: Aborting... . Stop.
so please guide me how to resolve this one
are you having Android.mk in your JNI folder?, see NDK-build the files which are all listed in Android.mk, so just be sure to have Android.mk file
Do you have a file application.mk.If you do have than please delete that file.Sometimes the path specified in the application.mk file causes this error.
I got the same issue and this is because mistaken I named file Andorid.mk instead Android.mk. This work for me.

Categories

Resources