Heads Up: Adding a library to Android

2,525 views
Skip to first unread message

Dianne Hackborn

unread,
Dec 18, 2008, 12:52:27 AM12/18/08
to android-...@googlegroups.com
Hi all,

One of the questions that comes up a lot is how to add new libraries and APIs to Android, so in cupcake we now have sample code for doing so.  If you want to add new features or APIs to a device running Android, which are not to be contributed back to the core platform, this should show you how to do it.  It also includes samples of implementing JNI-based native code that is used by such a library and writing clients against the library.

You can find the code here:

http://android.git.kernel.org/?p=platform/development.git;a=tree;f=samples/PlatformLibrary;hb=cupcake

See the README.txt file there for more detailed information.

Writing your own library should be a simple matter of copying the 2 or 4 files you need from there and then filling in your own code.

--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support.  All such questions should be posted on public forums, where I and others can see and answer them.

Atish

unread,
Dec 31, 2008, 8:11:59 AM12/31/08
to android-platform
Hi i tried that sample code.Will it work only in cupcake version??I
pur that folder in framework/base.The i called one method defined in
java file from my application.It gave me error.
package com.example.android does not exist
import com.example.android.platform_library;
^
packages/apps/Music/src/com/android/music/MusicBrowserActivity.java:
65: cannot find symbol
symbol : class PlatformLibrary
location: class com.android.music.MusicBrowserActivity
PlatformLibrary p=new PlatformLibrary();
^
packages/apps/Music/src/com/android/music/MusicBrowserActivity.java:
65: cannot find symbol
symbol : class PlatformLibrary
location: class com.android.music.MusicBrowserActivity
PlatformLibrary p=new PlatformLibrary();
^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
make: *** [out/target/common/obj/APPS/Music_intermediates/classes.jar]
Error 41
PLZ HELP ME OUT...
On Dec 18, 10:52 am, "Dianne Hackborn" <hack...@android.com> wrote:
> Hi all,
>
> One of the questions that comes up a lot is how to add new libraries and
> APIs to Android, so in cupcake we now have sample code for doing so.  If you
> want to add new features or APIs to a device running Android, which are not
> to be contributed back to the core platform, this should show you how to do
> it.  It also includes samples of implementing JNI-based native code that is
> used by such a library and writing clients against the library.
>
> You can find the code here:
>
> http://android.git.kernel.org/?p=platform/development.git;a=tree;f=sa...
>
> See the README.txt file there for more detailed information.
>
> Writing your own library should be a simple matter of copying the 2 or 4
> files you need from there and then filling in your own code.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Dianne Hackborn

unread,
Jan 1, 2009, 3:47:06 AM1/1/09
to android-...@googlegroups.com
Did you make sure to link to the library in your application's makefile?  Be sure the sample code builds and runs correctly, and then make sure your app is doing the same thing.

The sample code is built for the cupcake code base, though you can use the same idea for pre-cupcake.
hac...@android.com

Nick

unread,
Jan 6, 2009, 9:51:00 PM1/6/09
to android-platform
Dianne,

What extra steps are needed to make a shared library part of a custom
Android SDK?

For example, current SDK releases contain the com.google.android.maps
classes in Android.jar. What would it take to include
com.example.android.platform_library in Android.jar during the SDK
build?

I would like to be able to make a custom SDK that exposes a shared
library that can be used to develop applications against. This way
developers can develop entirely in Eclipse without having to pull
their application into the Android system build process.

Thanks,

Nick


On Dec 18 2008, 12:52 am, "Dianne Hackborn" <hack...@android.com>
wrote:
> Hi all,
>
> One of the questions that comes up a lot is how to add new libraries and
> APIs to Android, so in cupcake we now have sample code for doing so.  If you
> want to add new features or APIs to a device running Android, which are not
> to be contributed back to the core platform, this should show you how to do
> it.  It also includes samples of implementing JNI-based native code that is
> used by such a library and writing clients against the library.
>
> You can find the code here:
>
> http://android.git.kernel.org/?p=platform/development.git;a=tree;f=sa...
>
> See the README.txt file there for more detailed information.
>
> Writing your own library should be a simple matter of copying the 2 or 4
> files you need from there and then filling in your own code.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Dianne Hackborn

unread,
Jan 6, 2009, 10:11:55 PM1/6/09
to android-...@googlegroups.com
Sorry, I don't really know much about building an SDK.  I do know there is working going on now to allow different SDKs with custom libraries and other things.
hac...@android.com

