簡(jiǎn)介
Android Studio升級(jí)到3.0后,有不少的改動(dòng)和新特性,先貼出官方的遷移說(shuō)明。
本文會(huì)持續(xù)收集與總結(jié)本人在使用Android Studio 3.0進(jìn)行開(kāi)發(fā)的過(guò)程中所遇到的問(wèn)題。
版本配置
Gradle版本
Android Studio 3.0需要的Gradle版本至少為4.1。
如果是使用gradle wrapper,則
工程根目錄/gradle/wrapper/gradle-wrapper.properties
中的distributionUrl字段為https\://services.gradle.org/distributions/gradle-4.1-all.zip
。
Android Gradle插件版本
Android Studio 3.0需要Android Gradle插件版本為3.0.0。
Android Studio 3.0默認(rèn)使用Google's Maven Repository來(lái)下載Android Support Library,所以在腳本中要使用
google()
來(lái)加入谷歌倉(cāng)庫(kù)。工程根目錄/build.gradle
的相關(guān)配置如下。
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
使用annotationProcessor
- 從Android Studio 3.0開(kāi)始,使用annotationProcessor代替apt。不可再使用apt,否則會(huì)編譯報(bào)錯(cuò)。
Error:android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProcessor' configuration instead.
- 比如在Android Studio 3.0之前在application模塊導(dǎo)入ButterKnife 8.4.0的gradle配置如下。
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
- 而在Android Studio 3.0中,使用annotationProcessor代替apt,不用再導(dǎo)入android-apt插件。
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
修改apk名稱
- 常用的修改輸出的apk文件的名稱的腳本如下。
def apkBaseName() {
// 先查找project.ext.apkName變量,若無(wú)則使用項(xiàng)目名
if(project.hasProperty("apkName")) {
return project.apkName
} else {
return project.name
}
}
def buildTime() {
return new Date().format("yyyyMMdd")
}
def delUnderline(String str) {
def result = str.startsWith("_") ? str.substring(1) : str
return result.endsWith("_") ? result.substring(0, result.length() - 1) : result
}
android.applicationVariants.all { variant -> // ApplicationVariant
variant.outputs.each { output -> // BaseVariantOutput
def file = output.outputFile
if(file != null && file.name.endsWith(".apk")) {
def flavorName = delUnderline(variant.flavorName)
def buildTypeName = delUnderline(variant.buildType.name)
def apkFile = new File(file.parent, "${apkBaseName()}_" +
"${buildTypeName.empty ? "" : buildTypeName + "_"}" +
"${flavorName.empty ? "" : flavorName + "_"}" +
"v${variant.versionName}_" +
"${buildTime()}.apk")
output.outputFile = apkFile
}
}
}
- 在Android Studio 3.0中執(zhí)行此腳本會(huì)報(bào)錯(cuò)如下,原因是ApkVariantOutputImpl的outputFile屬性改為只讀。
Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl
- 不再設(shè)置outputFile屬性,而是設(shè)置outputFileName。同時(shí)把
each()
改為all()
。
android.applicationVariants.all { variant -> // ApplicationVariant
variant.outputs.all {
if (outputFileName.endsWith(".apk")) {
def flavorName = delUnderline(variant.flavorName)
def buildTypeName = delUnderline(variant.buildType.name)
outputFileName = "fileName"
}
}
}
AAPT2
為了改進(jìn)增量資源處理,Android Gradle插件3.0默認(rèn)開(kāi)啟AAPT2。
在舊項(xiàng)目中開(kāi)啟AAPT2,有時(shí)候會(huì)報(bào)錯(cuò),如:
Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
- 可在
gradle.properties
中加入以下配置來(lái)禁用AAPT2。
android.enableAapt2=false
新的依賴配置
Gradle 3.4推出了新的Java Library Plugin配置,而Android Gradle插件3.0是使用Gradle 4.1的,因此,需要注意更改為新的依賴配置。
舊的依賴配置,如
compile project(':base-library')
,會(huì)導(dǎo)致如下錯(cuò)誤。應(yīng)該修改為implementation project(':base-library')
。
Error:Cannot choose between the following configurations of project :base-library:
- debugApiElements
- debugRuntimeElements
- releaseApiElements
- releaseRuntimeElements
flavor
- 從Android Gradle插件3.0開(kāi)始,如果
build.gradle
中有自定義的productFlavors配置,需要添加自定義的flavorDimensions(風(fēng)味維度),否則會(huì)編譯報(bào)錯(cuò)。
Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.
- 解決方法是:先定義一個(gè)flavorDimensions,之后在每個(gè)flavor中指定為這個(gè)dimension。
android {
flavorDimensions 'core'
productFlavors {
beta {
dimension 'core'
}
production {
dimension 'core'
}
}
}
- 在設(shè)置flavorDimensions之前,最終的Build Variant = Product Flavor + Build Type。而設(shè)置之后,最終的Build Variant = 維度1 + 維度2 + ... + 維度n + Build Type。
Kotlin支持
在Android Studio 3.0之前,使用Kotlin需要進(jìn)行額外的配置。而Android Studio 3.0開(kāi)始,默認(rèn)內(nèi)置支持Kotlin,無(wú)需額外配置。
使用Android Studio工具欄中的Code -> Convert Java File To Kotlin File,可將
.java
文件轉(zhuǎn)為.kt
文件。
Java8支持
- 從Android Studio 2.1起,官方通過(guò)Jack來(lái)支持Java8,從而開(kāi)發(fā)者能使用Lambda等特性。
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
jackOptions {
enabled true
}
}
}
- 可在Android Studio工具欄,F(xiàn)ile -> Project Structure,修改Source Compatibility和Target Compatibility為1.8。
- 從Android Studio 3.0起,默認(rèn)支持Java8,無(wú)需額外進(jìn)行JackOptions配置。
Android Profiler
從Android Studio 3.0起,新增Android Profiler來(lái)代替舊的Android Monitor工具。
Android Profiler提供了CPU、Memory和network等三個(gè)調(diào)試分析工具。
-
Android Profiler的詳細(xì)使用方法參考官方文檔。
Device File Explorer
- 在Android Studio 3.0主界面的右下角,點(diǎn)開(kāi)"Device File Explorer",可訪問(wèn)當(dāng)前連接設(shè)備的文件。