VMMF
VMMF

Reputation: 954

Include a .json file in an android library and not in /res/raw or /assets folders of my demo app project

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?

Upvotes: 2

Views: 5109

Answers (1)

Daniel
Daniel

Reputation: 2373

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.

Upvotes: 3

Related Questions