Xavier Ducrohet

unread,
Jan 6, 2009, 10:36:17 PM1/6/09
to android-...@googlegroups.com
Hi Nick,

This is currently not supported by the official SDK.
There is however work being done to support this, both at the build
system level and in the developer tools (both Ant script and ADT)

In cupcake you will be able to build (and distribute) an SDK add-on
that will contain:
- new system image to run the emulator with your custom version of
Android, including your new library(ies)
- a jar file for developers to link against when using your new APIs (if any).
- custom emulator skins.

Developers will be able to choose what they wish to target (ie
standard Android platform, or add-on with optional APIs).

While some of this work is already in cupcake (if you do make SDK you
will see the SDK folder structure has changed and now contains
platforms/ and add-ons/ folders), it's unfortunately not ready.
I'll will post in here when you can start using it.

Xav

On Tue, Jan 6, 2009 at 6:51 PM, Nick <nick...@gmail.com> wrote:
>

Nick

unread,
Jan 7, 2009, 8:43:37 AM1/7/09
to android-platform
Thank you both!

On Jan 6, 10:36 pm, Xavier Ducrohet <x...@google.com> wrote:
> Hi Nick,
>
> This is currently not supported by the official SDK.
> There is however work being done to support this, both at the build
> system level and in the developer tools (both Ant script and ADT)
>
> In cupcake you will be able to build (and distribute) an SDK add-on
> that will contain:
> - new system image to run the emulator with your custom version of
> Android, including your newlibrary(ies)
> - a jar file for developers to link against when using your new APIs (if any).
> - custom emulator skins.
>
> Developers will be able to choose what they wish to target (ie
> standard Android platform, or add-on with optional APIs).
>
> While some of this work is already in cupcake (if you do make SDK you
> will see the SDK folder structure has changed and now contains
> platforms/ and add-ons/ folders), it's unfortunately not ready.
> I'll will post in here when you can start using it.
>
> Xav
>
> On Tue, Jan 6, 2009 at 6:51 PM, Nick <nickp...@gmail.com> wrote:
>
> > Dianne,
>
> > What extra steps are needed to make a sharedlibrarypart of a custom
> > Android SDK?
>
> > For example, current SDK releases contain the com.google.android.maps
> > classes in Android.jar. What would it take to include
> > com.example.android.platform_library in Android.jar during the SDK
> > build?
>
> > I would like to be able to make a custom SDK that exposes a shared
> >librarythat can be used to develop applications against. This way
> > developers can develop entirely in Eclipse without having to pull
> > their application into the Android system build process.
>
> > Thanks,
>
> > Nick
>
> > On Dec 18 2008, 12:52 am, "Dianne Hackborn" <hack...@android.com>
> > wrote:
> >> Hi all,
>
> >> One of the questions that comes up a lot is how to add new libraries and
> >> APIs to Android, so in cupcake we now have sample code for doing so.  If you
> >> want to add new features or APIs to a device running Android, which are not
> >> to be contributed back to the core platform, this should show you how to do
> >> it.  It also includes samples of implementing JNI-based native code that is
> >> used by such alibraryand writing clients against thelibrary.
>
> >> You can find the code here:
>
> >>http://android.git.kernel.org/?p=platform/development.git;a=tree;f=sa...
>
> >> See the README.txt file there for more detailed information.
>
> >> Writing your ownlibraryshould be a simple matter of copying the 2 or 4

QF

unread,
Jan 12, 2009, 10:57:45 PM1/12/09
to android-platform
Hi Xav,

Will the SDK add-on support resources, such drawables or layout files?

Thanks,
Q.F.

On Jan 6, 10:36 pm, Xavier Ducrohet <x...@google.com> wrote:
> Hi Nick,
>
> This is currently not supported by the official SDK.
> There is however work being done to support this, both at the build
> system level and in the developer tools (both Ant script and ADT)
>
> In cupcake you will be able to build (and distribute) an SDK add-on
> that will contain:
> - new system image to run the emulator with your custom version of
> Android, including your new library(ies)
> - a jar file for developers to link against when using your new APIs (if any).
> - custom emulator skins.
>
> Developers will be able to choose what they wish to target (ie
> standard Android platform, or add-on with optional APIs).
>
> While some of this work is already in cupcake (if you do make SDK you
> will see the SDK folder structure has changed and now contains
> platforms/ and add-ons/ folders), it's unfortunately not ready.
> I'll will post in here when you can start using it.
>
> Xav
>

