AndroidStudio升级Gradle3.0使用

#Gradle

 classpath 'com.android.tools.build:gradle:3.0.1'

 

官方默认搭配是 gradle 4.1,最新4.4.1 ,速度将更快: 

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-all.zip
百度云下载地址:

 gradle 4.1 https://pan.baidu.com/s/1eTyvIkq

 gradle 4.4.1 https://pan.baidu.com/s/1mjkCtjM

#编译方式

原先的 compile 关键词现在变成了 api,但一般情况下不应该使用 api,因为 api 是会向外暴露你引用的依赖,这样会导致 gradle 编译时需要频繁遍历所有嵌套依赖是否有变更,影响编译速度。更推荐的是使用 implementation 替代原先的 compileimplementation 和 api 的区别就是不会暴露引用的依赖。比如 A implementation 引用了 B,如果 B implementation 引用了 C,那么 A 将无法间接引用 C,除非 A 自己也声明引用 C。而如果 A implementation B,B api C,则 A 可以间接引用 C。

使用 implementation 是提升编译速度的关键。如果没有必要引用间接内容的话,应该尽量使用implementation

关于此有一篇写得比官方清楚很多的好文章:

https://stackoverflow.com/questions/44413952/gradle-implementation-vs-api-configuration

另外,原先的 provided 现在改名为 compileOnly,并新增了 runtimeOnly

在Google IO 相关话题的中提到了一个建议,就是依赖首先应该设置为implement的,如果没有错,那就用implement,如果有错,那么使用api指令,这样会使编译速度有所增快。 
参考资料

需要科学上网

Google I/O 2017 - How speed up Gradle builds (FULL VIDEO)

Google I/O 2017 - How speed up Gradle builds (NEW GRADLE PLUGIN 3.0.0 PART ONLY)

Google I/O 2017 - How speed up Gradle builds (reference to 1*)


#设置生成的应用文件名

Gradle 3.0 更改了生成应用文件名称的方式,更简单直观:

applicationVariants.all { variant ->
    variant.outputs.all {
        def apkName = 'AppName' + '-' + variant.versionName
        if (!variant.flavorName.isEmpty()) {
            apkName += ('-' + variant.flavorName)
        }
        outputFileName = apkName + '.apk'
    }
}

#Java8支持

Gradle 带来了新的 Java 8 兼容方案 desugar,启用方式十分简单,只要在 gradle android 层次之下加入如下代码即可:

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

目前 desugar 兼容支持的内容有:

  • Lambda expressions
  • Method References
  • Type Annotations
  • Default and static interface methods
  • Repeating annotations

不支持 stream、function

禁用 desugar:

android.enableDesugar=false

更多详情:https://developer.android.com/studio/write/java8-support.html

参考:http://blog.csdn.net/jabony/article/details/78930174

#配置构建多渠道

flavor 配置有变,官方中文文档,很详细,见文档就是了:

https://developer.android.com/studio/build/build-variants.html?hl=zh-cn

参考:http://blog.csdn.net/jabony/article/details/78932024

#APPT2的支持

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

android.enableAapt2=false

参考:http://blog.csdn.net/jabony/article/details/78933955

#Support Library 26.1.0

  • Fragment and FragmentActivity (the base class for AppCompatActivity) now implement the LifecycleOwner interface from Architecture Components.

  • All instances of the findViewById() method now return T instead of View. This change has the following implications

  • FragmentManager and Fragment have an isStateSaved() method to allow querying whether or not a transaction will be allowed without state loss. This is especially useful to check when handling an onClick() event before executing any transaction.

  • Path motion is supported in AnimatedVectorDrawableCompat. Path motion allows one object animator to change two properties at the same time based on one path; the path is specified as android:pathData in the animator’s XML).

  • New FlingAnimation that supports animating with an initial velocity and smoothly slows down.、

  • Support libs v26 版本完美解决了 AppBarLayout$ScrollingViewBehavior fling 的问题,介绍和演示相关微博:http://weibo.com/2263023493/FkQscgdRt?from=page_1005052263023493_profile

  • ResourcesCompat.getFont allows loading font resources—including font-family XML—that may be used with TextView.setTypeface().

  • Downloadable fonts

  • Emoji compatibility library(19+)

  • Autosizing TextView

  • PreferenceDataStore now allows you to implement your own preferences storage, set with new methods in Preference and PreferenceManager.

#其他

  • 对 ConstraintLayout 编辑更好的支持,之前 ConstraintLayout XML 内容容易遇到一些属性值被破坏的情况,3.0 不再发生。

#Lifecycle Arch






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值