2

Here is what I'm trying to do:

I want to create a JAR to be included in Android apps that contains views and images. As far as I know, you can't package the res folders in a JAR. So what I am looking to do is convert the XML files in my res folder to an Android view class, which I can then compile as part of the project into the JAR.

Are there any tools or projects which have done this successfully? Alternative solutions are also welcome.

drhodes
  • 41
  • 6
  • 1
    How about just referencing it as a library? Right click the project in Eclipse and hit properties, then click android and select the checkbox for `is library`. Now in other projects you could go to their properties and add the library as a reference. – chRyNaN Apr 23 '13 at 23:50
  • "I want to create a JAR to be included in Android apps that contains views and images" -- since there is no practical way to convert an image into Java code, I echo the other responders and recommend an Android library project: http://developer.android.com/tools/projects/index.html#LibraryProjects – CommonsWare Apr 23 '13 at 23:56
  • Currently doing this, although I would prefer to distribute it as a JAR. – drhodes Apr 23 '13 at 23:58
  • 1
    What is your specific problem with an Android library project that leads you to "prefer to distribute it as a JAR"? If it is a question of distributing Java source code, an Android library project does not require Java source code for distribution. – CommonsWare Apr 24 '13 at 00:05
  • @CommonsWare Can you elaborate or link to somewhere which explains how to distribute a library project without Java source? Since resources are referenced through generated source (i.e. the `R` class), wouldn't all code that uses those images need to be in source form in that project? – kabuko Apr 24 '13 at 00:35
  • @kabuko: No. If nothing else, you can use the `getIdentifier()` approach to look up the `R` values at runtime. Plus, I think the way `R` is generated for library projects now, you don't even need that, though I'd have to run an experiment to confirm that (another item for my todo list...). But, as an example, Maps V2 (and the rest of the Play Service SDK) is distributed as a JAR-based Android library project. – CommonsWare Apr 24 '13 at 00:39
  • @CommonsWare Thanks, this will have to go on my list of things to investigate too. – kabuko Apr 24 '13 at 20:55

3 Answers3

1

You can make every view in it dynamically and set the appropriate setting- every setting in xml has a matching Java call to change. That doesn't fix anything other than the layouts folder though. It would be far easier to give them the code as an Android library project.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Do this:

public class MyView extends View{
    private TextView tv; //or other elements in the layout you want
    //here also has constructors
    @Override
    public void onDraw(Canvas canvas){
        //draw the views that you prepared in your constructor
    }
}

If you extend LinearLayout it would be LinearLayout in XML, extends RelativeLayout then be RelativeLayout etc. All these layouts are subclasses of View.

StoneBird
  • 1,900
  • 13
  • 12
0

I had this exact issue: wanting to distribute my library as a jar file. A reasonable number of support issues were caused by people just not being familiar with the library project bits (shocking to me, but who am I to argue).

First, there was really no support for automatically generating java classes from XML layouts. I'd thought that I could perhaps extract the source code used by the ADT to render your XML layouts into separate classes, but after futzing around with that for a couple of hours I gave up and wrote the layouts out by hand. Make them extend RelativeLayout and build the hierarchy in code. Works, but doesn't quite scale. At some point it may be worth digging into the ADT source to allow you to export a layout.

For images I restricted it to just icons, then wrote an ugly little tool that would generate a byte[] array from the PNG files and then adds that as a static final variable in a separate class for each image: you're limited to small images, I think less than 40k or some unknown part of the Android build process craps out because the generated class files are too large. Once that is done I can reconstitute the images in my view classes using code like this:

audio_icon = new BitmapDrawable(BitmapFactory.decodeByteArray(AudioIcon.bytes, 0, AudioIcon.bytes.length));

I have a package for all the icons, and it actually worked pretty well. You do give up the customization options offered by the library format, but it has made our integration process real simple.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I need to distribute some things as a jar as well. What I did for the images was to [load them as jar resources](http://stackoverflow.com/questions/574809/load-a-resource-contained-in-a-jar) (not Android resources). I suppose it's possible changes to the Android tool chain could break this, but it's worked so far. – kabuko Apr 24 '13 at 00:37