Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Studio Cookbook

You're reading from  Android Studio Cookbook

Product type Book
Published in Oct 2015
Publisher
ISBN-13 9781785286186
Pages 232 pages
Edition 1st Edition
Languages
Author (1):
Mike van Drongelen Mike van Drongelen
Profile icon Mike van Drongelen
Toc

The use of Gradle build scripts


Android Studio uses Gradle build scripts. It is a project automation tool and uses a Domain-specific Language (DSL) instead of the more common XML form to create the configuration of a project.

Projects come with a top-level build file and a build file for each module. These files are called build.gradle. Most of the time, it is only the build file for the app module that needs your attention.

Note

You may note that some properties that you could find in the Android manifest file previously, such as the target SDK and versioning properties, are now defined in a build file and should reside in the build file only.

A typical build.gradle file may look like this:

applylugin: 'com.android.application'
android {
  compileSdkVersion 21
  buildToolsVersion "21.0.0"
  defaultConfig {
  minSdkVersion 8
  targetSdkVersion 21
  versionCode 1
  versionName "0.1"
  } 
}
dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
}

The Gradle build system is not something that you need to worry too much about right now. In later recipes, we will see what the real power of it will be. The system is also designed to support complex scenarios that may be faced while creating Android applications, such as handling customized versions of the same app for various customers (build flavors) or creating multiple APK files for different device types or different Android OS versions.

For now, it is ok just to know that this is the place where we will define compileSdkVersion, targetSdkVersion, and minSdkVersion, just like we did in the manifest file previously in case you have been using Eclipse.

Also, this is the place where we define versionCode and versionName, which reflect the version of your app that is useful if someone is going to update the app you wrote.

Another interesting key element of the Gradle functionality is that of dependencies. Dependencies can be local or remote libraries and JAR files. The project depends on them in order to be able to compile and run. In the build.gradle file that you will find in the previous folder the app folder you will find the defined repository in which the libraries reside. jCenter is the default repository.

If for example you wish to add the Parse functionality, which is something that we will do in the recipes found in the next chapter, the following dependency declaration will add the local Parse library to your project:

dependencies {
compile fileTree(dir: 'libs', include: 'Parse-*.jar')compile project(':Parse-1.9.1')
}

Using external libraries has become much easier. For example, if you want to add UniversalImageLoader, a well-known library to load images from the Internet, or if you want to use the functionality from the Gson library, which basically is an object wrapper for JSON data, to your app, the following dependency declaration will make these libraries available to the project:

dependencies {
compile 'com.google.code.gson:gson:2.3+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
}

There's more...

Some other Gradle concepts will be explained in the recipes of the next chapters. Gradle is a topic that one could write a book about on, and you can find many interesting in-depth tutorials on the Internet if you would like to know more about it.

See also

  • For more information about Gradle build scripts, refer to Chapter 2, Applications with a Cloud-based Backend

You have been reading a chapter from
Android Studio Cookbook
Published in: Oct 2015 Publisher: ISBN-13: 9781785286186
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}