詳解Android Studio 3.0的新特性與適配
簡介
Android Studio升級到3.0后,有不少的改動和新特性,先貼出官方的遷移說明。
本文會持續(xù)收集與總結(jié)本人在使用Android Studio 3.0進(jìn)行開發(fā)的過程中所遇到的問題。
版本配置
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來下載Android Support Library,所以在腳本中要使用google()來加入谷歌倉庫。
工程根目錄/build.gradle的相關(guān)配置如下。
buildscript { repositories { google() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0' } }
使用annotationProcessor
從Android Studio 3.0開始,使用annotationProcessor代替apt。不可再使用apt,否則會編譯報(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變量,若無則使用項(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í)行此腳本會報(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)開啟AAPT2。
在舊項(xiàng)目中開啟AAPT2,有時(shí)候會報(bào)錯(cuò),如:
Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
可在gradle.properties中加入以下配置來禁用AAPT2。
android.enableAapt2=false
新的依賴配置
Gradle 3.4推出了新的Java Library Plugin配置,而Android Gradle插件3.0是使用Gradle 4.1的,因此,需要注意更改為新的依賴配置。
舊的依賴配置,如compile project(':base-library'),會導(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開始,如果build.gradle中有自定義的productFlavors配置,需要添加自定義的flavorDimensions(風(fēng)味維度),否則會編譯報(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開始,默認(rèn)內(nèi)置支持Kotlin,無需額外配置。
使用Android Studio工具欄中的Code -> Convert Java File To Kotlin File,可將.java文件轉(zhuǎn)為.kt文件。
Java8支持
從Android Studio 2.1起,官方通過Jack來支持Java8,從而開發(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。
Project Structure
從Android Studio 3.0起,默認(rèn)支持Java8,無需額外進(jìn)行JackOptions配置。
Android Profiler
從Android Studio 3.0起,新增Android Profiler來代替舊的Android Monitor工具。
Android Profiler提供了CPU、Memory和network等三個(gè)調(diào)試分析工具。
Android Profiler
Android Profiler的詳細(xì)使用方法參考官方文檔。
CPU Profiler
Memory Profiler
Network Profiler
Device File Explorer
在Android Studio 3.0主界面的右下角,點(diǎn)開"Device File Explorer",可訪問當(dāng)前連接設(shè)備的文件。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
實(shí)現(xiàn)倒計(jì)時(shí)每隔1秒,變換一下時(shí)間,截圖如下,感興趣的朋友想看下實(shí)現(xiàn)代碼,希望對你學(xué)習(xí)有所幫助2013-06-06Android使用Spinner實(shí)現(xiàn)城市級聯(lián)下拉框
這篇文章主要為大家詳細(xì)介紹了Android使用Spinner實(shí)現(xiàn)城市級聯(lián)下拉框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息
本篇文章主要介紹了android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息,就是實(shí)現(xiàn)在通知欄看到下載進(jìn)度。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Android 異步任務(wù) 設(shè)置 超時(shí)使用handler更新通知功能
這篇文章主要介紹了Android 異步任務(wù) 設(shè)置 超時(shí)使用handler更新通知,文中給大家提到了使用AsyncTask設(shè)置請求超時(shí)的注意事項(xiàng) ,需要的朋友可以參考下2017-12-12Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法
這篇文章主要介紹了Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法,涉及Android中TextView控件的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01Android Studio生成 Flutter 模板代碼技巧詳解
這篇文章主要為大家介紹了Android Studio生成 Flutter 模板代碼技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
本文主要講解狀態(tài)欄通知Notification、NotificationManager,小編覺得非常詳細(xì),給大家一個(gè)參考,希望對大家學(xué)習(xí)有所幫助。2016-06-06