Gradle for Android and Java

You can find the corresponding udacity course here.

Table of Contents

  1. Gradle Fundamentals
  2. Gradle for Java
  3. Gradle for Android
  4. Special Topics

Gradle Fundamentals

Why Gradle? Allow different app flavours like paid or free, debug and release.

Groovy

Groovy is the scripting language for java developers.

Groovy functions always return the last expression in the function block.

Closure

Closures are like code blocks and are surrounded by { and }. Example:

def myClosure {
	...
	...
}

Call it by myClosure().

Make a one line closure def myClosure = { ... }

Higher order = functions as arguments are possible. Example:

def applyTwice(func, arg) {
	func(func(arg))
}

If a closure is only taking 1 argument by default that argument is called it. Access it by $it. Example:

def myList = ["Gradle", "Groovy", "Android"]
def printItem = {item -> println "list elem: $item"}
myList.each(printItem)
myList.each{println "list item: $it"}

Classes

class GroovyGreeter {
	...
	...
}

Setters and getters are automatically created, accress them by .

For more Groovy knowledge Learn X in Y: Groovy and Official Groovy user guide.

Tasks

A task does an action.

Run tasks with the Gradle wrapper

Daemon = process that hangs around in the background of your operating system. Always use a daemon if possible.

Typed tasks

Example:

task copyFiles(type: Copy)

Common types are:

See all task types in the Gradle DSL Reference. The Gradle DSL Reference is your best friend: learn it, love it!

incremental builds = doing the minimum amount necessary

Create custom types for tasks

class HelloTask extends DefaultTask {
	String firstName

	@TaskAction
	void doAction() {
		... //Use for ex. firstName
  	}
}

task hello(type: HelloTask) {firstName = 'Gunda'}

Relationships between tasks

Build scripts

Gradle build scripts are written in the domain specific language (DSL) that sits on top of Groovy.

The Gradle plattform is written in Java.

Plugins can be written in any JVM language like Java, Groovy or Scala.

Keep build scripts declarativ. Low-level logic is for Gradle plugins!

A build script has a delegate object. = Entire project delegates to a project object.

Declare tasks with method task(Tasknames) on the project object. Then Gradle tasks show under other tasks the Tasknames.

These are all identic:

project.task("myTask")
task("myTask")
task "myTask"
task myTask

Functions on tasks

task configuration closure example:

task myTask {
	description "The description of my Task" //this is a function
	group "The group of my Task" //call omitting the parentheses
	doLast {
		...
	}
}

Log levels

println statements are in the quite logging level.

Build Lifecyle

Has 3 steps:

  1. Initialization Set up multi-project builds
  2. Configuration Execute the build script and configure all the projects tasks
  3. Execution Execute the projects tasks

The best debugger is clear thought and print statements.

Gradle for Java

Use gradle plugins available for gradle instead of reinventing the wheel.

Add the java plugin with apply plugin: "java". Find a quickstart guide here.

Create a JAR with the command gradle assemble and execute it with gradle execute.

Add repositories to your project by repository {...}. Define dependencies on artifacts contained on your repositories. Ex.: dependencies { compile ... }.

Generate a dependency report with gradle dependencies.

Identify version conflicts with gradle dependencyInsight --dependency dependency-name what generates the dependency insight report.

Create fancy file collections with custom configurations:

configurations {
	custom
}

dependencies {
	custom 'com.google.guava:guava:18.0'
}

The scheme for archive names goes basename-appendix-version-classifier-extension.

High quality software depends on rigorous testing. That’s why tests need to be automated. There exist two sorts of tests:

Add test dependency bytestCompile 'junit:junit:4.12'.

Execute tests with gradle test and get detailed test reports from the build/reports directory.

Find Gradle Plugins on the Gradle Plugin Portal or look at the Standard Gradle plugins.

Set the Gradle version to use in the gradle-wrapper.properties file and always version control it.

Generate the wrapper with gradle wrapper.

Gradle for Android

The entire Android build process is done by Gradle.

Build an APK is executing a Gradle script with the Android Gradle plugin provided by Google.

Get a “failed to sync” message is usually an error in one of your build scripts.

Generated folders and files by Android Studio in a Gradle project:

Folder What holds it?
.gradle Information for incremental build support like tasks inputs and outputs.
.idea Android Studio’s model of your project
build Outputs generated by your build
gradle Wrapper JAR and wrapper properties
File What contains it?
build.gradle Your Gradle script
.iml Part of Android Studio’s project model
gradlew / gradlew.bat The wrapper scripts
local.properties The Location of Android sdk on your machine

Use build types (app/build.gradle) to build different versions of an app.

Find the DAC (developer.android.com) documentation on the Android Gradle plugin here.

Create app flavors by adding the following underneath the buildTypes block:

productFlavors {
	free {
		applicationId "com.example.udacity.flavors.free"
	}
	paid {
		applicationId "com.example.udacity.flavors.paid"
	}
}

Create flavor specific content by right click on app>New>Android resources file enter a file name like “strings” and choose the source set like “paid” or “free”.

Advanced Android Builds

Application Libraries for Multiproject Builds

Two sorts of libraries:

Create an Android Library by right click on the project>New>Module select “Android Library”. Add your custom in project library with compile project(":mylibrary") in the dependencies block of your build.gradle. Like this we can create Activities that are easy to reuse between applications.

Application Signing

Three things needed:

  1. Create a key store and a key
  2. Create a signing config
  3. Assign the signing config to a build type

Create a key store and a key

One time signing: Navigate to Build>Generate Signed APK and go through the wizzard.

Automatic signing: Right click on app>Open Module Settings switch to the Signing tab and create a new signing configuration. Go to the Build Types tab, select a build type and assign the Signing Config.

Multi Deck Support

Turn Java byte code into Davik byte code = dexing. This creates 1 table limited to 65,000 entries.

If you have more then 65,000 methods in a project, set multiDexEnabled true in the defaultConfig in the build.gradle file. This will break up the dexing table into multiple tables.

Proguard

Reduce size of your app by stripping out unused code and ressources. Add minifyEnabled true and shrinkResources true to your buildTypes in your build.gradle file.

You can also obfuscate your code with proguard.

Android Testing

Test locations unit test connected
general /src/test /src/androidTest
flavor “free” /src/testFree /src/androidTestFree

Special Topics

Add Gradle Versions Plugin to your project to hold your non-google plugins versions up to date.

More videos on Gradle can be found on Gradle’s youtube channel.

Written with for Gradle in Kiel.