Use Sample data directory from a library
Asked Answered
H

2

9

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?

Hakluyt answered 19/2, 2018 at 21:27 Comment(2)
Which type of library did you create?Grote
Android library.Distraught
C
6

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.

Culinarian answered 25/2, 2018 at 17:0 Comment(0)
G
2

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.txt

Additionally, 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" />
Grote answered 23/2, 2018 at 10:12 Comment(5)
And do you know is there any other way to have it shared between multiple projects?Distraught
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.Grote
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.Distraught
@VladimirJovanović I've updated my answer. It will help you avoid the same data in each row of RecyclerViewGrote
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.Distraught

© 2022 - 2024 — McMap. All rights reserved.