Have you ever felt frustrated while trying to package custom Android UI controls into .jar files in order to reference them in another project? While doing this with functionality that is resources-free shouldn’t be a hassle, redistributing a library with components using resources such as images, XML for the layouts and styles may become a serious challenge. The reason most developers get stuck here comes from the .jar specifics: .jar files do not support resources, they contain bytecode only.

The good news is that if you target Android Studio users only, you have a smart and elegant solution at your disposal. Android Studio with Gradle introduces a new Project Library redistributable called .aar, which is basically a ZIP archive containing the corresponding .jar library and all associated resources. When referenced by another Android Studio project the build system easily resolves all resource references and things work like a charm.

Targeting Eclipse users may become a challenge though, as Eclipse supports .jar libraries only. The good news is that there is an easy workaround which I’ll share in this post. We’ve stumbled upon this issue while building our native Android Charting library and we’ve figured that our solution might save you some serious headache.

Now, suppose that you are developing a custom view which you want to share in different projects or with other developers as a library. Also suppose, that this custom view extensively uses styles, image resources, and other XML defined values that are automatically resolved by the Android framework according to the screen size you’re running at the moment. Here’s an example:

I’ve prepared a very simple Android Library Project in Eclipse which defines a class called MyCustomView that inherits from View and adds several fields like backgroundColor (yes, I know View delivers this as setBackground but for the sake of the sample…), strokeColor and strokeWidth. I have also defined a style infrastructure for this view by describing its stylable attributes and defining a default style:

    <declare-styleable name="MyCustomView">
        <attr name="backgroundColor" format="color"/>
        <attr name="strokeColor" format="color"/>
        <attr name="strokeWidth" format="dimension"/>
<style name="MyCustomViewStyle">
   <item name="backgroundColor">#ff004453</item>
   <item name="strokeColor">#ff542331</item>
   <item name="strokeWidth">10dp</item>

I have also created another project in the same Eclipse workspace, referenced my library and added an instance of MyCustomView in my main activity:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

The result of running the application is predictable: getting the whole main activity screen estate coloured and stroked with the default values for background color and stroke color of for MyCustomView.

So what if I want now to have a redistributable package of this custom view that contains both a compiled .jar and all of the needed resources? What we did to solve this problem was actually quite simple: reuse the existing Android Library project by stripping out the source code of the custom view and embedding the already built .jar file as a dependency within the project. As you know, all other projects referencing it will automatically get this .jar as a dependency as well:

Now you can simply ZIP the whole CustomViewLibrary along with the embedded .jar and share it with anyone.

Related Posts


Comments are disabled in preview mode.