Gradle Build System


현재의 안드로이드 앱 빌드 환경은 
Ant to gradle로 변경됨.

The Android build system was originally written in Ant
It was based on a rather flat project structure that did not offer much support for things such as build variationsdependency,management, and publishing

Ant has an XML tag-based programming model that is simple and extensible, though mnay developers find it cumbersome.

In addtion, Ant uses a declarative model.
However, most modern programming languages prefer imperative model

Gradle
written by Groovy programming language, which builds on top of Java's core runtime and API.

Groovy loosely follows Java's syntax which, when combined with its syntax, lowers the learning curve.

Modern software development involves following steps:
- linking
- compilation
- testing
- packaging
- distribution of end product

supporting variations of the end product
- a debug version
- a release version
- a paid version
- a free version

managing thrid-party software libraries

Gradle Syntax

The basic structure of gradle build script comprises configuration and task blocks.

Task blocks
Define code that is executed at various points during the build.

Configuration blocks
special Groovy closures that add properties and methods to underlying objects at runtime.

Task block은 안드로이드에서 이미 정의 되어 있기 때문에
Configuration block을 설정 하게 된다.

Configuration block의 형태

Gradle Build Concepts

The Gradle build system is a general tool for building software packages from a collection of source files.

Gradle Android Strcture

Gradle은 Hierarchical strcutrue를 지원한다. 즉 sub-project들 또는 module이 무한으로 겹쳐서 생성 될 수 있다.

사용되는 파일 리스트들은 아래와 같다.

.gradle: Temporary Gradle output, caches, and other supporting metadata are stored under this folder.

app: the simplest Android Project

gradle: This folder contains the Gradle wrapper. The Gradle wrapper is a JAR file that contains a version of the Gradle runtime compatible with the current project.

build.gradle: The overall project build logic lives in this file. It is reponsible for including any required subporjects and triggering the build of each one.

gradle.properties: Gradle and JVM properties are stored in this file. You can use it to configure the Gradle daemon and manage how gradle spawns JVM processes during the buld. You can also use this file to help Gradle communicate when on a network with a web proxy.

gradlew/gradlew.bat These files are the operating system-specific files used to execute Gradle via the wrapper. If Gradle is not installed on your system, or if you don't have a version compatible with your build, then it is recommended to use one of these files to invoke Gradle.

local.properties: This file is used to define properties specific to the local machine, such as the location of the Android SDK or NDK.

setting.gradle: This file is required with multiproject builds, or any project defining a subproject. It define which subproject. It defines which subprojects are included in the overall build.

Project.iml,.idea,.gitignore: While these files (with the exception of .gitignore discussed in Chapther 7) are not part of the Gradle build system, they are constantly updated as you make changes to your Gradle files.

Project Dependencies

대부분 필요한 기능을 이곳에 구현하게 된다.

Case Study: The Gradle Weather Project


실습 코드 다운

git clone https://bitbucket.org/csgerber/gradlewather.git

Gradle Weather라는 project는 다양한 build script를 포함하고 있으므로 이것을 이용해서 이해도를 높이도록 하겠다.

이것은 fake weather를 보여주게 된다.

본격적으로 case study를 수행하기전에 new branch를 생성해준다.
과정은 아래와 같다.

  • git log
  • step1 -> new branch -> name = mylocal

Build.gradle (project: gradleweather

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

buildscript를 포함하고 있다.
buildscript는 현재의 build 파일들을 설정 한다.

여기에 설정한 내용들은 모든 subprojects에 영향을 미치게 된다.

jcenter()의 내용은 internet-accessible Maven repository이다. 이것은 많은 Android dependecies를 가지고 있으며
open source project를 포함 한다.

그다음으로 dependencies를 설정 하는데 Gradle 0.12 이상이라고 설정 한다.
마지막으로 child project들은 모두 같은 JCenter repository로 설정 된다.

local.properties

sdk.dir=/root/Android_Application_Tools/android-sdk-linux_r24

It includes only a setting for the location of the Android SDK.

app/build.gradle

apply plugin: 'com.android.application'

The first line engages the Android Gradle plug-in for use in the current build.

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.0'

Set SDK version and build tools version
SDK version은 Android SDK APis를 말하는 것으로 컴파일해야 되는 target을 의미한다.
Build tools version은 build에 사용될 도구의 버전으로 Dalvik Executable conversion, ZIP algnment 등과 같은 것들을 결정 한다.

    defaultConfig {
        applicationId "com.apress.gerber.gradleweather"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

Defines the application ID (which is used when you submit to the Google Play Store),
the minimum SDK version that your app is compatible with,
the SDK that you are targeting, the app version, and version name.

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Google play store에 출시할때 obfuscation을 하기 위한 proguard사용에 관한 내용이다.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.+'
}

의존성 lib들을 설정해 준다.

Android Library Dependencies

Gradle's roboust repository system allows you to easily locate and use code from other companies, 
open source libraries, or libraries from others in your own organizatoin.

In this section, you will evolve our app by using an Android library dependency that makes the network request for weather data.

사실 실제로 network data를 가져오는 것은 아니지 
existing Android app code를 어떻게 재활용 하는지에 대해서 알아보는 것이다.

과정

  • File
  • New Moudle to open the New Module Wizard
  • Android Library in the first dialog box
  • WeatherRequest, minimum SDK settings
  • Add No Activity from the next page of the wizard

이렇게 생성을 하고 WeatherRequest의 build.gradle 내용을 살펴 보면 아래와 같다.

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
}

main build gradle과 주된 차이점은 Android library plug-in을 사용한다는 것이다.
이 plug-in은 특별한 Android archive file format인 AAR format을 모듈 소스로 부터 생성 하게 된다.

Step 3

NationalWeatherRequest class를 추가 한다.

적절한 기능을 추가 한다.

main app의 build.gradle dependencies 수정
compile project(':WeatherRequest')

Opening Older Projects

Gradle은 생각 보다 Version이 빠르다.
따라서 이전의 Project를 open할 경우 예상치 못한 error를 만날 수도 있다.

이경우 해당 지시사항을 이용해서 그대로 처리해 주면 된다.
ex) Fix plugin version and re-import project


'Computer Science > Android Application' 카테고리의 다른 글

쓰레드 (Thread)  (0) 2017.08.04
이벤트 (Event)  (0) 2017.08.04
Android Plot Libraries  (0) 2016.01.22
Android wear app  (0) 2015.08.25
Android Studio 자동 import 기능  (0) 2015.07.15

+ Recent posts