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 then 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?
An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.
Create a new project to make your library in. Click File > New > New Module > Android Library > Next > (choose name) > Finish. Then add whatever classes and resourced you want to your library. When you build the module an AAR file will be created.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With