Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Sample data directory from a library

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?

like image 737
Vladimir Jovanović Avatar asked Feb 19 '18 21:02

Vladimir Jovanović


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

like image 159
lelloman Avatar answered Oct 01 '22 01:10

lelloman