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
,’password’參數給properties設值。login
gradlew -Plogin=me -Ppassword=this_is_my_password assembleDebug
2.2 轉換Eclipse Android項目到Android Studio
問題
早期都是使用Eclipse開發,現在想要使用Android Studio。
解決方法
Android Studio自帶導入向導,根據向導一步一步做。
2.3 轉換Eclipse Android項目到Android Studio通過Eclipse
問題
早期都是使用Eclipse開發,現在想要使用Android Studio。(推薦使用2.2方式)
解決方法
右鍵項目
Export
->
Generate Gradle build files
->選擇這個項目下的所有library->finish
然後就會生成一個
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。
- 添加一個
task到build.gradlewrapper
修改版本後在控制台執行task wrapper(type: Wrapper) { gradleVersion = 2.2 }
就可以使用新的gradlew版本。gradlew wrapper
- 或修改gradle/wrapper 目錄下的.properties 更改相應的版本即可。
2.5 在不同項目直接共享設定
問題
在一個項目中有多個library項目,可以去除這些,modules重複的設定。
解決方法
- 在頂層
中定義了build.gradle
節點allprojects
果在單個項目中如果沒有特殊情況則不需要重複定義倉庫。allprojects { repositories { jcenter() } }
- 使用
由于gradle是多項目工程,如果使用subprojects
則可以一起定義所有library項目設定。subprojects
則所有library可以去掉subprojects { apply plugin: 'com.android.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打包
如果沒有keystore則根據向導建立。