The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | Platform Library Example |
| 2 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 3 | |
| 4 | |
| 5 | This directory contains a full example of writing your own Android platform |
| 6 | shared library, without changing the Android framework. It also shows how to |
| 7 | write JNI code for incorporating native code into the library, and a client |
| 8 | application that uses the library. |
| 9 | |
| 10 | This example is ONLY for people working with the open source platform to |
| 11 | create a system image that will be delivered on a device which will include |
| 12 | a custom library as shown here. It can not be used to create a third party |
| 13 | shared library, which is not currently supported in Android. |
| 14 | |
| 15 | To declare your library to the framework, you must place a file with a .xml |
| 16 | extension in the /system/etc/permissions directory with the following contents: |
| 17 | |
| 18 | <?xml version="1.0" encoding="utf-8"?> |
| 19 | <permissions> |
| 20 | <library name="com.example.android.platform_library" |
| 21 | file="/system/framework/com.example.android.platform_library.jar"/> |
| 22 | </permissions> |
| 23 | |
| 24 | There are three major parts of this example, supplying three distinct |
| 25 | build targets and corresponding build outputs: |
| 26 | |
| 27 | |
| 28 | com.example.android.platform_library |
| 29 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 30 | |
| 31 | The top-level Android.mk defines the rules to build the shared library itself, |
| 32 | whose target is "com.example.android.platform_library". The code for this |
| 33 | library lives under java/. |
| 34 | |
| 35 | Note that the product for this library is a raw .jar file, NOT a .apk, which |
| 36 | means there is no manifest or resources associated with the library. |
| 37 | Unfortunately this means that if you need any resources for the library, such |
| 38 | as drawables or layout files, you will need to add these to the core framework |
| 39 | resources under frameworks/base/res. Please make sure when doing this that |
| 40 | you do not make any of these resources public, they should not become part of |
| 41 | the Android API. In the future we will allow shared libraries to have their |
| 42 | own resources. |
| 43 | |
| 44 | Other than that, the library is very straight-forward, and you can write |
| 45 | basically whatever code you want. You can also put code in other Java |
| 46 | namespaces -- the namespace given in the <library> tag above is just the |
| 47 | public unique name by which clients will link to your library, but once this |
| 48 | link happens all of the Java namespaces in that library will be available |
| 49 | to the client. |
| 50 | |
| 51 | |
| 52 | libplatform_library_jni |
| 53 | ~~~~~~~~~~~~~~~~~~~~~~~ |
| 54 | |
| 55 | This is an optional example of how to write JNI code associated with a |
| 56 | shared library. This code lives under jni/. The jni/Android.mk file defines |
| 57 | the rules for building the final .so in which the code lives. This example |
| 58 | provides everything needed to hook up the native code with the Java library |
| 59 | and call through to it, plus a very simple JNI call. |
| 60 | |
| 61 | |
| 62 | PlatformLibraryClient |
| 63 | ~~~~~~~~~~~~~~~~~~~~~ |
| 64 | |
| 65 | This shows an example of how you can write client applications for your new |
| 66 | shared library. This code lives under client/. Note that the example is |
| 67 | simply a regular Android .apk, like all of the other .apks created by the |
| 68 | build system. The only two special things needed to use your library are: |
| 69 | |
| 70 | - A LOCAL_JAVA_LIBRARIES line in the Android.mk to have the build system link |
| 71 | against your shared library. |
| 72 | |
| 73 | - A <uses-library> line in the AndroidManifest.xml to have the runtime load |
| 74 | your library into the application. |