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

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。

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.

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.

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.
Thanks

Related

Android studio -- how to include resource file in Java package using gradle

I have an android project which requires to include a non-java resource file to included in the final package with Java class.
If you are using Eclipse, it is very easy, just put the non-Java resource file in the same package of Java classes, Eclipse will automatically copy the resource file to the destination.
I tried the same method in Android Studio, which uses Gradle for building, but it doesn't pack the resource file in the final Java class package. I have no control over how to read the resource file, it must use class.getResourceAsStream() to read the resource file.
Is there any way to pack resource file in Java package in the final product? Any suggestion is much appreciated.
First, Create a java resources folder.
Second, Create the same structure with a package name of a class.
for example, picture blow, you will have the same folder structure if you have a Tobee.class in a com.tobee package.
You can read a file in a Tobee class by calling a getResource or getResourceAsStream method.
getClass().getResourceAsStream("jar_properties");
Alright, I have solved the issue: pack everything in a jar including resource files then import this jar into the Android project.

Android Library project building jar with resources using ant

Android project referencing to multiple libraries, with resource files.
I want to generate a jar with resource files from the library project and want to use it in
main android project, am not using eclipse, I want this to be done through ant .
Is it possible, because I checked few sites, where they have given to delete the src
and distributing it as zip.
If its possible please give me the process how to go with building through ant.
As far I know this is not possible. Android does not package xml files or drawables in jar files and you cannot access them from another project. You could look at *.aar files, which were introduced last year. But it only works with the Gradle system and Android Studio. It can contain xml and drawable resources from your project.

Android load resource file from library project

I have one android library project, there I need to load string from the strings.xml, how to do that. In normal android project we will use getResources().getString(R.string.protocol). I need similar kind of way to access the resources.
You can't access main project's resources from a library project.
You can access library project's resources the same way you access project's own resources from main project.

Android how to export jar with resources?

I create an Android project for build my custom widget, and I want to export it as a jar for other project so that reuse them conveniently, but now a problem is, I can't use resources which is included in my custom widget jar, for example a png picture, it gets a null from the resource Id of class R! How can I sovle it?
Update question:
How can I parse LayerDrawable xml from assets folder in code?
You cannot export self-contained Android Library jar at this time and announced will be available in the future SDK release.
https://developer.android.com/guide/developing/projects/index.html#LibraryProjects
You cannot export the Android Project to Jar with Resources. What you can do is Make First Project as Library and then Add that to another project.
See Documentation Here shows how to Setup a Project as Library and use in another Project
In order to get the app working with my exported library .jar file, I had to copy everything in the resources directory "res" from the library project to the application project. Also had to merge file string.xml as well as merge the ApplicationManfiest.xml files as well. To add the project as a library through eclipse is much simpler.

How to include Java resources from a JAR in my Android APK?

I have an Android project that depends on a non-Android JAR that contains resources (Java resources, not Android resources), which classes within the JAR need to load. When running the application, these resources are not found (i.e., ClassLoader.getResourceAsStream() fails), apparently because the resources are not being included in the APK.
I found some discussion here:
http://code.google.com/p/android/issues/detail?id=10076#c7
But I need to build the APK in Eclipse. Short of doing a command-line build with a deprecated tool (ugh), or duplicating all the resources (ugh), how can I make it work?
Create your jar file with the classes you need and save it your computer. Then in the project explorer right click the project and go to properties -> Java Build Path -> Libraries. Now import the jar file.
You should now have full access to the classes and methods in your code and the jar file will be installed with your APK. My guess is you have utility classes and when you are calling them in your source Eclipse is importing them from another project.
The solution I have given works for sure (I do it myself).

Categories

Resources