jake
jake

Reputation: 41

How to build an Android project to jar that included other project

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。

Upvotes: 0

Views: 70

Answers (3)

Amit Gupta
Amit Gupta

Reputation: 8939

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

Upvotes: 0

Yasitha Waduge
Yasitha Waduge

Reputation: 13540

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.

Upvotes: 0

Steven Schoen
Steven Schoen

Reputation: 4376

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.

Upvotes: 1

Related Questions