Using the Gradle right way as an Android developer

Bhanu Pro
2 min readJul 9, 2021
Photo by Danial Igdery on Unsplash

Yes, you are in the right place if you are trying to learn/ implement new things using Gradle tasks.

As most of the app developers faced or implemented, bundling some static files or some type of configuration files into assets folder while releasing apps. whether it includes tensor flow lite model files or some type of JSON files.

I have also come across this type of feature, so I researched and automated manually downloading and putting it into the assets folder when even some kind of configuration or data changes.

So for demo purposes, I am putting some JSON files specific to the app.

Step 1: Downloading files from URL

I have defined these tasks in project-level build.gradle file to download files from the internet and put them into the specific folder

static def downloadFile(String url, String filePath) {
def f = new File(filePath)
if (!f.exists()) {
new URL(url).withInputStream{i->f.withOutputStream{it << i }}
}
}

Step2:

Here I am registering a new task to execute file downloading

tasks.register('downloadStaticFiles'){
def configJsonPath = "app/src/main/assets/config.json"
downloadFile("url of config file", configJsonPath)
}

and here we have to execute this task before building the app, so it will be bundled into the app after project build.

Step3:

In App-level build.gradle, execute this task before prebuild like this.

preBuild.dependsOn('downloadStaticFiles')

I hope you enjoyed this tutorial if you like this article don’t forget to give a claps

--

--