fructose

unread,
Feb 4, 2009, 4:56:08 PM2/4/09
to android-platform
Hello

Could somebody please add some clarification to this thread.

I would like to write some C/C++ code which is accessed and can access
an app in the Java layer above, which I presume can only be done via
JNI?

The readme says the example is only for people to create a system
image that will be delivered to a device. Doesn't that therefore limit
the applicability of the example to device manufacturers only who can
flash the image to the device?

Is it possible for a 3rd party developer to write a C/C++ engine and
Jave App combination that can be installed onto post production
devices? If not, when will support for this be added?

Thanks

Dianne Hackborn

unread,
Feb 4, 2009, 5:17:57 PM2/4/09
to android-...@googlegroups.com
Native code is not currently supported for third party developers.  It's something that we want to do, but there is no timeframe right now.

You can search pretty much any android group to find this information repeated over and over. :}
--
Dianne Hackborn
Android framework engineer
hac...@android.com

fructose

unread,
Feb 4, 2009, 6:16:16 PM2/4/09
to android-platform
Hi


Sorry, I am a moderator in a development group myself and know how
frustrating it can be if people post questions that are seemingly
asked repeatedly.
I have been searching every group believe me, the reason I am
persisting is I have not found the information that is repeated over
and over to adequately or simply explain the situation ;-).

Would adding stickies or FAQs of the most recurring themes help?


Ok so if developing applications in native code is not currently
supported for 3rd party developers, could Google give an indication
(including whatever disclaimers about timeframes, non committalls
etc.) about if and possibly when this could be available?


An understandable technical explanation of why its not possible would
be greatly appreciated by many users of these boards. I saw one thread
which basically seemed to be saying the reason was for binary
compataibility reasons. I didn't understand how that is applicable to
my app developed only by me for me.

Thanks for your patience.

Dianne Hackborn

unread,
Feb 4, 2009, 6:22:58 PM2/4/09
to android-...@googlegroups.com
On Wed, Feb 4, 2009 at 3:16 PM, fructose <david_p...@hotmail.com> wrote:
Ok so if developing applications in native code is not currently
supported for 3rd party developers, could Google give an indication
(including whatever disclaimers about timeframes, non committalls
etc.) about if and possibly when this could be available?

I just said this in my previous message: "there is no timeframe".
 
An understandable technical explanation of why its not possible would
be greatly appreciated by many users of these boards. I saw one thread
which basically seemed to be saying the reason was for binary
compataibility reasons. I didn't understand how that is applicable to
my app developed only by me for me.

It isn't only your app, it's your app and the system it is running on top of.

Shadakshari Hiremath

unread,
Apr 7, 2009, 3:07:26 AM4/7/09
to android-...@googlegroups.com
Hi Dianne,

I had a look into the sample PlatformLibrary code, its make file and Readme.txt.
The library implements few methods which do a specific work.

I want to add a platform library of my own which is supposed to
implement one content provider, one service and a broadcast receiver.
Is it possible to do this in cupcake? How can I inform Android
platform that it should start my provider and service on device bootup
and broadcast receiver is registered with the platform.

Readme.txt says the JAR is not associated with any AndroidManifest.xml
file. But, provider, service and broadcast receiver are supposed to be
specified in AndroidManifest.xml of the application if they are built
into application code.

BR,
Shaddu

Jean-Baptiste Queru

unread,
Apr 7, 2009, 7:12:23 AM4/7/09
to android-...@googlegroups.com
This is really a question for the android-porting group.

It really sounds like you're just looking to add an application,
though, not a (linkable) library.

JBQ

--
Jean-Baptiste M. "JBQ" Queru
Android Engineer, Google.

Questions sent directly to me that have no reason for being private
will likely get ignored or forwarded to a public forum with no further
warning.

Dianne Hackborn

unread,
Apr 7, 2009, 1:09:18 PM4/7/09
to android-...@googlegroups.com
If you want to add a content provifer, service, broadcast receiver, activity -- just put them in their own .apk that is bundled in the system.  That isn't what a shared library is for at all.  A shared library is just classes that other apps can link with to use.

On Tue, Apr 7, 2009 at 12:07 AM, Shadakshari Hiremath <sha...@gmail.com> wrote:
Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

auj012

unread,
Apr 7, 2009, 5:13:05 PM4/7/09
to android-platform
Dianne
In the sample code that you created,README.txt states that
" To declare your library to the framework, you must place a file with
a .xml
extension in the /system/etc/permissions directory with the following
contents "

