一直在使用Android studio
2.3.1版本,因为工作开发使用的都是这个版本,所以在经历了N次提示更新后,依旧还是没有升级,直到今天终于忍不住了,因为Android Studio
3.2版本在这个月发布了,增加了很多新特性,具体参见官方介绍吧:

https://android-developers.googleblog.com/2018/09/android-studio-32.html?linkId=57299773

<https://android-developers.googleblog.com/2018/09/android-studio-32.html?linkId=57299773>

<>一、下载并安装Android studio 3.2

<>1.1 下载:

Android studio 3.2官方下载页:
https://developer.android.com/studio/?utm_source=android-studio
<https://developer.android.com/studio/?utm_source=android-studio>

Android studio 3.2 windows x64 下载直达
Windows(64-bit)
android-studio-ide-181.5014246-windows.exe
Recommended
923 MB
978673a7babf51a9dca67729213c178995c1039a496dc1dfccb4c095b842c753

https://dl.google.com/dl/android/studio/install/3.2.0.26/android-studio-ide-181.5014246-windows.exe

<https://dl.google.com/dl/android/studio/install/3.2.0.26/android-studio-ide-181.5014246-windows.exe>

<>1.2 安装

双击下载好的android-studio-ide-181.5014246-windows.exe开始安装
提示删除旧版本 选择Next


警告要删除指定目录下的文件 选择是


等待安装完毕


提示你导入配置文件: 选择原来版本的配置文件即可


到此安装完毕,打开Android Studio 3.2版本后,会提示gradle或者build tool需要更新,按照提示更新即可。

<>二、Gradle 版本升级

Android studio 项目中需要更改配置的地方

<>2.1 repositories 节点配置

Gradle Plugin 升级到 3.0.0 及以上,修改 project/build.gradle 文件
buildscript { repositories { jcenter() google()//增加 } dependencies { ...
classpath'com.android.tools.build:gradle:3.2.0' ... } } allprojects {
repositories{ jcenter() google()//增加 } }
<>2.2 更新Gradle 版本

Gradle 需要升级到 4.1 及以上,我选择了最新的4.6
修改 project/gradle/gradle-wrapper.properties 文件:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
<>2.3 依赖关键字变化

在Moudel的build.grable的dependencies领域中
3.0之前:
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations' }) compile
'com.android.support:appcompat-v7:26.+' compile
'com.android.support:support-v4:26.+' compile
'com.android.support:recyclerview-v7:26.+' compile
'com.android.support:cardview-v7:26.+' compile
'com.github.bumptech.glide:glide:3.7.0' testCompile 'junit:junit:4.12' compile
'ru.bartwell:exfilepicker:2.1' }
3.0之后:
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation(
'com.android.support.test.espresso:espresso-core:2.2.2', { exclude group:
'com.android.support', module: 'support-annotations' }) implementation
'com.android.support:appcompat-v7:26.+' implementation
'com.android.support:support-v4:26.+' implementation
'com.android.support:recyclerview-v7:26.+' implementation
'com.android.support:cardview-v7:26.+' implementation
'com.github.bumptech.glide:glide:3.7.0' testImplementation 'junit:junit:4.12'
implementation'ru.bartwell:exfilepicker:2.1' }
注:需要将androidTestCompile 更改为androidTestImplementation,否则会报以下提示:

Configuration ‘androidTestCompile’ is obsolete and has been replaced
with ‘androidTestImplementation’ and ‘androidTestApi’. It will be
removed at the end of 2018. For more information see:
http://d.android.com/r/tools/update-dependency-configurations.html
<http://d.android.com/r/tools/update-dependency-configurations.html>

api 对应之前的 compile 关键字,功能一模一样。会传递依赖,导致 gradle 编译的时候遍历整颗依赖树
implementation 对应之前的 compile ,与 api 类似,关键区别是不会有依赖传递
compileOnly 对应之前的 provided,依赖仅用于编译期不会打包进最终的 apk 中
runtimeOnly 对应之前的 apk,与上面的 compileOnly 相反(现在已弃用)
androidTestImplementation 对应之前的androidTestCompile
testImplementation 对应之前的testCompile
关于 implementation 与 api 的区别,主要在依赖是否会传递上。如:A 依赖 B,B 依赖 C,若使用api则 A 可以引用 C,而
implementation 则不能引用。
这里更推荐用 implementation,一是不会间接的暴露引用,清晰知道目前项目的依赖情况;二是可以提高编译时依赖树的查找速度,进而提升编译速度。

<>2.4 打包时重命名APK
def releaseTime() { return new Date().format("yyyy.MM.dd", TimeZone.getTimeZone
("UTC")) }
在Module的build.gradle的android领域中使用如下代码
3.0以前
applicationVariants.all{ variant -> variant.outputs.each{ output -> def
outputFile=output.outputFile if(outputFile!=null && outputFile.name.endsWith(
'.apk')) { def fileName="APP_NAME
v${defaultConfig.versionName}-${releaseTime()}.apk" output.outputFile=new File(
outputFile.parent,fileName) } } }
3.0以后
variant.outputs.each 更改为variant.outputs.all
由于 outputFile 属性变为只读,需要进行如下修改,直接对 outputFileName 属性赋值即可:
applicationVariants.all { variant -> variant.outputs.all { output -> def
outputFile=output.outputFile if(outputFile!=null && outputFile.name.endsWith(
'.apk')) { def fileName if(variant.buildType.name=="release"){ fileName =
"APP_NAME v${defaultConfig.versionName}-${releaseTime()}.apk" }else { fileName =
"APP_NAME v${variant.versionName}_debug.apk" } outputFileName = fileName } } }
<>2.5 多渠道打包

3.0 后 Gradle 添加了 flavorDimensions 属性(直译为风味纬度,可以理解为特点纬度),用来控制多个版本的代码和资源
如果说3.0以前的版本差异化打包更多的是为了厂商定制的,那么3.0以后的版本差异化打包就是在厂商的基础之上加入了机型,渠道等一些参数,变成了多个维度的产品。


也就是说之前的一个产品只有一个参数进行描述的话,现在就可以为其增加多个参数进行配置,比如A厂商的A渠道的A机型、A厂商的B渠道的C机型等,维度越多,产品的样式越发丰富。
官网提供的解决方式是:
在项目 app/build.gradle 文件的android领域中添加 flavorDimensions
// Specifies a flavor dimension. flavorDimensions "color" productFlavors { red
{ // Assigns this product flavor to the 'color' flavor dimension. // This step
is optional if you are using only one dimension. dimension "color" ... } blue {
dimension"color" ... } }
<>2.6 AAPT2

AAPT2 将默认启用,如果遇到离奇的问题,可以尝试禁用,只要在 gradle.properties 中加入:
android.enableAapt2=false

参考:Developers->迁移到 Android Plugin for Gradle 3.0.0
<https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration>

最后总结一下:工具才是提高生产力的根本,便捷高效的工具加上清晰严谨的代码方能制作出一个好的程序。

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信