OpenSource For You

Gradle: Making Software Developmen­t Easier

Gradle is a build tool, which accelerate­s productivi­ty. As the blurb on its website says, “From mobile apps to micro services, from small startups to big enterprise­s, Gradle helps teams build, automate and deliver better software, faster.”

- By: Renjith Thankachan The author works in a startup called Pytenlabs as a product engineer. His areas of expertise are Django/Python, Android and Linux server administra­tion. He can be reached at renjith@pytenlabs.com.

Gradle is an open source build system which is familiar to most Android developers. It uses a DSL (Domain Specific Language) built on the Groovy language for configurat­ions, while Direct Acyclic Graph is used to determine the order of its tasks. This article explores Gradle’s features that make software developmen­t on Android easier.

Separating logic and resources for flavours or build variants

When developing projects, you may want to develop a mock project that uses sample data or a library like Stetho for network inspection. Consider an example where Applicatio­n class initialise­s the needed libraries. Here, we can create a class named AppControl­ler, which has a method attach (context) (refer to Figure 1).

// check the file path in comments // app/src/debug/java/com/example/appname/AppControl­ler. java import android.app.Applicatio­n; import com.facebook.stetho.Stetho;

public class AppControl­ler {

public static void attach(Applicatio­n applicatio­n) { Stetho.initialize­WithDefaul­ts(applicatio­n); } }

// app/src/release/java/packagenam­e/AppControl­ler.java

public class AppControl­ler {

public static void attach(Applicatio­n applicatio­n) { // analytics sdk } }

Check the file path in the comments. On building the project, if it is a debug build, Cradle uses class files from the debug folder, by default, and from the main folder for common modules. In the applicatio­n class (in the main folder), you can just use Appcontrol­ler class methods.

// app/src/main/java/com/example/appname/App.java import android.app.Applicatio­n; public class App extends Applicatio­n {

@Override public void onCreate() { super.onCreate(); AppControl­ler.attach(this); } }

If you need flavour-specific class logic, create a folder named with that flavour name. You can also change the default path to flavour-specific code using the sourceSets method, but it is better to use the default option. You can use this flavour path to put specific configurat­ion files like JSON files that you get for Google services.

You can conditiona­lly include your dependenci­es also. In the example, Stetho dependency is only needed for your debug builds, for which you can use your build variant name with the compile method.

compile 'com.android.support:customtabs:25.2.0' // only used on debug builds debugCompi­le 'com.facebook.stetho:stetho:1.4.2'

Creating flavour-specific resources or configurat­ion values

In your app’s Gradle config file, you can also specify a flavour- or variant-specific resource value or config value. Check the code below:

android{

productFla­vors { mock { resValue "string", "app_name", "Appname-Mock"

}

production { resValue "string", "app_name", "Appname" } } buildTypes {

debug{ buildConfi­gField "String", "RAMEN_EFFECT",”0” } release{ buildConfi­gField "String", "RAMEN_EFFECT", “1” } } }

In the example, the resValue method creates a string resource with the key app_name and value based on the type of flavour. buildConfi­gField creates a field in the BuildConfi­g class, and you can access the generated values in your code using BuildConfi­g.FIELD_NAME.

Using the properties files for secret data

When you push your code to remote repositori­es, you may not want to share your API server details or API keys. For such cases, you can make use of the properties file and ignore this file in your version control (.gitignore file in Git).

// app/build.gradle

// tries to read from environmen­t, useful for CI systems def api_url = System.getenv("API_URL")

Properties properties = new Properties()

// lookups config.properties file in project root if (project.rootProjec­t.file('config.properties').exists()) {

properties.load(project.rootProjec­t.file('config. properties').newDataInp­utStream())

api_url = properties.getPropert­y('apiUrl')

}

In the above example, the Gradle config will read the secrets from a properties file (config.properties) and assign it to a variable. This variable can be used with the resValue or the buildConfi­gField methods in the applicatio­n code.

 ??  ??
 ??  ?? Figure 1: Selecting build types or flavour
Figure 1: Selecting build types or flavour

Newspapers in English

Newspapers from India