Android DNK developed so file. How to call it directly in Linux? - android-ndk

Now I have a so file which developed by DNK. I want to call the so file use C in Linux. But it always prompts:
[root#PCGiter Code]# gcc SoTest2.c -o SoTest2.exe -ldl
[root#PCGiter Code]# ./SoTest2.exe
Open Error:libcom_wuba_aes_ExecV3_1_0.so: cannot open shared object file: No such file or directory.

This answer is for creating the executables for Android and executing them in shell like in Linux, but not how to execute Android executables in Linux.
Use Android-ndk for building the source files, then you can copy directly to emulator and execute in the adb shell.
Example of the make file for creating an executable for android
# For building the Test executable
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Linker flags
LOCAL_LDLIBS += -llog
LOCAL_LDLIBS += -lOpenSLES
LOCAL_LDLIBS += -landroid
# Include paths
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)
# Local C Flags if any
LOCAL_CFLAGS :=
# Source Files to compile
LOCAL_SRC_FILES := \
# Shared libraries to be used while linking
LOCAL_SHARED_LIBRARIES :=
# Local module name
LOCAL_MODULE :=
include $(BUILD_EXECUTABLE)
You can get in to the emulator shell by launching the emulator and then executing the command in command prompt "adb shell" .
Usually what I observed is when copied to /data/ folder only, I was able to execute. Other folders such as /mnt/sdcard I was not able to execute the executable.

Related

ndk-build error cannot found -landroid

building .so file, I receive this link error.
"C:/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -landroid"
"Android.mk" file includes this option.
LOCAL_LDLIBS += -llog -ldl -lz -landroid
And "Application.mk" file includes this options.
APP_ABI := armeabis
APP_ABI += x86
when I set the "APP_PLATFORM=android-xx" option in "Application.mk" file, I can build .so file successfully for both x86 and armeabi.
OR
when I remove "APP_ABI := armeabi" option in Application.mk file without "APP_PLATFORM=android-xx" option, I can also build only .so file for x86.
In my desktop, there is libandroid.so file in ndk\platform\android-xx\arm\usr\lib directory...
Why does this error occur?
If not set, APP_PLATFORM defaults to the lowest supported platform version for that NDK. For r10d, this was android-3 (cupcake).
libandroid wasn't available until android-9 (Gingerbread): https://developer.android.com/ndk/guides/stable_apis.html

How do you add APP_CFLAGS to an android makefile

I was able to successfully pass in APP_CFLAGS via eclipse through via the settings' ndk-build command as
ndk-build -B NDK_DEBUG=1 APP_CFLAGS=-DTEST
I have now switched to Android studio and was trying to do it directly in the makefile with the following, but its not taking:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
APP_CFLAGS += -DTEST
LOCAL_SRC_FILES:= test.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
It builds fine, but my TEST variable is never set in the C++ code.
How can I add it to the makefile?
Or, How can I add it to Android Studio project settings?
You could use LOCAL_CFLAGS for C/C++ code additional flags or LOCAL_CPPFLAGS for C++ code in Android build system.
LOCAL_CFLAGS := -DTEST=1

Warning : Overriding commands for target Android Makefile

I've got an issue building my C++ code using NDK r9d, since I try to compile C files using C++ compiler (G++) I've got this warnings :
C:/Android/ndk/build/core/build-binary.mk:393: warning: overriding commands for target
C:/Android/ndk/build/core/build-binary.mk:391: warning: ignoring old commands for target
Before I didn't need to compile with C++ 11 and my C files was compiled with GCC, I had no problems, but since I had LOCAL_CPP_EXTENSION := .cpp .c, this warnings appears (only for C files).
I found that someone else had the same problem (Overriding commands for target Android Makefile) but didn't get any answer.
Here is my files :
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
MY_INC_PATH := ../../..
LOCAL_MODULE := test
LOCAL_CFLAGS := -Werror
LOCAL_CPPFLAGS := -std=c++11
LOCAL_LDLIBS := -ldl -llog -lGLESv1_CM
LOCAL_C_INCLUDES := \
$(MY_INC_PATH)
MY_SRC_PATH := ../../../..
LOCAL_CPP_EXTENSION := .cpp .c
LOCAL_SRC_FILES := \
$(MY_SRC_PATH)/XXX.c \
$(MY_SRC_PATH)/YYY.cpp \
$(MY_SRC_PATH)/ZZZ.cpp
include $(BUILD_SHARED_LIBRARY)
I use NDK r9d and compile with G++ 4.8 and C++ 11 activated. Thanks for your help.
As of r9d, NDK does not provide methods to unassociate .c files from C compiler. You can redefine $$(TARGET_CC), and also set LOCAL_CFLAGS += -std=c++11, and not set LOCAL_CPP_EXTENSION to include .c, but that would be a hack, anyways. So if you cannot rename the files, and do not want to hack your NDK, the cleanest solution would be to simply ignore the warning.

Problems compiling object using arm-linux-androideabi-gcc

Background
OSX is OS
R8 NDK
I am trying to compile the following class using the Android GCC compiler...
#include <stdint.h>
int main (void){
return 0;
}
I do the with the following command...
un#un:~/Development/Code/OpenGL$ ~/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc hello.c -o hello
I get...
In file included from hello.c:1:0:
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/include/stdint.h:3:26: fatal error: stdint.h: No such file or directory
compilation terminated.
So due to a lack of gcc knowledge (but some Google ability) I find this and try it...
un#un:~/Development/Code/OpenGL$ ~/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi-gcc hello.c -o hello -ffreestanding
and I get...
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/Users/un/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status
Can someone help me with what I am doing wrong? Am I missing a link or something? Android.mk is not an option.
UPDATE this isn't working either...
arm-linux-androideabi-gcc hello.c --sysroot=~/Development/Android/android-ndk-r8c/platforms/android-9/arch-arm
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtbegin_dynamic.o: No such file or directory
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot open crtend_android.o: No such file or directory
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lc
/Users/jackiegleason/Development/Android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status
You must tell GCC where to find the Android system files and headers. Either use:
ndk-build and an Android.mk with BUILD_EXECUTABLE
or, the --sysroot GCC option
[1]
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo.c
include $(BUILD_EXECUTABLE)
[2]
# Change `android-9` with the level you target
/path/to/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt\
/darwin-x86/bin/arm-linux-androideabi-gcc\
--sysroot /path/to/android-ndk-r8c/platforms/android-9/arch-arm/\
foo.c -o foo
# Or generate a ready-to-use standalone toolchain (better)
/path/to/android-ndk-r8c/build/tools/make-standalone-toolchain.sh \
--platform=android-9 \
--install-dir=/tmp/my-android-toolchain
export SYSROOT=/tmp/my-android-toolchain/sysroot
/path/to/arm-linux-androideabi-gcc --sysroot $SYSROOT foo.c -o foo
So, since I don't want to use Android.mk file, I went ahead and created a standalone toolchain. this is done using the following...
/Users/un/Downloads/android-ndk-r8d/build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=/tmp/my-toolchain
/tmp/my-toolchain/bin/arm-linux-androideabi-gcc hello.c
I would like to know what the "alternative" is in terms of the gcc linking I could do.
This answer adds a bit more details to #deltheil's answer. I had similar issues as I was trying to compile I2C-tools for debugging I2C bus on Android. Somehow after struggling for more than a day with make files and trying different options including --sysroot & --dynamic-linker options etc., I finally tried to compile it within the Android AOSP tree. I used the Google Nexus-S AOSP to build a binary that I intended to run on Samsung S3 phone. I created a folder called i2c-tools for the sources inside the AOSP/external folder and copied Android.mk, Cleanspec.mk & MODULE_LICENCE from another executable folder (ping) and modified it for i2c-tools as follows:
ifneq ($(TARGET_SIMULATOR), true)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := i2cdetect.c i2cbusses.c
LOCAL_C_INCLUDES := $(KERNEL_HEADERS)
LOCAL_MODULE := i2cdetect
LOCAL_MODULE_TAGS := tests
LOCAL_SHARED_LIBRARIES := libc
include $(BUILD_EXECUTABLE)
endif
Then I just ran:
source build/envsetup.sh
make i2cdetect
from the AOSP base folder and voila, I had a working executable in out/target/product/generic/system/bin/ folder. Note that I had to copy all needed source and header files from the original (i2c-tools)/tools & include folders and had to modify some of the #include to remove the extra path for header files that were now in the same place as the c-source.
In my case, I needed an .o file and did not need to define main().
I had to specify the -c switch:
~/ax/arm-linux-androideabi-g++ --sysroot=~/an/platforms/android-8/arch-arm/ -c dummy.c
where ~/ax and ~/an are links:
ax -> ~/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/
an -> ~/android-ndk-r9d/
did dummy.o.
i solved the problem ,
the default compress file manager in ubuntu wasn't extracting symbolic links ,
so i tried : tar jxf filename.tar.bz2
to untar the ndk.tar.bz2 and now it works fine

NDK profiling showing ".so has no symbols"

I am doing NDK profiling for my project using android-ndk-profiler-3.1. I have made changes in Android.mk as follows...
LOCAL_PATH := $(call my-dir)
-include android-ndk-profiler.mk
include $(CLEAR_VARS)
# Module name -------------------------------------------------------
LOCAL_CFLAGS += -O3
TARGET_ARCH_ABI :=armeabi
LOCAL_CFLAGS := -pg
LOCAL_STATIC_LIBRARIES := andprof
LOCAL_LDLIBS += -llog
LOCAL_MODULE := libitv
include $(BUILD_SHARED_LIBRARY)
Application.mk is as follows...
APP_ABI := armeabi
APP_PLATFORM := android-10
I have called monstartup("itv.so"); function in the beginning of the native code and moncleanup(); function in the stop method. And gmon.out file is created successfully.And then I have pasted gmon.out in
D:\android\android-ndk-r6-windows\android-ndk-r6\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin directory.
But when I am trying to read gmon.out using the following command...
D:\android\android-ndk-r6-windows\android-ndk-r6\toolchains\arm-linux-androideab
i-4.4.3\prebuilt\windows\bin>arm-linux-androideabi-gprof D:\InternetTV_FD_Canvas
\libs\armeabi\libitv.so > out.txt
This Error is showing...
arm-linux-androideabi-gprof: file `D:\InternetTV_FD_Canvas\libs\armeabi\libitv.so'
has no symbols
I am not able to make out why this error is coming even I have done everything fine.
Can anybody please help me.
Any help will be appreciated.
Thanks in Advance.
The NDK build process creates 2 libraries, one with symbols and one stripped without. You install the stripped, symbol-less library in your APK, but you need to use the unstripped version with gprof. If you run:
arm-linux-androideabi-gprof D:\InternetTV_FD_Canvas\obj\local\armeabi\libitv.so
... then that should be the correct library.

Resources