Android: Customize the Gradle Build — Download Extra Resources Pre-Build

CHEN SU
CS Random Thoughts on Tech
2 min readAug 14, 2018

--

There are cases that your project depends on some resources provided by another team in your company or other 3rd party organizations, which are not hosted in Maven central or Google or anywhere. For example, some web pages packaged as a zip file, shared with you for your app to load in WebView.

Are you gonna manually download the zip file from the url and unzip and copy it the assets folder every time? This is too time wasting, and this is how customizing the gradle build could help.

First let’s find some zip file hosted on some websites, and assume that’s what we want to include in our assets folder.

Found this link http://www.geosensor.net/gisacpdocs/chapter1.zip, which is a slide for a course.

project level build.gradle

In the project level build.gradle, I’ve created several tasks, including downloading the zip, unzip it, remove it, etc.

A tip on testing those tasks separately is that you can run commands from Android Studio’s terminal, using gradle wrapper or gradle:

use gradlew to run tasks

If you see any permission denied error when running gradlew, simply give read-and-write access to gradlew by using $ chmod u+x gradlew

To make sure every time when you build the app, it always download the resource to your assets folder, simply create a new run configuration, and insert a new Gradle task before the default Make, calling task “syncSlides”.

All set, click the green RUN button, and you’ll see slides folder created under assets folder before the app gets installed and starts running.

That’s it. Customizing Gradle build is powerful and super fun! Cheers!

For codes, check out from here:

https://github.com/martinsuchen35/Android-CustomizeGradleBuild

--

--