开发者

Android - Is it possible to create a custom library to use across several applications?

开发者 https://www.devze.com 2022-12-12 20:06 出处:网络
Is it possible to create a custom library in Android (having its own layout resources) for use across several Android applications?

Is it possible to create a custom library in Android (having its own layout resources) for use across several Android applications?

  • I created a regular *.jar file but when I tried to create/style my views dynamically most of the properties do not work. Even referencing simple styles from the android.jar file such as android.attr.listSeparatorTextViewStyle did not work.

  • I created an Android project without any Activity, having its own resource files and t开发者_运维知识库hen referenced this project from another Android project to use in its build path. Everything seems to work fine but the emulator keeps crashing (with no meaningful error message in the LogCat) when I try to run the project.

Am I missing something?


Might be late to reply but found this which is interesting. Setting up a Library Project

An Android library project is a development project that holds shared Android source code and resources. Other Android application projects can reference the library project and, at build time, include its compiled sources in their .apk files. Multiple application projects can reference the same library project and any single application project can reference multiple library projects.

If you have source code and resources that are common to multiple application projects, you can move them to a library project so that it is easier to maintain across applications and versions. Here are some common scenarios in which you could make use of library projects:


Well, I think there is a way to do it (although I didn't try it myself), but it requires the users of your library having to build their APKs manually via aapt.

The problem is this: The JAR you're exporting contains the R class file which holds the resource IDs of your library. So, any application can access these IDs by simply linking your JAR. However, that's not enough, because by default, only resources in the app's res/ folder are bundled with the APK. Hence, you have to create the APK yourself using aapt, telling it to also include any resources from your library. Check the aapt tool help for more information.

Since this implies that users of your library have to introduce a manual build process (or at least modify it), it makes for a poor experience.


Is it possible to create a custom library in android (having its own layout resources) for use across several android applications?

No. JARs cannot have resources. You can share code this way, but resources have to reside in the applications reusing the JARs.

Even referencing simple styles from the android.jar file such as android.attr.listSeparatorTextViewStyle did not work.

Make sure you are linking your JAR against android.jar. You can see many examples of this in the CWAC components on my github page.


Is this still true with R21? I am able to export a library project into jar file and used in other customer projects successfully, both code wise and accessing assets file wise.

yet http://developer.android.com/tools/projects/index.html still has the following statement which seems not true anymore:

You cannot export a library project to a JAR file A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools.

0

精彩评论

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