Dynamically linking to a shared Java Library in Android - android

I'm trying to separate a Java Library, that is used by multiple Android "services", into a dynamic or shared library that can be loaded by those independent services without having the library included into the APK of each service.
I know there are different ways of doing this like creating an Android Service or using DexLoader and Reflection but I'm trying to avoid changing the source of the library. Instead I'm trying to build it and install it on my device (essentially extending the provided android API).
The following is a very similar question which is still unanswered:
Create Android apps in Eclipse sharing common library
I know this is something Google doesn't want to disclose so finding information online is extremely difficult.
So far I've tried placing a simple "Hello World" program under the frameworks dir and build it which successfully created a jar for my program. Then I added my package in product/core.mk and in addition added my package definition under api/10.xml after which I ran "make sdk" which resulted in the following error message:
******************************
You have tried to change the API from what has been previously released in
an SDK. Please fix the errors listed above.
******************************
make: *** [out/target/common/obj/PACKAGING/checkapi-last-timestamp] Error 38
As a workaround I added my package into "public_api.xml" file, inside the out directory, which is somehow dynamically created during the build process. With this workaround the SDK is built with no errors (although if I do clean again I'll have to modify the public_api.xml again because it will be removed due to clean). However, when I try to import and use my package anywhere it still says that my package "does not exist"
Any help will be greatly appreciated! Thank you!

Finally figured it out. The solution turns out to be very simple!
Place your library in the frameworks/base folder and make sure all your source code is inside under java directory like so:
../frameworks/base/HelloWorld/java/<source files and folders>
Edit core.mk file located under build/target/product/ to include your package in the list. This will add HelloWorld library to the framework:
PRODUCT_PACKAGES := \
bouncycastle \
:
:
DefaultContainerService \
Bugreport \
HelloWorld
Edit pathmap.mk file located under build/core/ to include your directory in the list. This will add HelloWorld library to the android.jar
FRAMEWORKS_BASE_SUBDIRS := \
$(addsuffix /java, \
core \
graphics \
location \
media \
opengl \
sax \
telephony \
wifi \
vpn \
keystore \
voip \
HelloWorld \
)
Done. rebuild android and it should not complain and add your library to framework.jar!
I hope this helps.

Related

Tensor Flow Could not load custom protobuf files in

I have just created a protobuf file (.pb file) for my own custom images using a TensorFlow tutorial.
But when I replaced the same file into the assets folder in tensorflow/examples/android/assets and try to build and generate an APK, the APK gets generated, but when I run the APK in an Android device, the APK crashes.
If I run the classify_image from Python, it gives me proper results.
Appreciate any help.
Since DecodeJpeg isn't supported as part of the core, you'll need to strip it out of the graph first.
bazel build tensorflow/python/tools:strip_unused && \
bazel-bin/tensorflow/python/tools/strip_unused \
--input_graph=your_retrained_graph.pb \
--output_graph=stripped_graph.pb \
--input_node_names=Mul \
--output_node_names=final_result \
--input_binary=true
Change few parameters in this file
/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowImageListener.java
The input sizes need to be 299, not 224. You'll also need to change the mean and std values both to 128.
INPUT_NAME to "Mul:0" ,
OUTPUT_NAME to "final_result:0"
after which you will be able to compile the apk.
Good Luck

Compiling libDispatch (GCD) for Android

I'm trying to compile libdispatch for linux on Android. However I could not find any instructions for Android in the project files.
I can see there are several StackOverflow questions about using libdispatch, but the information is rather thin.
Has anyone successfully compiled libdispatch for Android using NDK?
I haven't personally tried compiling libdispatch for Android, but I have built many other libraries. It looks like building libdispatch should be quite easy, as there is already an Android.mk file in the repository. You should be able to build this library with the following command (split across lines for readability):
<ndk directory path>/ndk-build \
-C <path to libdispatch source directory> \
NDK_LIBS_OUT=<path to directory for built libraries> \
APP_BUILD_SCRIPT=Android.mk

MAKE problems in Android Project enabled with cocos2dx - "No rule to make target"?

BACKGROUND:
I'm following a tutorial online: Game apps in Android by Todd Perkins
- It's been very straightforward other than the hiccups that come with setting up NDK with eclipse, which I've managed to solve.
Problem:
I'm stuck at the point where I want my cocos2dx code to respond to a touch event and based on that, call Java android code from an activity.
I've added a bunch of screen shots below to show you how I've set everything up:
When I run build_native.sh:
Now I've made sure that I've linked my folders correctly in the project.
Below is a screen shot of the "linked" JNI folder(jni_linked), along with JniURL.cpp and JniURL.h:
and here's what my Android.mk file looks like:
I've double/triple checked the path of my Jni folder and it is:
C:\Users\DarkRaveDev\Documents\cocos2d-x-2.0.1\cocos2dx\platform\android\jni
Do I need to change the Android.mk file to have this line:
LOCAL_SRC_FILES := helloworld/main.cpp \
../../Classes/AppDelegate.cpp \
../../jni_linked/JniURL.cpp \
../../Classes/HelloWorldScene.cpp
It's probably treating:
/../../../cocos2dx/platform/android/jni/JniURL.cpp \
as starting with an absolute path instead of a relative path. It probably should be:
$(LOCAL_PATH)/../../../cocos2dx/platform/android/jni/JniURL.cpp \
or maybe:
../../../cocos2dx/platform/android/jni/JniURL.cpp \

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...

Compiling and using OpenSSL for Android, on windows using eclipse

I am developing an Android app on windows, using eclipse.
I would like to use OpenSSL for my Android application. It needs to be used with C++ via NDK.
I tried to download fries' OpenSSL source code from https://github.com/fries/android-external-openssl and build it.
I read the README.android file, but I didn't understand what these instructions are for.
Do I need to download the OpenSSL source code in addition to the fries source code?
I have tried putting all the files from Fries' into my jni folder of the Android project. It complained about not finding e_os.h and openssl/crypto.h and others like that.
More specifically:
In file included from D:/Projects/Fatal/Android/OpenSSL/jni/ssl/s2_meth.c:59:
D:/Projects/Fatal/Android/OpenSSL/jni/ssl/ssl_locl.h:124:18: error: e_os.h: No such file or directory
D:/Projects/Fatal/Android/OpenSSL/jni/ssl/ssl_locl.h:126:28: error: openssl/buffer.h: No such file or directory
D:/Projects/Fatal/Android/OpenSSL/jni/ssl/ssl_locl.h:127:26: error: openssl/comp.h: No such file or directory
I am not sure why it doesn't find those files, I tried to add all sort of paths into LOCAL_C_INCLUDES in all sort of Android.mk files, but nothing worked.
I would like to know how to build OpenSSL for Android. Thank you.
I know this is old, but I kept coming across this when I was searching for this problem. In the case that you are building a standalone OpenSSL to go with your project and are getting this error, I found a solution as per this thread:
In openssl-android/crypto, openssl-android/ssl, and openssl-android/apps, you'll find a variable declaration for local_c_includes that is something like this:
local_c_includes := \
$(NDK_PROJECT_PATH) \
$(NDK_PROJECT_PATH)/crypto/asn1 \
$(NDK_PROJECT_PATH)/crypto/evp \
$(NDK_PROJECT_PATH)/include \
$(NDK_PROJECT_PATH)/include/openssl
If you've placed your openssl-android directory in your Android project in the project/jni directory, then these variables no longer point to the correct location. The way I solved it was to modify these paths to be relative to LOCAL_PATH:
local_c_includes := \
$(LOCAL_PATH)/.. \
$(LOCAL_PATH)/asn1 \
$(LOCAL_PATH)/evp \
$(LOCAL_PATH)/../include \
$(LOCAL_PATH)/../include/openssl
Remember, you'll have to do this for each of the directories you're trying to build (crypto, ssl, or apps).
I realized that the fries' github is just something to patch the source of OpenSSL, it's not a stand alone OpenSSL for android.
It is required because it provide Android.mk files which are required by ndk-build of ndk. You don't want to build it using standard gcc because you want to build it for Android.

Categories

Resources