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?
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.
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" />
layout
. The only limitation which I know, it's impossible to use JSON
as described in your tutorial. –
Grote 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. –
Distraught RecyclerView
–
Grote © 2022 - 2024 — McMap. All rights reserved.