詳解Android Studio 3.0的新特性與適配
簡介
Android Studio升級到3.0后,有不少的改動和新特性,先貼出官方的遷移說明。
本文會持續(xù)收集與總結本人在使用Android Studio 3.0進行開發(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默認使用Google's Maven Repository來下載Android Support Library,所以在腳本中要使用google()來加入谷歌倉庫。
工程根目錄/build.gradle的相關配置如下。
buildscript { repositories { google() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0' } }
使用annotationProcessor
從Android Studio 3.0開始,使用annotationProcessor代替apt。不可再使用apt,否則會編譯報錯。
Error:android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProcessor' configuration instead.
比如在Android Studio 3.0之前在application模塊導入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,不用再導入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變量,若無則使用項目名 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í)行此腳本會報錯如下,原因是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
不再設置outputFile屬性,而是設置outputFileName。同時把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
為了改進增量資源處理,Android Gradle插件3.0默認開啟AAPT2。
在舊項目中開啟AAPT2,有時候會報錯,如:
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'),會導致如下錯誤。應該修改為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(風味維度),否則會編譯報錯。
Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.
解決方法是:先定義一個flavorDimensions,之后在每個flavor中指定為這個dimension。
android { flavorDimensions 'core' productFlavors { beta { dimension 'core' } production { dimension 'core' } } }
在設置flavorDimensions之前,最終的Build Variant = Product Flavor + Build Type。而設置之后,最終的Build Variant = 維度1 + 維度2 + ... + 維度n + Build Type。
Kotlin支持
在Android Studio 3.0之前,使用Kotlin需要進行額外的配置。而Android Studio 3.0開始,默認內置支持Kotlin,無需額外配置。
使用Android Studio工具欄中的Code -> Convert Java File To Kotlin File,可將.java文件轉為.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工具欄,File -> Project Structure,修改Source Compatibility和Target Compatibility為1.8。
Project Structure
從Android Studio 3.0起,默認支持Java8,無需額外進行JackOptions配置。
Android Profiler
從Android Studio 3.0起,新增Android Profiler來代替舊的Android Monitor工具。
Android Profiler提供了CPU、Memory和network等三個調試分析工具。
Android Profiler
Android Profiler的詳細使用方法參考官方文檔。
CPU Profiler
Memory Profiler
Network Profiler
Device File Explorer
在Android Studio 3.0主界面的右下角,點開"Device File Explorer",可訪問當前連接設備的文件。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android 異步任務 設置 超時使用handler更新通知功能
這篇文章主要介紹了Android 異步任務 設置 超時使用handler更新通知,文中給大家提到了使用AsyncTask設置請求超時的注意事項 ,需要的朋友可以參考下2017-12-12Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法
這篇文章主要介紹了Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法,涉及Android中TextView控件的相關操作技巧,需要的朋友可以參考下2016-01-01Android Studio生成 Flutter 模板代碼技巧詳解
這篇文章主要為大家介紹了Android Studio生成 Flutter 模板代碼技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
本文主要講解狀態(tài)欄通知Notification、NotificationManager,小編覺得非常詳細,給大家一個參考,希望對大家學習有所幫助。2016-06-06