How to include a binary file with Android NDK project? - android

I know its bad practice but whats the best way to include a binary file with Android NDK project? The file should be executable with the system command.
I don't want to copy it manually copy to the device but distribute it within my .apk.

It is enough to rename the executable to "lib_myexecutable_.so" and put it in libs/armeabi folder under your project root: this file will be extracted by the Package Manager on the device to /data/data/your.package.full.name/lib, with executable permissions.
If you use ndk-build to build myexecutable, all ./libs/$(TARGET_ARCH_ABI) directory will be refreshed.
To automate the rename operation, use the following in your Android.mk:
... # whatever you have to define the executable
include $(BUILD_EXECUTABLE)
# immediately after this,
all: .libs/$(TARGET_ARCH_ABI)/lib_myexecutable_.so
.libs/$(TARGET_ARCH_ABI)/lib_myexecutable_.so: $(LOCAL_INSTALLED)
<tab>$(call host-mv, $<, $#)

Related

ndk_build.cmd in cygwin not recognizing included mk files

I have multiple static libs that are built using jni. I have a scheme that includes other mk files, etc to allow me to share compile settings, include files from other dependent projects, etc.
I have set it up successfully on the mac and have been building it. However, when I tried to do the same in windows under cygwin I'm running into issues...
Here is my jni/Android.mk file..
LOCAL_PATH := $(call my-dir)
INJ_PROJECT_PATH := $(PWD)
include $(INJ_PROJECT_PATH)/jni/Project.mk
The problem I'm seeing is that ndk-build is complaining about the Project.mk file. Here is what I'm seeing..
shammi#SHAMMIDEV /cygdrive/c/projects/injitiProjects/injitiEngine/Engine/Core
$ ndk-build.cmd NDK_APP_OUT=../../build/Android/Engine/release
jni/Android.mk:5: /cygdrive/c/projects/injitiProjects/injitiEngine/Engine/Core/jni/Project.mk: No such file or directory
jni/Android.mk:5: /cygdrive/c/projects/injitiProjects/injitiEngine/Engine/Core/jni/Project.mk: No such file or directory
jni/Android.mk:5: /cygdrive/c/projects/injitiProjects/injitiEngine/Engine/Core/jni/Project.mk: No such file or directory
jni/Android.mk:5: /cygdrive/c/projects/injitiProjects/injitiEngine/Engine/Core/jni/Project.mk: No such file or directory
C:/projects/Android/sdk/ndk-bundle/build//../build/core/build-all.mk:89: Android NDK: WARNING: There are no modules to build in this project!
make: *** No rule to make target `/cygdrive/c/projects/injitiProjects/injitiEngine/Engine/Core/jni/Project.mk'. Stop.
I have confirmed that /cygdrive/c/projects/injitiProjects/injitiEngine/Engine/Core/jni/Project.mk does exist, and I am the owner and it has read permissions for user/group/other.
Again, this exact same setup works absolutely fine on the mac. I'm just looking at having a portable dev environment.
What am I missing here..?
I guess I forgot that ndk-build.cmd was a windows shell batch script (DOH).
The answer was that I was able to call /build/ndk-build which is a sh script that is comfortable running in cygwin.
You probably need to use LOCAL_PATH instead of PWD. I'm not really sure how PWD gets set (I think it's a make builtin?), but we actually have control over LOCAL_PATH and can make sure it gets set in the right way for cygwin.
Instead of include what if you define it to be APP_BUILD_SCRIPT?
for ex.
APP_BUILD_SCRIPT := jni/Android.mk
Also Id try $(shell $pwd) instead of $(PWD) if the above doesn't work, usually ndk doesn't interpret shell commands for me if I don't use the shell in the parenthesis.

"openssl/aes.h: No such file or directory" under Android

I'm trying to compile to android environment. And because of that I get the following error:
error: openssl/aes.h: No such file or directory
I find some solution in stack but, I don't get how to end the process to be able to compile.
I already compiled one version of openssl were should I add the libs? or how can I generate the *.a?
Do you know how can I add this library to the arm-linux-androideabi-g++ that I need to run to be able to pass this problem?
[1] Get openssl library which has aes.h file in its include folder.
[2] If you have compiled openssl library in your lib folder then add to -lssl or -lopenssl to your command line.
Here you can find openssl includes: openssl
Download this includes and put them in some folder in your project, i.e. project_dir/module_dir/jni/openssl-includes.
Then you need set a LOCAL_C_INCLUDES variable in your Android.mk (which also is in jni folder:
LOCAL_C_INCLUDES += ./openssl-includes
After that, you can include files in openssl-includes folder directly by name, i.e.:
#include <aes.h>
If you need an *.a file as output, you should include BUILD_STATIC_LIBRARY in Android.mk, if you need a *.so lib, include BUILD_SHARED_LIBRARY.

Android NDK: build after added a new library module directly under jni/ folder

In my Android NDK project, I used to have only one library module:
jni/
Android.mk
... (more source files)
Then, I need to add another new library module, which has no connection with the existing library module. I mean they are independent. I directly added the new library under jni/ folder.
jni/
Android.mk
... (more source files)
new-lib/
Android.mk
... (more source files)
Question 1:
I run ndk-build command under my Android project to build. But new-lib is not built at all. Is that because I need to declare the new-lib module in the Android.mk under jni/ folder ? If so, how to declare ?
Question 2: when I load the new-lib (suppose the module name is "newlib"), should I use
System.loadLibrary("new-lib/newlib");
or just
System.loadLibrary("newlib"); ?
First of all, you don't run android-ndk command, you run ndk-build command.
Question 1:
You can tell your top level Android.mk i.e. $PROJECT/jni/Android.mk to include another Android.mk that are located in different places/sub-directories.
If you have defined any Android.mk files in sub-directories, you have to include them explicitly in your top-level Android.mk. There is a helper function for that
include $(call all-subdir-makefiles)
Just add it in your top-level Android.mk file. This will include all the Android.mk files from sub-directories of the current file's path.
Question 2:
use System.loadLibrary("newlib");
No need to specify path.

fluidsynth library file compilation to .so file in android

I am trying to compile the files from https://bitbucket.org/kunstmusik/fluidsynth-android to .so file. The files from this site are .c and .h which have the include files. I have installed ndk, configured it.
I have created a new project in eclipse
created the jni folder and copied the files from the web site to this folder
in command prompt in the jni folder, I issued the command ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk. This ran without error
In the /obj/local/ folder, files were created with .o and .d in the fluidsynth-andriod folder. There is also a .a file in armeabi and aremabi-v7a. I assume the .a is a static library. Do I need to create a .so file? Do i need to link the c/c++ to java. If so how do I do this.
Please help...
I was running into this same issue myself. The Android.mk file included in the fluidsynth-android code contains the line
include $(BUILD_STATIC_LIBRARY)
This is what causes the .a files to be generated.
If you change this to
include $(BUILD_SHARED_LIBRARY)
you will get .so files instead of .a files.

Compiling linked ( not copied ) files using NDK

I'm building an OpenGL based app - writing in native C/C++.
I want to have my native files in a single location and only linked to the project ( so when I edit them in XCode/Eclipse the other project already has the updated files ). However, I can't figure out how to get ndk-build to build files that aren't actually in the JNI folder. Symbolic links ( in OSX ) didn't do the trick.
I keep getting
make: *** No rule to make target `/Developer/SDKs/android-ndk-r7b/build/core/myfile.cpp', needed by `obj/local/armeabi/objs/glHelloArrow/myfile.o'. Stop.
Any ideas?
I build outside of JNI folder on Windows. My Android.mk includes a makefile in a different folder:
LOCAL_PATH := $(MY_WORKSPACE)/path/jni
include $(MY_WORKSPACE)/path/jni/Src.mk
where the actual sources are listed:
LOCAL_SRC_FILES := \
a.cpp \
b.cpp \
etc.
One caveat is - make does not work right with relative paths. Or rather, it does, but assumes the path to be relative to the current folder, which in case of the ndk-build process can be all over the place. Thus the env variable that denotes the root of the file location.
Some details here.
I have a similar issue with cross-platform codebase. I've not found any way to do this.
My workaround is to create a symlink from my source code root directory into jni, so I can give the NDK build system paths like jni/link/common/foo.c.
Just be sure that you remove the symlink if you ever need to run tools that do recursive directory traversals...

Categories

Resources