chaitanyad
chaitanyad

Reputation: 1007

android accessing assets form library project

  1. Created a library project with some reusable code from my production project.
  2. The library code used a json file from assets folder of my production project.
  3. 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.
  4. 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.
  5. After the file being moved to the library project, while reading the json file, it throws file not found exception.
  6. 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.
  7. The purpose of the library is defeated if the json file is not packaged with it.
  8. How do I get context such that I can read asset file from the library project. ?

Upvotes: 10

Views: 6434

Answers (1)

Shivam Verma
Shivam Verma

Reputation: 8023

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

Upvotes: 10

Related Questions