轉(zhuǎn)自:http://www.reibang.com/p/02cb9a0eb2a0
build配置
buildscript {
? ? ? ?repositories { ? ??
? ? ? ? ? ? ? ? ? ? jcenter()?
?}?
dependencies { ??
? classpath'com.android.tools.build:gradle:1.2.3'
}?
}
Android腳本
applyplugin:'com.android.application'
Android配置
android {?
? ? ? ? ?compileSdkVersion22
? ? ? ? ?buildToolsVersion"22.0.1"
}
項目結(jié)構(gòu)
MyApp├──build.gradle├──settings.gradle└──app├──build.gradle├──build├──libs└──src└──main├──java│ ? └──com.package.myapp└──res├──drawable├──layout└──etc.
Gradle Wrapper結(jié)構(gòu)(這些新建項目時都添加給了用戶袄膏,不需要重新添加)
myapp/ ├── gradlew ?├── gradlew.bat └── gradle/wrapper/ ? ? ├── gradle-wrapper.jar ? ? └── gradle-wrapper.properties
運行build任務 - 列出所有可用任務
$./gradlew tasks
生成App-debug.apk任務
$./gradlew assembleDebug# Apk路徑: MyApp/app/build/ outputs/apk
手動導入Eclipse-Android項目(自動導入請連續(xù)點“下一步”)
在項目路徑下創(chuàng)建build.gradle文件:
buildscript {?
? ? ? ?repositories { ?
? ? ? ? ? ? ? ? ?jcenter() ?
?} ?
dependencies { ??
? ?classpath'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin:'com.android.application'
android { ?
compileSdkVersion22
buildToolsVersion"22.0.1"
sourceSets { ? ??
?main { ? ? ?
?? ?manifest.srcFile'AndroidManifest.xml'
? ? ? ? ? ?java.srcDirs = ['src']
?? ? ? ? ?resources.srcDirs = ['src']
?? ? ? ? ?aidl.srcDirs = ['src']
?? ? ? ? ?renderscript.srcDirs = ['src']
?? ? ? ? ?res.srcDirs = ['res']?
? ? ? ? ?assets.srcDirs = ['assets']
?? ? ?}?
? ? ?androidTest.setRoot('tests')
??}
?}
dependencies { ?
compile fileTree(dir:'libs', include: ['*.jar'])
}
PS也可以復制粘貼Eclipse-Android項目的源代碼到Android Studio的項目里
Gradle所有文件結(jié)構(gòu)
MyApp├──build.gradle├──settings.gradle└──app└──build.gradle
settings.gradle
include':app'
MyApp/build.gradle
buildscript {?
repositories {
?? ? jcenter()
?}?
dependencies {
?? ? classpath'com.android.tools.build:gradle:1.2.3'
}
?}
allprojects {
?repositories {
?? ? jcenter()
??}
}
MyApp/app/build.gradle
applyplugin:'com.android.application'android { compileSdkVersion22
buildToolsVersion"22.0.1"
defaultConfig { ? ?
?applicationId"com.gradleforandroid.gettingstarted"
minSdkVersion14
targetSdkVersion22
versionCode1
versionName"1.0"
}
buildTypes {
?? ? release {
?? ? ? ? minifyEnabled false
?? ? ? ? proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
?}
?}
dependencies {
compilefileTree(dir:'libs', include: ['*.jar'])
?compile'com.android.support:appcompat-v7:22.2.0'
?}
基礎任務
$./gradlew assemble -為所有構(gòu)建類型創(chuàng)建apk$./gradlew check 運行所有的檢查蓖乘,比如說AndroidLint带欢,如果發(fā)現(xiàn)問題可終止任務$./gradlew build 運行以上兩個任務$./gradlew clean -清除生成的apk++++$./gradlew connectedCheck - 在設備上運行測試$./gradlew deviceCheck - 遠程設備運行測試$./gradlew installDebug/installRelease - 在設備商安裝指定版本$./gradlew uninstall - 卸載
Build Types不同版本的參數(shù)設置 - BuildConfig/Resource Value
android { ?
buildTypes {
?? ? ?debug {? ? ? ? ? buildConfigField"String","API_URL","\\"http://test.example.com/api\\""buildConfigField"boolean","LOG_HTTP_CALLS","true"resValue"string","app_name","Example DEBUG"
} ? ? ?
? ? release { ? ? ? ? ?buildConfigField"String","API_URL","\\"http://example.com/api\\""buildConfigField"boolean","LOG_HTTP_CALLS","false"resValue"string","app_name","Example"
}
??}
}
全局設置(項目根目錄的build.gradle)
allprojects { ?
apply plugin:'com.android.application'android {
?? ? ?compileSdkVersion22
buildToolsVersion"22.0.1"
}
?}
設置全局參數(shù)
ext {
??compileSdkVersion =22
buildToolsVersion ="22.0.1"
}
在MyApp/app/build.gradle里面使用參數(shù)
android{
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
默認任務(MyApp/build.gradle)
defaultTasks'clean','assembleDebug'
倉庫
預設配置倉庫
repositories{
mavenCentral()
jcenter()
mavenLocal()
}
遠程倉庫
repositories { maven { ? ? url"http://repo.acmecorp.com/maven2"credentials { ? ? ? ? username'user'password'secretpassword'} } ivy { ? ? url"http://repo.acmecorp.com/repo"}}
本地倉庫
repositories { maven { ? ? url"../repo"}}
本地依賴
項目文件依賴
dependencies { ?compile fileTree(dir:'libs',include: ['*.jar'])}
原生庫結(jié)構(gòu)與配置
# 結(jié)構(gòu):app ├── AndroidManifest.xml └── jniLibs ? ? ├── armeabi ? ? │ ? └── nativelib.so ? ? ├── armeabi-v7a ? ? │ ? └── nativelib.so ? ? ├── mips ? ? │ ? └── nativelib.so ? ? └── x86 ? ? ? ? └── nativelib.so# 配置:android {sourceSets.main { ? ? ?jniLibs.srcDir'src/main/libs'}}
Libray項目
# 修改Android插件:apply plugin:'com.android.library'# settings.gradle新增libray項目:include':app',':library'# app內(nèi)引用library項目:dependencies { ?compile project(':library')}
依賴概念
<待續(xù)>
Android Studio內(nèi)添加依賴
<待續(xù)>
加速構(gòu)建
在gradle.properties里面添加:org.gradle.parallel=true
單元測試
使用JUnit
# 結(jié)構(gòu):app└─── src├─── main│ ├─── java ? ? │ ? ?│ ? ?└─── com.example.app ? ? │ ? ?└───res ? ? └───test└─── java ? ? ? ? ? ? ? └─── com.example.app# 依賴:dependencies {testCompile'junit:junit:4.12'}
使用Robolectric
# 依賴:applyplugin:'org.robolectric'dependencies {
?? ?compile fileTree(dir:'libs',include:['*.jar'])
?? ?compile'com.android.support:appcompat-v7:22.2.0
'testCompile'junit:junit:4.12'
testCompile'org.robolectric:robolectric:3.0'
testCompile'org.robolectric:shadows-support:3.0'
}
# Demo:@RunWith(RobolectricTestRunner.class)@Config(manifest ="app/src/main/AndroidManifest.xml", sdk =18)publicclassMainActivityTest{@Testpublic void clickingButtonShouldChangeText() {AppCompatActivityactivity =Robolectric.buildActivity(MainActivity.class).create().get();Buttonbutton = (Button) activity.findViewById(R.id.button);TextViewtextView = (TextView) activity.findViewById(R.id.label); ? ? button.performClick(); ? ? assertThat(textView.getText().toString(), equalTo(activity.getString(R.string.hello_robolectric))); } }
功能測試
使用Espresso
<待續(xù)>
測試覆蓋度
使用Jacoco
<待續(xù)>
<待續(xù)>
<待續(xù)>
縮減apk文件大小
使用ProGuard
android { buildTypes { ? ? release { ? ? ? ? minifyEnabledtrueproguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'} } }
收縮資源文件 - 自動 (<手動待續(xù)>)
android { buildTypes { ? ? release { ? ? ? ? minifyEnabledtrueshrinkResourcestrue} } }
加速構(gòu)建
org.gradle.parallel=true# 并行構(gòu)建org.gradle.daemon=true# 開啟Gradle守護進程org.gradle.jvmargs=-Xms256m -Xmx1024m# 配置JVM<參照下圖>
使用Profiling
<待續(xù)>
使用Jack(Java Android Compiler Kit) and Jill(Jack Intermediate Library Linker)
<待續(xù)>
忽略Lint
android { ?lintOptions { ? ? ?abortOnErrorfalse}}
使用Ant
<待續(xù)>
app打包 - 進階
分割apk
android { ?splits { ? ? ?density {enabletrueexclude'ldpi','mdpi'compatibleScreens'normal','large','xlarge'} ?} }生成結(jié)果:app-hdpi-release.apkapp-universal-release.apkapp-xhdpi-release.apkapp-xxhdpi-release.apkapp-xxxhdpi-release.apk