天天看點

Gradle for Android 使用之旅之從建立項目到釋出

Gradle Build Files in Android 第二章

從建立項目到釋出

2.1 Setting Project Properties

問題

如何通過

ext

節點把一些常量從

build.gradle

中移除加入到

gradle.properties

,或者在控制台

-p

設定值。

解決方法

  • build.gradle

    中通過

    ext

    把例如版本号,版本名集中設定。

    例如:

    ext {
      appcompat_version = '24.2.1'
      versionCodes=13
      versionNames='2.3'
    }
               
    使用:
    defaultConfig {
       applicationId "com.branch.www.gradledemo"
       minSdkVersion 18
       targetSdkVersion 24
       versionCode versionCodes
       versionName versionNames
    }
               
    compile("com.android.support:appcompat-v7:$appcompat_version")
               
  • 使用gradle.properties

    例如在

    gradle.properties

    加入:
    login='user'
    pass='my_long_and_highly_complex_password'
               
    然後在

    build.gradle

    中使用
    repositories {
        maven {
            url 'http://repo.mycompany.com/maven2'
            credentials {
            username 'user'
            password 'password'
        }
     }
    }
               
    或你可以在控制台使用

    -P

    輸入

    login

    ,’password’參數給properties設值。
    gradlew -Plogin=me -Ppassword=this_is_my_password assembleDebug
               

2.2 轉換Eclipse Android項目到Android Studio

問題

早期都是使用Eclipse開發,現在想要使用Android Studio。

解決方法

Android Studio自帶導入向導,根據向導一步一步做。

Gradle for Android 使用之旅之從建立項目到釋出

2.3 轉換Eclipse Android項目到Android Studio通過Eclipse

問題

早期都是使用Eclipse開發,現在想要使用Android Studio。(推薦使用2.2方式)

解決方法

右鍵項目

Export

->

Generate Gradle build files

->選擇這個項目下的所有library->finish

Gradle for Android 使用之旅之從建立項目到釋出

然後就會生成一個

build.gradle

apply plugin: 'android'

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

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}
           

2.4 更新Gradle 版本

問題

根據需要更新Gradle版本

解決方法

寫wrapper或修改gradle properties url。

  • 添加一個

    wrapper

    task到build.gradle
    task wrapper(type: Wrapper) {
      gradleVersion = 2.2
    }
               
    修改版本後在控制台執行

    gradlew wrapper

    就可以使用新的gradlew版本。
  • 或修改gradle/wrapper 目錄下的.properties
    Gradle for Android 使用之旅之從建立項目到釋出
    更改相應的版本即可。

2.5 在不同項目直接共享設定

問題

在一個項目中有多個library項目,可以去除這些,modules重複的設定。

解決方法

  • 在頂層

    build.gradle

    中定義了

    allprojects

    節點
    allprojects {
        repositories {
            jcenter()
        }
    }
               
    果在單個項目中如果沒有特殊情況則不需要重複定義倉庫。
  • 使用

    subprojects

    由于gradle是多項目工程,如果使用

    subprojects

    則可以一起定義所有library項目設定。
    subprojects {
        apply plugin: 'com.android.library'
    }
               
    則所有library可以去掉

    apply plugin

2.6 簽名APK包

問題

經過正式簽名釋出App到應用市場

解決方法

建立keystore,使用它簽名APK。使用

signingConfigs

配置如下:

android {
        // ... other sections ...
        signingConfigs {
            release {
                keyAlias 'my_alias'
                keyPassword 'password'
                storeFile file('/Users/kousen/keystores/myapp.keystore')
                storePassword 'password'
            }
        }
    }
           

然後在buildTypes中設定對應的簽名

android {
        // ... other sections ...
        buildTypes {
            release {
                // ... other settings ...
                signingConfig signingConfigs.release
            }
        }
    }
           

在控制台輸入

gradlew assembleRelease

則會build一個簽名包到

/build/outputs/apk

目錄下。

2.7 使用Android Studio打包

Gradle for Android 使用之旅之從建立項目到釋出

如果沒有keystore則根據向導建立。

第三章:gradle配置進階