Find your content:

Search form

You are here

What is gradle?

 
Share

What is gradle?

 

Gradle for Android

By default, Android projects are handled by the Gradle build system. If you create a new project in Android studio, the Gradle build scripts are automatically created. Android studio provides the Gradle runtime, hence no additional installation is required.

If you press the run button in Android Studio, it triggers the corresponding Gradle task and starts the application.

You can also run Gradle via the command line. To avoid unnecessary local installation, Gradle provides a wrapper script which allows you to run Gradle without any local installation.

 

Conversion process from source code to Android application

Java source files are converted into java class files. The Android SDK contains tool called dx which converts java class to dex file. All class files of the application are placed in this .dex file. During this conversion process reduntant information in the class files are optimized in the .dex file. 

The .dex file and other resources e.g images and xml files are packed into .apk file which is done by "Android Asset Packing Tool"

Finally apk file deployed into devices using adb command.

As of Android 5.0 the Android Runtime(ART) is used as runtime for all the android applications. ART uses a combination of ahead of time and just in time compilation. During the installation of an application on an android device, the application code is translated into machine code.

The dex2oat tool takes the .dex file created by the Android tool chain and compiles that into an Executable and Linkable Format (ELF file). This file contains the dex code, compiled native code and meta-data. Keeping the .dex code allows that existing tools still work.

 
Using the gradle on the command line
 
Multi distribution: the same application must be customized for several clients or companies
Multi apk: Supporting the creation of multiple apk for different device types while reusing parts of the code.
 
Android gradle build targets
 

./gradlew build - build project, runs both the assemble and check task

./gradlew clean build - build project complete from scratch

./gradlew test - Run the tests

./gradlew connectedAndroidTest - Run the instrumentation test.

Difference between unit test vs instrumentation test

Often Unit tests are referred to as “local tests” or “local unit tests”. The main reason for this seems to be that you want to be able to run tests without a device or an emulator attached.

Unit tests cannot test the UI for your app without mocking objects such as an Activity.

Instrumentation tests run on a device or an emulator. In the background, your app will be installed and then a testing app will also be installed which will control your app, lunching it and running UI tests as needed.

Instrumentation tests can be used to test none UI logic as well. They are especially useful when you need to test code that has a dependency on a context.

Remove unused resources and shrinking

The Gradle build system for Android supports resource shrinking at build time. This automatically removes resources that are unused from the packaged application. In addition to that, this also removes unnecessary resources from libraries you are depending on. This can hugely reduce the size of your application.

To enable resource shrinking, update your build file similar to the following snippet.

minifyEnabled true
shrinkResources true

 

Keep the version external

A good practice is to define the version of your library dependencies outside the dependencies closure for better maintenance.

ext {
    // App dependencies
    junitVersion = '4.12'
    mockitoVersion = '1.10.19'
    powerMockito = '1.6.2'
    hamcrestVersion = '1.3'
}

dependencies {
    // Dependencies for local unit tests
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-all:$mockitoVersion"
    testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
    testCompile "org.powermock:powermock-module-junit4:$powerMockito"
    testCompile "org.powermock:powermock-api-mockito:$ext.powerMockito"
}

My Block Status

My Block Content