- 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. ?
Asked
Active
Viewed 5,665 times
10

Cœur
- 37,241
- 25
- 195
- 267

chaitanyad
- 998
- 1
- 9
- 27
-
6if someone downvotes, please have the courtesy to say what went wrong. My question is wrong? This is not the right forum to ask this question? Didn't understand the language of the question ? Or the person downvoting is just a sadist. – chaitanyad Dec 12 '14 at 10:51
-
http://stackoverflow.com/q/17213236/1531054 – S.D. Dec 24 '14 at 11:47
1 Answers
10
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 :

Community
- 1
- 1

Shivam Verma
- 7,973
- 3
- 26
- 34
-
This is not true anymore: https://stackoverflow.com/a/22254488/3332634 – yshahak Sep 21 '22 at 11:12