2.1 Setting Project Properties
Gradle build files support property definitions using a simple ext syntax, where in
this case “ext” stands for “extra.” This makes it easy to define a variable value once and
use it throughout the file.
大概原意:Gradle构建文件支持使用ext语法来定义属性,我们可以用ext来定义可以全局使用的变量值
Sample “extra” property
ext {
def AAVersion = '4.0-SNAPSHOT' // change this to your desired version
}
dependencies {
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
The use of the def keyword here implies that this is a local variable in the current
build file. Defining the variable without def (or any other type) adds the variable as
an attribute of the project object, making it available in this project as well as any of
its subprojects.
大概原意:关键字def表明AAVersion变量是当前build file的局部变量;假如没有使用def来定义变量,那么该变量将作为project的属性,对其他的子项目也可见(可用)
Maven repo with credentials
传统写法
repositories {
maven {
url 'http://repo.mycompany.com/maven2'
credentials {
username 'user'
password 'password'
}
}
}
特殊写法
don’t keep the actual username and password values in the build file
// gradle.properties file
login='user'
pass='my_long_and_highly_complex_password'
repositories {
maven {
url 'http://repo.mycompany.com/maven2'
credentials {
username login
password pass
}
}
}
2.2 Porting Apps from Eclipse ADT to Android Studio
1 导入图解
2 项目结构
It uses the old project structure, where res , src , and AndroidManifest.xml are all direct children of the root.:保持原来的项目结构
3 dependencies
使用dependencies代替原来的jars库
2.3 Porting Apps from Eclipse ADT Using Eclipse
android {
compileSdkVersion 18
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aild.ext.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
2.4 Upgrading to a Newer Version of Gradle
1. Add a wrapper task to your build.gradle file and generate new wrapper scripts
1.1 The gradle wrapper command supports a –gradle-version argument
Specifing the wrapper version on the command line
> ./gradlew wrapper --gradle-version 2.12
:wrapper
BUILD SUCCESSFUL
Total time: ... sec
1.2 add the wrapper task to the (top-level) build file, and specify a value for gradleVersion
task wrapper(type: Wrapper) {
gradleVersion = 2.12
}
2. Edit the distributionUrl value in gradle-wrapper.properties directly
In addition to the generated scripts gradlew and gradlew.bat, the wrapper relies on a
folder called gradle/wrapper and the two files included there, gradle-wrapper.jar and
gradle-wrapper.properties
大概原意:除了使用gradlew生成脚本外,wrapper依赖于gradle-wrapper.jar和gradle-wrapper.properties两个文件
gradlew
gradlew.bat
gradle/wrapper/
gradle-wrapper.jar
gradle-wrapper.properties
Properties in the gradle-wrapper.properties file
#... date of most recent update ...
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
// **直接修改distributionUrl的版本号**
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip
2.5 Sharing Settings Among Projects
1 subprojects
// Using a subprojects block in the top-level build file
subprojects {
apply plugin: 'com.android.library'
}
2.6 Signing a Release APK
1 通过signingConfigs定义签名文件
android {
// ... other sections ...
signingConfigs {
release {
keyAlias 'my_alias'
keyPassword 'password'
storeFile file('/Users/kousen/keystores/myapp.keystore')
storePassword 'password'
}
}
}
keyAlias
The value used in the keytool when signing a particular key
keyPassword
A particular key’s password used during the signing process
storeFile
The disk file containing keys and certificates, generated by the keytool
storePassword
The password used for the keystore itself
2 使用上边定义的签名文件
android {
// ... other sections ...
buildTypes {
release {
// ... other settings ...
signingConfig signingConfigs.release
}
}
}
2.7 Signing a Release APK Using Android Studio
使用AS生成签名apk
第一步
![]()
第二步
![]()
第三步