开发者

How reusable Android libraries operate (development, deployment etc)

I am trying to create reusable library for my Android project. The library will include resources. Unfortunately, I can't find any decent explanation on how Android libraries operate.

Android reference just says how to set up Eclipse project and Ant project. I am using Maven and IntelliJ. And I would figure how to set them up if I could find any suitable description on how all pieces work together.

So I wonder if someone wants to take a pick and try to explain it. And here are some of my open questions.

Lets say I have project AndroidApp which has AndroidManifest.xml with package app.foo and produces AndroidApp.apk.

I also have project AndroidLib with package app.bar. What do I need in the AndroidManifest.xml for AndroidLib? Is just declaring package sufficient or there should be something else?

Now, I want to have resources in both and refer to resources in AndroidLib from AndroidApp. Do I just call lib.bar.R? Some posts suggest that there will be no id conflicts. True or false?

What is deliverable artifact for AndroidLib? Is it also apk? How do I deploy bo开发者_JAVA技巧th? Or more importantly, can I produce single apk that would include AndroidApp and AndroidLib?

How do projects relate to each other in development environment? For example, in IntelliJ I tried to just include module with AndroidLib in AndroidApp but it just compiled resources into AndroidApp. (Possibly I missed uses-libary). If I would use javac and Android tools, what would be the sequence?

Anything else worth knowing?

Anyway. I'd appreciate if someone could share there knowledge base on that matter.


I have written a detailed post about this: Android Application, Android Libraries and Jar Libraries.

If you want to mimic the way original Android build system behaves, then the best way is to run

ant debug -d

on a project that uses Android Library ( or Android libraries ).

This will give you all command lines ant runs to create R.java files, compile code and package the final .apk file. This is basically the best reference you can find right now. Though note that Android's build system is evolving and you'll need to keep your build system in sync with the original one.


Ok, I'm no expert in this, but these are my thoughts (which could be wrong):

As far as I can say, there is strictly speaking not really such a thing as "Libraries" on Android. There are basically three options:

  1. Let the "Lib" project be a "normal" Java project and add that project to the build path of the "App" project. The "Lib" can then only contain Java code, which is compiled and added normally to the "App"s APK. Since an app running on a device can't access another app's code, if you'd want to reuse the "Lib" in second Android app, it would have to include a copy of the (compiled) lib code in its APK. This also means the lib cannot contain any resources, because they need to be in the corresponding directories in the "App" project.

  2. Similar to #1, but you have the Lib code compile into a regular Java library (jar), which you copy/compile into the lib directory of the "App" project. Other than that it would have the same limitations as #1, so that any app you reate would have to include a copy of the JAR in it's APK.

  3. Have the "Lib" project be a full grown stand alone app and define an API using custom Intents have have all communication between "Lib" and "App" run over them. This requires the "Lib" and "App" to be distributed separately, so that a user will need to manually install both. However this allows you to create additional apps that can reuse the functionality of the "Lib" app without including duplicate code.


I have recently merged in the library projects support in the samples project for the Maven Android Plugin. Just see how it all works by checking out how they are set up.

https://github.com/jayway/maven-android-plugin-samples

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