0

How can I build one Android project to jar that is can included in other android project? When I use eclipse export an Android project to jar and use the jar to other android project ,The "R" file and the resource files will be error。

3 Answers 3

1

Android resource files (which are represented in your R.java files) cannot be put into Jars. That is why libraries such as ActionBarSherlock require to be opened as library projects, instead of simple Jars.

Sign up to request clarification or add additional context in comments.

Comments

0

You can build Android Library Project to jar, but within library project you should prevent using R class, Instead of R class you can use reflection to load resources.

For example if you need to load string from strings.xml file

int id = context.getResources().getIdentifier("string_name", "string", context.getPackageName()); String value = context.getString(id); 

But this is what they mentioned about getIdentifier method:

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name. 

2 Comments

Thanks,I know that mothod , But if my project is bigger , I will change my code that is very hard。
Oh! then only option is to use your project as a library project. :(
0

You can build Android Project to jar file but don't use any Resource file. B'coz when you will try to use that jar file in your application at run time Resource not Found exception will come.

So while creating jar file please unchecked res,gen,Manifest, etc. You can Define your Base Activity class, Base Interface, Database Manager, GPS Manager in your library project and use it in your our application it will surely work.

So see the below screen shots.

enter image description here

Thanks

2 Comments

Thanks,Now I have an project already that can run independent。 There will have an other project use that project or include that project,so I want export my project to jar 。But The R file of those two projects has same id like A Project has public static final int activity_main=0x7f030000; B Project alse has public static final int activity_main=0x7f030000;
@jake Don't use any Resource in your library project like Drawable,layout etc. Supply any resources from your main project. B'coz R.java conflict occur between library and your main project. So avoid using Resources in library project to overcome R.java conflicting issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.