Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top