官术网_书友最值得收藏!

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
主站蜘蛛池模板: 上栗县| 丰都县| 玉门市| 策勒县| 新化县| 广河县| 汉中市| 阿荣旗| 吉安市| 乌鲁木齐县| 拉孜县| 平顺县| 寻乌县| 偃师市| 嘉禾县| 浠水县| 湘潭县| 峡江县| 沙田区| 卢氏县| 高邮市| 丁青县| 樟树市| 前郭尔| 新竹市| 修武县| 博野县| 元阳县| 鲁山县| 出国| 黄陵县| 祁东县| 左云县| 平江县| 右玉县| 莱阳市| 衡阳县| 南开区| 松滋市| 杂多县| 周宁县|