简介
Android Studio升级到3.0后,有不少的改动和新特性,先贴出。
本文会持续收集与总结本人在使用Android Studio 3.0进行开发的过程中所遇到的问题。
版本配置
Gradle版本
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中执行此脚本会报错如下,原因是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推出了,而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,从而开发者能使用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的详细使用方法参考官方文档。
Device File Explorer
在Android Studio 3.0主界面的右下角,点开"Device File Explorer",可访问当前连接设备的文件。