I created Sample data directory for my Android app using the process described in this article. I would like to share this set of sample data between my projects, so I created a library that only has sample data inside. But as far as I can see sampledata
folder is not being compiled into the library. Is there a way to share sample data between multiple Android projects?

- 2,261
- 2
- 24
- 43
-
Which type of library did you create? – Denysole Feb 23 '18 at 07:47
-
Android library. – Vladimir Jovanović Feb 23 '18 at 08:10
2 Answers
As already said, you can't do that with a library because sampledata
simply can't be part of an Android library.
One thing you could though, host your names
file somewhere and then fetch it with a gradle task, you could just add to an app's build.gradle
clean.doFirst {
println "cleanSamples"
def samplesDir = new File(projectDir.absolutePath, "sampledata")
if (samplesDir.exists()) {
samplesDir.deleteDir()
}
}
task fetchSamples {
println "fetchSamples"
def samplesDir = new File(projectDir.absolutePath, "sampledata")
if (samplesDir.exists()) {
println "samples dir already exists"
return
}
samplesDir.mkdir()
def names = new File(samplesDir, "names")
new URL('http://path/to/names').withInputStream { i ->
names.withOutputStream {
it << i
}
}
}
You can see 2 functions there, the first one is run before a clean
task and it will just delete your sampledata
folder. The second one is a task run on every build, it won't download the file every time but only if the directory is not there.
I understand you might as well copy paste names
file, but, with this method you need to copy paste the tasks only once and you would be able to change names
in any project just by uploading a new file and doing a clean build.

- 13,883
- 5
- 63
- 85
The short answer is no, you can't do that with sampledata
folder. Basically, the format of the Android Libraries is AAR
. If you reference the official documentation, it says that:
The file itself is a zip file containing the following mandatory entries:
/AndroidManifest.xml
/classes.jar
/res/
/R.txt
/public.txtAdditionally, an AAR file may include one or more of the following optional entries:
/assets/
/libs/name.jar
/jni/abi_name/name.so (where abi_name is one of the Android supported ABIs)
/proguard.txt
/lint.jar
So, sampledata
can't be a part of AAR
library.
UPDATE
Instead of your own data samples, you can use predefined sample resources.
For example @tools:sample/first_names
will randomly select from some common first names, eg., Sophia, Jacob, Ivan.
Example of usage:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="@tools:sample/first_names" />

- 3,903
- 1
- 20
- 28
-
And do you know is there any other way to have it shared between multiple projects? – Vladimir Jovanović Feb 23 '18 at 10:15
-
I spent plenty of hours but didn't reach any result. You can use any of your resources as sample data in the `layout`. The only limitation which I know, it's impossible to use `JSON` as described in your tutorial. – Denysole Feb 23 '18 at 10:24
-
The main problem for me with `resources` is that in repeatable elements (item in `RecyclerView`) same text will be shown over and over. And I am also not sure will resource referenced by `tools:` be removed by ProGuard. Anyway, thanks for the help. I will leave this question opened for couple more days, and if there is no better answer you will get the bounty. – Vladimir Jovanović Feb 23 '18 at 10:31
-
@VladimirJovanović I've updated my answer. It will help you avoid the same data in each row of `RecyclerView` – Denysole Feb 23 '18 at 10:44
-
I know about those, but I like to have "Jake" in the list of names. :) It's just weird to me that there is no way to have custom samples available for multiple projects, without copy/paste. – Vladimir Jovanović Feb 23 '18 at 11:01