Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 使用gradle構建Android項目的基礎知識

使用gradle構建Android項目的基礎知識

編輯:關於Android編程

1.1 Gradle Build Files in Android

1 settings.gradle

位於根目錄,它指明了那些包含在多項目構建中的子項目(Android支持gradle多項目構建)

2 build.gradle(top-level)

位於根目錄,在這裡添加配置選項會對所有的子項目起效

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

2.1 buildscript

The Gradle distribution does not include Android functionality by default. Google
provides an Android plug-in for Gradle, which allows easy configuration of Android
projects. The buildscript block in the top-level build file tells Gradle where to
download that plug-in.

大概原意:Gradle默認是沒有構建Android項目的功能的;而Google官方提供了一個Android插件給gradle用於簡化Android項目的構建配置。buildscript標簽配置就是用於說明gradle需要去哪裡下載該Android插件,相當於給gradle配置了一個下載的地址

倉庫配置

jcenter()-Bintray JCenter Artifactory repository:默認的倉庫,該倉庫所有的內容都放在CDN上,並使用HTTPS鏈接,它的速度相對較快

mavenCentral():Maven默認的倉庫配置

2.2 allprojects

The allprojects section indicates that the top-level project and any subprojects all
default to using the jcenter() repository to resolve any Java library dependencies.

大概原意:allprojects模塊表明頂層項目和其他子項目默認使用jcenter()倉庫去下載任何需要的java-lib庫

2.3 task clean(簡單描述,其他章節再詳細介紹)

Gradle允許我們自定義任務(task),並將其跟其他的任務關聯起來(DAG)。上邊的配置作用是:定義了一個刪除的任務,用於刪除根目錄的build子目錄

3 Gradle build file for the app subproject

每個子項目的獨立build.gradle文件,用於配置子項目特殊的一些構建屬性

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.kousenit.myandroidapp"
        minSdkVersion 19
        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'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
}

3.1 apply

The apply functionality in Gradle adds the Android plug-in to the build system,which enables the android section Domain Specific Language (DSL) configuration.

大概原意:apply功能將Android插件添加到整個構建系統中,使其支持Android部分領域特定語言

3.2 dependencies

compile fileTree(dir: ‘libs’, include:[‘*.jar’]):
libs路徑下的所有.jar文件都被添加到編譯類路徑下

testCompile ‘junit:junit:4.12’:下載4.12版的junit,用於進行單元測試

compile ‘com.android.support:appcompat-v7:23.3.0’:下載23.3.0版本的appcompat-v7,作為其中一個編譯的依賴庫

1.2 Configure SDK Versions and Other Defaults

The top-level Android build file adds the Android plug-in for Gradle to your project,via the buildscript block. Module build files “apply” the plug-in, which adds an android block to the Gradle DSL.

大概原意:根目錄的buildscript模塊將Android插件添加到構建系統中,子目錄中的build.gradle使用該插件將android作用塊添加到Gradle-DSL,在該作用塊中你可以為每個子項目配置特殊的構建屬性

1 android

android作用塊是Android-DSL的入口,在這裡需要使用compileSdkVersion指定編譯的目標版本,使用build
ToolsVersion指定構建工具的版本

2 defaultConfig作用塊幾個屬性

2.1 applicationId

該應用的包名

2.2 minSdkVersion

該應用支持的最低版本的SDK

2.3 targetSdkVersion

該應用支持的目標版本的SDK

2.4 versionCode

通常用於應用升級

Prior to the switch to Gradle, the minSdkVersion and buildToolsVersion properties
were specified in the Android Manifest as attributes of a tag. That
approach is now deprecated, as the values there are overridden by the values in the
Gradle build file.

大意:AndroidManifest中使用配置的minSdkVersion和buildToolsVersion屬性會被build.gradle中的配置覆蓋

3 compileOptions

配置該app使用的JDK版本

1.3 Executing Gradle Builds from the Command Line

The purpose of the Gradle wrapper is to allow a client to run Gradle without having
to install it first. The wrapper uses the gradle-wrapper.jar and the gradle-
wrapper.properties files in the gradle/wrapper folder in the application root to start the
process.

大概原意:Gradle wrapper的作用是讓客戶端在沒有安裝Gradle的情況下也能運行gradle腳本;其使用gradle-wrapper.jar和gradle-wrapper.properties文件來運行程序

1 gradle-wrapper.properties

#Mon Dec 28 10:00:20 PST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

1.1 The distributionUrl property indicates that the wrapper will download and install version 2.10 of Gradle:將下載2.10版本的gradle-wrapper

1.2 the Gradle distribution will be cached in
the zipStorePath folder under the zipStoreBase directory and then be available for
all subsequent executions of Gradle tasks:第一次下載後將會把wrapper緩存在zipStorePath目錄下,以後將會適用於所有的gradle任務(即以後的task將會默認使用緩存中的wrapper)

2 Additional features and command-line flags

2.1

// 多個同名的task,只會執行一次
Executing more than one task
> ./gradlew lint assembleDebug
Note that repeating the same task name only executes it once.

2.2

// 可使用-x來排除不需要執行的task
> ./gradlew assembleDebug -x lintDebug
Excluding the lintDebug task

2.3

// 可使用縮略的字符串執行,但必須保證縮略的字符串沒有歧義
> ./gradlew anDep
Not enough letters to be unique

1.4 Executing Gradle Builds from Android Studio

To execute a particular task, double-click the entry in the Gradle window.雙擊下圖的某個選項就可執行

常用命令
常用命令
terminal查看進度
terminal

1.5 Adding Java Library Dependencies

gradle支持多種不同方式的依賴引用

1.1 The full syntax for a dependency calls out the group, name, and version numbers explicitly

testCompile group: 'junit', name: 'junit',version: '4.12'

1.2 精簡方式

testCompile 'junit:junit:4.12'

1.3 版本號是可變的

testCompile 'junit:junit:4.+'

This tells Gradle that any version of JUnit greater than or equal to 4.0 is required to
compile the project’s tests. While this works, it makes the build less deterministic and
therefore less reproducible. Explicit version numbers also protect you from changes
in later versions of a particular API.

1.4 添加多個引用資源

If you want to add a set of files to a configuration without adding them to a reposi‐
tory, you can use the files or fileTree syntax inside the dependencies block

dependencies {
    compile files('libs/a.jar', 'libs/b.jar')
    compile fileTree(dir: 'libs', include: '*.jar')
}

1.6 Adding Library Dependencies Using Android Studio

1 dependencies列表
app
2 添加界面
add
3 搜索界面
search
4 搜索結果界面
search-result

1.7 Configuring Repositories

1

JCenter():https://jcenter.bintray.com

2

mavenCentral():http://repo1.maven.org/maven2

3 Any Maven repository can be added to the default list using a maven argument with a url block

repositories {
    maven {
        url 'http://repo.spring.io/milestone'
    }
}

4 Password-protected repositories use a credentials block (設置帳號密碼)

repositories {
    maven {
        credentials {
            username 'username'
            password 'password'
        }
        url 'http://repo.mycompany.com/maven2'
    }
}

5 If you have files on the local filesystem, you can use a directory as a repository with the flatDir syntax

repositories {
    flatDir {
        dirs 'lib'
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved