Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android accessing assets form library project

Tags:

android

  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. ?
like image 494
chaitanyad Avatar asked Dec 12 '14 10:12

chaitanyad


People also ask

How do I access Android assets?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Right click app >> New >> Folder >> Assets folder.

How do I access an asset file?

To view asset files in Files home, select Libraries and then select Asset Library . To view and edit asset files in Setup, enter Asset Files in the Quick Find box, then select Asset Files.

Where are Android assets?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.


1 Answers

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

like image 87
Shivam Verma Avatar answered Oct 19 '22 03:10

Shivam Verma