build配置php
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3' } }
Android腳本css
apply plugin: 'com.android.application'
Android配置java
android {
compileSdkVersion 22 buildToolsVersion "22.0.1" }
項目結構android
MyApp ├── build.gradle ├── settings.gradle └── app ├── build.gradle ├── build ├── libs └── src └── main ├── java │ └── com.package.myapp └── res ├── drawable ├── layout └── etc.
Gradle Wrapper結構(這些新建項目時都添加給了用戶,不須要從新添加)api
myapp/
├── gradlew
├── gradlew.bat
└── gradle/wrapper/
├── gradle-wrapper.jar
└── gradle-wrapper.properties
運行build任務 - 列出全部可用任務ruby
$ ./gradlew tasks
生成App-debug.apk任務bash
$ ./gradlew assembleDebug # Apk路徑: MyApp/app/build/ outputs/apk
手動導入Eclipse-Android項目(自動導入請連續點「下一步」)
在項目路徑下建立build.gradle文件:app
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3' } } apply plugin: 'com.android.application' android { compileSdkVersion 22 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的項目裏jvm
Gradle全部文件結構maven
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
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.gradleforandroid.gettingstarted" minSdkVersion 14 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.0' }
基礎任務
$ ./gradlew assemble -爲全部構建類型建立apk $ ./gradlew check 運行全部的檢查,好比說Android Lint,若是發現問題可終止任務 $ ./gradlew build 運行以上兩個任務 $ ./gradlew clean -清除生成的apk ++++ $ ./gradlew connectedCheck - 在設備上運行測試 $ ./gradlew deviceCheck - 遠程設備運行測試 $ ./gradlew installDebug/installRelease - 在設備商安裝指定版本 $ ./gradlew uninstall - 卸載
Build Types不一樣版本的參數設置 - 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 { compileSdkVersion 22 buildToolsVersion "22.0.1" } }
設置全局參數
ext {
compileSdkVersion = 22 buildToolsVersion = "22.0.1" }
在MyApp/app/build.gradle裏面使用參數
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']) }
原生庫結構與配置
# 結構: 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內引用library項目: dependencies { compile project(':library') }
依賴概念
<待續>
Android Studio內添加依賴
<待續>
在gradle.properties裏面添加: org.gradle.parallel=true
單元測試
使用JUnit
# 結構: app └─── src ├─── main │ ├─── java │ │ └─── com.example.app │ └───res └─── test └─── java └─── com.example.app # 依賴: dependencies { testCompile 'junit:junit:4.12' }
使用Robolectric
# 依賴: apply plugin: '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) public class MainActivityTest { @Test public void clickingButtonShouldChangeText() { AppCompatActivity activity = Robolectric.buildActivity(MainActivity.class).create().get(); Button button = (Button) activity.findViewById(R.id.button); TextView textView = (TextView) activity.findViewById(R.id.label); button.performClick(); assertThat(textView.getText().toString(), equalTo(activity.getString(R.string.hello_robolectric))); } }
功能測試
使用Espresso
<待續>
測試覆蓋度
使用Jacoco
<待續>
<待續>
<待續>
android {
buildTypes {
release {
minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
收縮資源文件 - 自動 (<手動待續>) android {
buildTypes {
release {
minifyEnabled true shrinkResources true } } }
加速構建
org.gradle.parallel=true # 並行構建 org.gradle.daemon=true # 開啓Gradle守護進程 org.gradle.jvmargs=-Xms256m -Xmx1024m # 配置JVM<參照下圖>
使用Profiling
<待續>
使用Jack(Java Android Compiler Kit) and Jill(Jack Intermediate Library Linker)
<待續>
忽略Lint
android {
lintOptions {
abortOnError false } }
使用Ant
<待續>
app打包 - 進階
分割apk
android {
splits {
density {
enable true exclude 'ldpi', 'mdpi' compatibleScreens 'normal', 'large', 'xlarge' } } } 生成結果: app-hdpi-release.apk app-universal-release.apk app-xhdpi-release.apk app-xxhdpi-release.apk app-xxxhdpi-release.apk