Use a sample data catalog from the library

I created a sample data catalog for my Android application 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 has only sample data. But as far as I can see, the sampledata folder sampledata not compile into the library. Is there a way to share data samples between multiple Android projects?

+7
android android-layout android-studio sample-data
source share
2 answers

As already mentioned, you cannot do this with the library, because sampledata simply cannot be part of the Android library.

However, you can place the names file somewhere, and then extract it using the gradle task, you can simply add build.gradle to the application

 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 } } } 

Here you can see 2 functions, the first of which is performed before the clean task, and it will simply delete your sampledata folder. The second is the task performed on each assembly, it will not download the file every time, but only if the directory does not exist.

I understand that you can also copy the paste names file, but with this method you need to copy the paste only once, and you can change the names in any project by simply uploading a new file and doing a clean build.

+3
source share

The short answer is no, you cannot do this using the sampledata folder. Mostly the format of Android AAR libraries. If you link to official documentation, it says that:

The file itself is a zip file containing the following required entries:

/AndroidManifest.xml

/classes.jar

/ RES /

/R.txt

/public.txt

In addition, an AAR file may include one or more of the following additional entries:

/assets/

/libs/name.jar

/jni/abi_name/name.so (where abi_name is one of the supported Android ABIs)

/proguard.txt

/lint.jar

So sampledata cannot be part of the AAR library.

UPDATE

Instead of your own sample data, you can use predefined sample resources.
For example, @tools:sample/first_names will randomly choose from some common names, for example, Sofia, Jacob, Ivan.

Usage example:

 <TextView android:layout_width="match_parent" android:layout_height="wrap_content" tools:text="@tools:sample/first_names" /> 
+1
source share

All Articles