android accessing assets form library project - android

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

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.

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.

Exporting Android Library project with resource folder images

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.

Android intellij IDEA 11 merge assets directory when using library modules

I have an Android project(A) that uses one library module(B) in Intellij IDEA 11. In the main module(A) I have a database file into assets directory and it's working fine.
Let's name the project modules like this: Main module (Module A) and secondary module(Module B).
Now I have to create a database for the library module(B) too and I added the database file into B assets directory because I thought that after compiling the project will be formed of one project having merged the module A and B. But the problem is that the B's database is not copied into the project assets directory.
Is there a way to tell Intellij to merge the assets directory too?
PS: I also have different resources in B's res directory and the application is working properly...
Thank you!
I am not familiar with IntelliJ. But I think this is more related to the Android SDK constraints, other than a config setting from IDE. as it is stated in the official dev guide here:
Library projects cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.
You should not use assets folder in library project, instead, put all db files into your main project's assets folder.
IntelliJ IDEA 11.1 already supports copying assets from Maven apklib dependencies and Android library modules in usual projects. http://youtrack.jetbrains.com/issue/IDEA-80771
You can define an artifact for the project settings for that.
To do so: Right click the Module B project then choose "Open Module Settings". Select Artifacts and press + button to define a new one. Choose the "Other" type and for "Output Layout" select "Add copy of File".

android: Adding external JAR file in to android

We are making an application that needs to convert one file format to another.
I am not sure whether we can add a JAR file in which we develop the code to convert the file format to our required file format, to our android project.
You should be able to create a folder called libs in your main project directory and add any jar that you want to use to this folder. If during testing on the emulator you get a class not found exception or something like that you may add the jar in the order and export tab of your build path configuration.

Categories

Resources