Exporting Android Library project with resource folder images - android

I am new to Android. I am using the reusable code, so I convert that to JAR file. I am using images from JAR source res folder. I have accessed those images by "R.drawable.imagename" in library source. While I am converting the library source to JAR, I have export those res folder. If I am using the JAR in another android application, that will be crashed. I have followed the link below to convert the library source to JAR.
"http://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/jarInEclipse.htm".
Where I am doing things wrong.? Is there any another procedure to convert the android library project to JAR with resources.? or Is there any wrong in accessing the res folder image in android library source.?
Please help.

UPDATE:
The new Gradle based build system supports "aar" files which , apart from compiled code, can contain resources etc as well.
OLD:
Resource packaging in library projects is not supported by android build tools. You will have to provide a separate zip for res folder along with the jar file.
Code goes in jar (for library) or dex (for app). Resources are separately bundled into apk file. Hence the SDK resource packager tool works only when you are exporting an apk file.
Yes, a jar file can contain resources, but, dependent android project's won't look up in jar file for resources.

Related

How to make a library containing native code and pure java for android

I have cross compiled the native code for different architectures for android and have its corresponding jni wrapper class working in android studio. Now I want to make it available as a library. I have two questions:
1)Can I just jar the .so file and jni files to make the library?(not .aar because my library contains only the pure java and native code, no android related stuff such as .xml,res,etc) If so what should be its structure?
2) If I cannot create .jar file, then all I am left with is creating the .aar file. That would include the use of android studio. Can I do it without the android studio?
Edit 1:
If I jar the files, then I don't want to extract .so and then use it. If I jar the files, then add it(the .jar) as a dependency in my project then it should automatically put .so into libs/jni folder in my apk so that jni can find it. Question is how do I do that?(if possible)
Fortunately found a way. As .aar is itself a simple zip file with structure as specified here. What I did is "jar" all the .class files (inside proper package folder) into classes.jar, created an empty folder "res", put all .so files in jni folder (with proper architecture) and made a simple AndroidManifest.xml containing basic tags. Then I jar them into file with extension .aar (like mylib.aar). I have tested it by copying .aar into another project's libs folder and putting a dependency on it in app's build.gradle( like compile(name:'mylib',ext:'aar')). And it works great.

android accessing assets form library project

Created a library project with some reusable code from my production project.
The library code used a json file from assets folder of my production project.
To read the asset folder files we need context. So while initializing an instance of the library project from production project i pass its context.
Now, that all code is working fine, I want to move the json file from my production project to the library project, because that file is not independent of the library and vice versa.
After the file being moved to the library project, while reading the json file, it throws file not found exception.
That is probably because the context that i pass to the library project is of my production project, whereas the file is now in library project's asset folder.
The purpose of the library is defeated if the json file is not packaged with it.
How do I get context such that I can read asset file from the library project. ?
From Android Docs : http://developer.android.com/tools/projects/index.html
Library modules cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library module. Any asset resources used by an application must be stored in the assets/ directory of the application module itself. However, resource files saved in the res/ directory are supported.
And this is how you can read a JSON from res/raw folder : JSON Parsing in android - from resource folder
Other workarounds :
how to reference an asset in a library 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.

Use already developed android project [with its resources(xml, images, layout)] into another project directly as .xyz file

I have read lots of questions on this site and come to the decision that if you wish to use your already developed code with its resources in android then you have to use it as a library.
But from the Building Android applications with Gradle tutorial I read something like...
Gradle supports a format called Android ARchive (AAR) . An AAR is similar to a JAR file, but it can contain resources as well as compiled bytecode. This allows that an AAR file is included similar to a JAR file**.
Does it means that we can use .aar file as an .jar file but with facility of using resources also?
Then I have tried to crate .aar file with the help of Android Studio, but .aar file doesn't contain layout XMLs or images -- it contains some layout and resources but it doesn't contain projects other resources file.
At last I am having the only same, annoying, stupid question: Can we use whole project with its resources with only one file like .jar or .aar or any other file format?
RajaReddy is quite mistaken. The JAR contains only code; you cannot access resources that way.
Google distributes their own "Google Play Services Library" as an Android library project, containing the binary code in a JAR file in the lib directory, the resources in the res directory, and an UnusedStub class in the src directory. If a better approach were viable yet I think they'd be using it.
UPDATE: While Android Studio is still in beta, it includes (buggy) support for AAR files. Seems this will eventually be the way to go.
Library projects bin folder contains jar file, copy that jar file in your main Application ( project ) libs folder we can get all the resource folders like this.
Follow these steps !
1) make your library project a normal project by deselecting IsLibrary flag.
2) Execute your project as Android Application. (It will not show any error)
3) you'll find a .jar file in bin folder..
4) Copy .jar in libs folder of your main application.
this will works fine with all the resources.
I was looking for the same thing for years. Combining byte code of java and resources (xml and other files) into one package. Currently I don't think its possible because even google has to include add resources separately in google play services lib available in the SDK .
What you can do best is generate a .aar or .jar file and add a folder of missing resource files.

Android .jar file requires referenced project to be opened

I'm very confused about adding external .jar files to my project.
I downloaded https://github.com/chrisbanes/Android-PullToRefresh library project.
After importing it to Eclipse, I cleaned & built it.
After that, in the bin/ folder of the library project, there is a .jar file.
Next I copied this .jar file to the /libs directory of my project from which I want to reference it.
As far as I understood, from now on there is no need to keep the library project in my workspace, because i added the .jar file to my project.
Picture of the projects tree:
https://www.dropbox.com/s/7w4tzis8v1vv4aj/jarProblem.png
When I'm running the referencing project with the library project open : no issues.
As soon as i close the library project: NullPointerException <- seems not to find the library.
I already tried every possible combination of build path configuration but I can't get my main project to be built with the pulltorefresh project closed.
I thought it was the sense of a .jar file to distribute a library without having to attach the source files.
It should work the same as the android-support-v4.jar ... for which i don't have the source code either.
What am I missing here ? Thank you.
There's a difference between a jar file and a library project.
Jar files can contain only code, no android resources. These just need to be placed in your libs folder and linked in.
Library projects can contain Android resources. They need to be compiled as Android libraries in Eclipse, and kept around as their own project. You then tell your main project that it references this one. Its necessary to keep the library project around because it needs to build the /gen files from it for your resources to be right. It also needs to be able to grab the contents of its assets folder.
Given that this library has a res folder, its an Android library project and can not be used as a simple jar.

Categories

Resources