I could not find /system/etc/permissions in my cupcake source code.
Do U mean the fileexplorer ? are we supposed to do a adb push on the
xml file that you mentioned or is it somehow part of the source
code ? Can we make this xml file part of source code and build it ?


Thanks

Dianne Hackborn

unread,
Apr 7, 2009, 6:34:48 PM4/7/09
to android-...@googlegroups.com
That is the directory on the device.  You should in your makefile have your xml file copied to that location.

Shadakshari Hiremath

unread,
Apr 8, 2009, 2:48:03 AM4/8/09
to android-...@googlegroups.com
I agree with the fact that we are writting an application and doesn't
really need a shared library and we need to use a staic library. We
already have written the application and added service, provider and
broadcast receiver in it and specified the same in
androidmanifest.xml.

Now we are looking at moving it inside a static library. Static
library need to contain service, provider and broadcast receiver.
Could we specify service, provider and receiver in application
manifest file which are actually part of static library and link it
with apk and how?

BR,
Shaddu

Dianne Hackborn

unread,
Apr 8, 2009, 4:26:58 AM4/8/09
to android-...@googlegroups.com
Yes, just declare them in the manifest with the correct class name.  As long as you statically like the library code in to your .apk, it will work.

auj012

unread,
Apr 8, 2009, 12:05:59 PM4/8/09
to android-platform
Dianne
Thanks for answering my question.I finally found the permissions
directory in my directory structure, but I am seeing that system/
framework does not include the platform library even though it is part
of the source code.
I have just updated the permissons.xml to include the platformlibrary,
is that enough to make the platformlibrary part of the system.img or
should I do anything else ?
Basically what are the steps to inlclude both the platformlibrary and
well as the client as part of system.img. They do not seem to be part
of the default build.
Also If I want to write my own library, what would be the right
directory structure, framework area or packages area ?
I mean this new library that I create is not a app or neither is it a
provider and it is not even a framework change, but the classes in the
library should be usable in all the above 3 places.
Please help
Thanks


On Apr 8, 3:26 am, Dianne Hackborn <hack...@android.com> wrote:
> Yes, just declare them in the manifest with the correct class name.  As long
> as you statically like the library code in to your .apk, it will work.
>
> On Tue, Apr 7, 2009 at 11:48 PM, Shadakshari Hiremath <shad...@gmail.com>wrote:
>
>
>
>
>
>
>
> > I agree with the fact that we are writting an application and doesn't
> > really need a shared library and we need to use a staic library. We
> > already have written the application and added service, provider and
> > broadcast receiver in it and specified the same in
> > androidmanifest.xml.
>
> > Now we are looking at moving it inside a static library. Static
> > library need to contain service, provider and broadcast receiver.
> > Could we specify service, provider and receiver in application
> > manifest file which are actually part of static library and link it
> > with apk and how?
>
> > BR,
> > Shaddu
>
> > On 08/04/2009, Dianne Hackborn <hack...@android.com> wrote:
> > > That is the directory on the device.  You should in your makefile have
> > your
> > > xml file copied to that location.
>
> > > On Tue, Apr 7, 2009 at 2:13 PM, auj012 <auj...@gmail.com> wrote:
>
> > >> Dianne
> > >> In the sample code that you created,README.txt states that
> > >> " To declare your library to the framework, you must place a file with
> > >> a .xml
> > >>  extension in the /system/etc/permissions directory with the following
> > >> contents "
>
> > >> I could not find /system/etc/permissions  in my cupcake source code.
> > >> Do U mean the fileexplorer ? are we supposed to do a adb push on the
> > >> xml file that you mentioned or is it somehow part of the source
> > >> code ?  Can we make this xml file part of source code and build it ?
>
> > >> Thanks
>
> > > --
> > > Dianne Hackborn
> > > Android framework engineer
> > > hack...@android.com
>
> > > Note: please don't send private questions to me, as I don't have time to
> > > provide private support, and so won't reply to such e-mails.  All such
> > > questions should be posted on public forums, where I and others can see
> > and
> > > answer them.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.- Hide quoted text -
>
> - Show quoted text -

Dianne Hackborn

unread,
Apr 8, 2009, 12:38:25 PM4/8/09
to android-...@googlegroups.com
What do you mean it should be accessible to the -framework-?  Could you please describe what you are trying to do?
hac...@android.com

auj012

