2

I'm writing an android library and at the same time a demo app that will use my library, so far I'm writing the contents of each one on the same project but on separated packages so that after finishing I can transform the library package into a .jar . I need to store a huge amount of ininial data inside the library package and that amount of data must be available to anyone using the library. I've been doing some research and I decided to store this initial data in a .json file and read that info when needed. I've been trying to look for the best place to store the json file and the two sugested places were the /res/raw or /assets folders of my android studio project. The thing is this folders are located in the part of the project that belong to the demo app and not inside the package that will become the library. My question are:

  1. Is there a way to place the .json file outside the /res/raw or /assets folders, lets say within a random package together with some .java files?
  2. I tried storing the initialization data inside .java files and it worked for a while but then I started having problems due to java limitations with the amount of data that can be written inside a method, so I selected a .json file as the place to store the data. Should I select any other kind of file, let's say one that I could place and read from wherever I want if such thing is possible?
  3. I'm I forced to separate the library from the demo into two different projects?
VMMF
  • 906
  • 1
  • 17
  • 28

1 Answers1

3

Assuming that you created a library using the File > New Module > Android Library option, your library should have its own resources folder. Look at this picture:

enter image description here

Here I created the mylibrary Android library using the process described above. Then I manually created a raw folder under mylibrary/src/main/res & after adding the appropriate Gradle dependencies I was able to access the cover.jpg file that I put there in raw in MainActivity which belongs to a separate project. So to answer your question you could place your JSON file in yourlibary/src/main/res/raw in your case. It's still an Android library although you could say it's a different project, since it is in a way.

Although this is indirectly related to your question I think it is also important to describe how to import this library into new projects since it's bound to come up and it can be confusing. There are two ways:

First,

You go to File > New > Import Module then you choose the source directory of the library project, make note of the module name. After that you'll see the library under the app folder in you left pane like in the picture. To use it though in your new project, you still have to add the dependency in the build.gradle (app) file like so:

dependencies {
    ........
    compile project(':mylibrary')
}

Second,

If all you have is an aar file for your Android Library, then you have to place the aar file under app/libs & then import it by going to File > New > New Module > Import .JAR/.AAR package & then choose your aar file. Be aware that people have had lots of issues with this method due to a bug in Gradle so avoid it if you can. Reference1, Reference2 & Reference3.

Last but not least, this reference I found says that you cannot include assets in Java libraries (the JAR ones) so this may not be an option for you. Hope this helps.

Community
  • 1
  • 1
Daniel
  • 2,355
  • 9
  • 23
  • 30
  • Thanks a lot, excellent answer but how did you get the app context from within the library. I need it to perform the reading of my Json file like: `InputStream is = getAppContext().getResources().openRawResource(R.raw.myJson);` – VMMF Jun 07 '16 at 03:53
  • I imagine you want to use the `JSON` file in your app as opposed to your library, that being the case, I am confused as to why you would be having issues getting the app context. In the screenshot, I am accessing the resource file from the library from an activity in the app. Even then Android libraries also can contain activities & these activities would have their own contexts as well. Can you be more specific about the issue you're having now? – Daniel Jun 07 '16 at 04:03
  • The Json file will be located on the library and also only accessed from the library. I'm reviewing some ways to pass the app context to the library and they all seem to be tricky and with downsides – VMMF Jun 07 '16 at 04:08
  • I see. In that case, you'll have to create at least one activity in your Android library & call `InputStream is = getApplicationContext().getResources().openRawResource(R.raw.myJson);` from there. Just make sure you change `getAppContext()` to `getApplicationContext()`. – Daniel Jun 07 '16 at 04:17