標籤:
一)基本配置
build配置
buildscript { repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:1.2.3‘ } }
Android指令碼
apply plugin: ‘com.android.application‘
Android配置
android { compileSdkVersion 22 buildToolsVersion "22.0.1"}
項目結構
MyApp├── build.gradle├── settings.gradle└── app ├── build.gradle ├── build ├── libs └── src └── main ├── java │ └── com.package.myapp └── res ├── drawable ├── layout └── etc.
Gradle Wrapper結構(這些建立項目時都添加給了使用者,不需要重新添加)
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項目(自動匯入請連續點“下一步”)
在項目路徑下建立build.gradle檔案:
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的項目裡
二)自訂配置
Gradle所有檔案結構
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
<待續>
七)建立任務與外掛程式
<待續>
八)配置CI
<待續>
九)自訂配置 - 進階
- 縮減apk檔案大小
使用ProGuardandroid { 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.apkapp-universal-release.apkapp-xhdpi-release.apkapp-xxhdpi-release.apkapp-xxxhdpi-release.apk
文/田浩浩_DockOne(簡書作者)
原文連結:http://www.jianshu.com/p/02cb9a0eb2a0
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。
Android Studio中Gradle使用詳解