unread,
Apr 8, 2009, 3:54:50 PM4/8/09
to android-platform
In our system, we want to turn features on and off depending on some
properties. For example, an app might show a diffrent screen for
product A and Product B. For this effort we are trying to create a
library with a set a properties that the app or framework will query
and then decide what needs to be done. Basically we trying to
conditional compile our code based on a certain set of properties.
This needs to be true for all areas of the code, app, provider as well
as framework.
So I am trying to create a library like your platformlibrary. Right
now I am still struggling to include platformlibrary as part of my
build.
Can you please give me the exact steps ?
Which files I should be changing to include the platformlibrary as
well as the client in my build ?
Thanks

auj012

unread,
Apr 8, 2009, 4:07:29 PM4/8/09
to android-platform
I have updated my classpath to include
PlatformLibrary/client/src, but client.apk is not part of the
generated apks. I expected to see a error while build but I did not
see it either. Not sure of this is because it is part of the samples
folder.

I also checked the /system/framework but
com.example.android.platform_library.jar not part of this folder.

auj012

unread,
Apr 14, 2009, 1:30:45 PM4/14/09
to android-platform
Dianne
I have answers to all my questions above. I was also able to create
a new application to the use the newly created custom library.
Can a ContentProvider, say Downloads or Contacts provider use the
custom library ?
I updated both the Android.mk and AndroidManifest of the testcontent
provider but I am gettig a error.
platformlibrary* does not exist.
Is it even possible to use the custom library in the content
provider ?
Thanks

Dianne Hackborn

unread,
Apr 14, 2009, 1:52:25 PM4/14/09
to android-...@googlegroups.com
Any .apk can use a shared library, regardless of what components it has.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Kartic

unread,
May 5, 2009, 8:01:22 AM5/5/09
to android-platform
Hi,

I am trying to do something similar...
I am able to add my jar file and jni shared object file to the system
image but a client apk file built on same lines as that explained in
this example is not getting generated.

I have verified this by looking at installed-files.txt.

Though I am able to generate my application using make vsClient where
vsClient is the LOCAL_MODULE var defined in the Android.mk file
and then I can install it to running emulator or device using adb
install but I want it to be integrated in the system image.

Is there any other step which needs to be done to generate and include
an apk file in automatically system image.

regards,
kartic

Dianne Hackborn

unread,
May 5, 2009, 12:36:44 PM5/5/09
to android-...@googlegroups.com
The sample code referenced at the start of this discussion is a complete working example of how to do this.
hac...@android.com

Kartic

unread,
May 6, 2009, 12:33:56 AM5/6/09
to android-platform
Hi Dianne,

I have tried working with the sample code also, in that also I am
facing similar problem.

The jar file and so file for JNI are being built and included in
system image but apk file for client is not getting built.

- Kartic

Dianne Hackborn

unread,
May 6, 2009, 2:26:02 AM5/6/09
to android-...@googlegroups.com
Did you follow the directions to add the permissions file?  I don't understand what you mean about the client .apk being built, it is just an apk, there is nothing special about building it except it linking with the library.
hac...@android.com

Kartic

unread,
May 6, 2009, 2:39:34 AM5/6/09
to android-platform
Yes I have done that, by client.apk not being built I mean I am not
able to find it along with other apk files like AlarmClock, Phone etc
at out/target/product/generic/system/app.

Also there is no entry for Client.apk in installed-files.txt.

I can build it by giving make PlatformLibraryClient but I want it to
be built and included in the image automatically.

I think I am missing out something very basic, it would be great if
you can help me in resolving this.

Thanks,
Kartic

Dianne Hackborn

unread,
May 6, 2009, 2:44:14 AM5/6/09
to android-...@googlegroups.com
Sorry I don't know enough about what you are doing to help.  I would suggest looking at a makefile of an .apk that is being built as you want as a guide for what to do.
hac...@android.com

Kartic

unread,
May 6, 2009, 2:48:11 AM5/6/09
to android-platform
Ok, let me rephrase the question, this might be little off topic...
I want to add any application to system image, do i have to do
anything special to do this..

I have added folder for my application to packages/apps
written a make file and manifest file similar to those present in
other applications in packages/apps

So now if apps like AlarmClock etc get built and included in system
image then my application should also get included in system image.

Am I missing some step over here?

Kartic

michae...@gmail.com

unread,
May 5, 2009, 5:16:31 PM5/5/09
to android-platform
Good
Reply all
Reply to author
Forward
0 new messages