2

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!

Community
  • 1
  • 1
HerpDerp
  • 53
  • 1
  • 8
  • Why not just use AIDL for RPC across the apps? – Kristopher Micinski May 18 '12 at 17:57
  • If your package is being ignored because it's not part of a list of offical APIs, presumably you need to find that list within the build configuration and add it. A recursive grep for occurrences of the name of something small which is included might help you find where yours has not yet been added. But, do you really want to do it this way? Framework modifications would only apply to devices running your customized android build. – Chris Stratton May 18 '12 at 18:04
  • @KristopherMicinski - I'm trying to avoid source code modification and trying to investigate this particular approach. chris - my device is running my own customized android build already so I'm fine with that. And yes that exactly what I'm trying to find in the build configuration but Google is making it very hard to do. – HerpDerp May 18 '12 at 18:08
  • Have you tried to create an "Android Library" project in Eclipse and then load the created APK-library via "uses-library" in AndroidManifest.xml? That should do what you want (no code changes in library users). – Robert May 18 '12 at 18:11
  • Yes, but that will add it statically to the APK which is what I'm trying to avoid. – HerpDerp May 18 '12 at 18:14
  • You'll at least have to modify the stubs (i.e., "android.jar" which gets linked in statically to provide the thunks..) – Kristopher Micinski May 18 '12 at 19:00
  • I realize that something must be done with the "android.jar" but I'm still trying to figure out how it works. Do you know where those stubs are defined? what I did try is this, I followed the following tutorial (http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-2-hacking-around/) and expended the jar and the dex file of my Hello World application to get the .class file which I then added to the android.jar file and it didn't seem to make a difference. Also, @ChrisStratton, I found the following file /out/target/common/obj/PACKAGING/public_api.xml but its on target – HerpDerp May 18 '12 at 19:38
  • Have you read [this blog post](http://android-developers.blogspot.co.nz/2011/07/custom-class-loading-in-dalvik.html) yet? It may give you some clue. – yorkw May 18 '12 at 21:58
  • At this week's AnDevCon III conference, Marko Gargenta's "Remixing Android" presentation went through the steps of creating a custom ROM, including creating an SDK add-on, which is effectively what you are trying to do. I seem to recall that Marakana.com has a video of another version of this presentation, and he may post his AnDevCon III slides in the upcoming days. – CommonsWare May 19 '12 at 00:25
  • @yorkw and CommonsWare, thanks for sharing this information I will go through it and report back if it worked for me. – HerpDerp May 22 '12 at 17:18
  • @yorkw thanks for the link I went through the blog and it uses DexLoader and Reflection which I'm trying to avoid using due to source code modification and because I'm trying to figure out this Android build system. – HerpDerp May 23 '12 at 18:57

1 Answers1

2

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.

HerpDerp
  • 53
  • 1
  • 8