tags:

views:

260

answers:

3

Hi,

I have developed some reusable android component which is basically a class . This class has some resource dependencies e.g. some png drawables, some xml layouts etc. So this class referenced the auto-generated R file.I would like to distribute this code in a single package like jar file to other developers for use in their applications.

I have read that the only possible solution is to distribute code together with all my resources, which others have to copy to their "res" folder (source).

So I created a jar file having the class file (say MyClass which is in the package com.xyz.android.app) and resources and tried to use this in my new application.

So I added the jar file to my new applications build path using add external jars option in eclipse and copied all the resources to my new application's res folder. (The activity class say MainActivity of my new application is in com.abc.myapplication package, just for the case if it may helpful)

But when I run this new application there is java.lang.ClassCastException in the MyClass class. I tried to debug the application and then I found that in the MyClass class, there is "R cannot be resolved" problem.

Then I changed MainActivity's package to com.xyz.android.app (which is not the way, other developers will be happy to do), But again the same problem.

But When I just copy the source java file such that both MainActivity.java and MyClass.java are in com.xyz.android.app package then application runs fine.

So if I need to distribute such that other users need not to bother these package naming things, how can I accomplish this? Please help !!

Edit

In android.jar, there are also some resources. How are they referenced in a project? How are they used in android classes? There is also android.R file?

Is it not possible to do the same thing i.e. to make jar file like android.jar for my reusable code?

+3  A: 

This is not so easy to do at the moment. The problem is the project using your jar does not know to look in there for drawables etc. The good news is it should soon be possible (hopefully with SDK 2.2 which is rumoured to be released at IO next week). See this blog post http://mobilebytes.wordpress.com/2010/04/27/android-tools-version6-coming/

Nic Strong
Quoting Google on this subject: "Yes, library projects are mostly for you as a developer to share your own code across your applications. This is not about redistributing reusable components, unless you're OK with sending (or posting somewhere) a zip file of your library with full source."
CommonsWare
I already assumed that the new project does not know to look in the jar for resources. So I have already copied the resources in the new project (as on http://stackoverflow.com/questions/1995004/packaging-android-resource-files-within-a-distributable-jar-file/2825174#2825174) . now I want my new project to work. So I want to know is it possible? and if yes how?
Kaillash
+2  A: 

As Nic Strong noted, there is nothing much built into the project to manage this, and current indications are that the upcoming changes to the SDK tools may only help a bit.

I am organizing some other tools to help deal with this problem. I hope to have developer documentation published in a few days.

CommonsWare
Sounds promising. I will check it out!
Nic Strong
In android.jar, there are also some resources. How are they referenced in a project? How are they used in android classes? There is also android.R file?
Kaillash
The "android.R file" is built into the operating system. You do not need to distribute it, and the numeric values will not be changing. So, that is safe to use without any additional work.
CommonsWare
A: 

I've been playing with Mark Murphy's ParcelHelper class at http://github.com/commonsguy/cwac-parcel. In a nutshell, it's a collection of methods that let you access the components of 'R' by name without needing to import 'R' into your code.

If I understand your question right, this is exactly what you want. You may still need to copy some resources, such as styleables, into your project.

Edward Falk
Thanks for response. at least, till Google does not provide any solution to this, What I need a way to distribute the code.
Kaillash