当前位置: 移动技术网 > 移动技术>移动开发>Android > 浅析Android Studio 3.0 升级各种坑(推荐)

浅析Android Studio 3.0 升级各种坑(推荐)

2019年07月24日  | 移动技术网移动技术  | 我要评论

点击 check for updates 增量更新:

下载完成,会提示更新 您将 gradle 更新至 4.1:

这里建议您对老项目先暂时点击 don't remind me on this project,以防有坑。当然我不入地狱谁入地狱,我点 update,于是问题来了,一直处于下载中,不过,莫担心,我下载好了,公众号聊天界面回复「 gradle-4.1-all 」,下载 gradle-4.1-all.zip 文件,放到:

重启 android studio。

gradle 目录:

mac系统默认:/users/(用户名)/.gradle

windows系统默认:c:\users(用户名).gradle

修改旧项目

首先我们新建项目,看看发生了哪些变化。

1、app/build.gradle

buildtoolsversion:这里没有构建工具的版本 buildtoolsversion 属性了,android studio 3.0 默认情况下,插件会自动为您使用的 android 插件版本使用最低要求的构建工具版本;

implementation:由以前的 compile 改成了 implementation。老版本的构建关键字 compile 被废弃了,而是改成了这两个:api:同 compile 作用一样,即认为本 module 将会泄露其依赖的 module 的内容;

implementation:本 module 不会通过自身的接口向外部暴露其依赖 module 的内容。

2、项目 build.gradle

google():android studio 3.0 现在默认使用 google 的 maven 存储库,而不是依赖于 android sdk manager 来获取 android 支持库,google play 服务,firebase 和其他依赖项的更新;

build.gradle:指定的是gradle插件的版本,由之前的 2.3.3 改成了 3.0.0。

3、gradle-wrapper.properties

gradle-wrapper.properties 中配置的是的 gradle 的版本。

可以对老项目就以上几点进行修改。

butterknife

如果您在 lib 里使用了 butterknife,会遇到以下错误:


网上说将 apply plugin: ‘com.jakewharton.butterknife' 注掉,是能解决问题,但是对于 lib 里使用了 butterknife 依旧报 r2 找不到,还得放开 apply plugin: ‘com.jakewharton.butterknife',最后我只能退而求其次,将 build.gradle 中 3.0.0 改成之前的 2.3.3,解决。

打包自定义 apk 文件名

打包时,要是自定义输出 apk 文件名可以这样做:

buildtypes {
  release {
    minifyenabledfalse
    proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
    applicationvariants.all { variant ->
      if (variant.buildtype.name == 'release') {
        variant.outputs.each { output ->
          def outputfile = output.outputfile
          if (outputfile !=null && outputfile.name.endswith('.apk')) {
            def filename = "sample_v${defaultconfig.versionname}_${releasetime()}_${variant.flavorname}.apk"
            output.outputfile = new file(outputfile.parent, filename)
          }
        }
      }
    }
  }
}

使用 gradle plugin 3.0.0 时报错:

cannot set the value of read-only property 'outputfile' for apkvariantoutputimpl_decorated{apkdata=main{type=main, fullname=release, filters=[]}} of type com.android.build.gradle.internal.api.apkvariantoutputimpl. open file

需要修改 each() 和 outputfile() 方法为 all() 和 outputfilename:

buildtypes {
  release {
    minifyenabledfalse
    proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
    applicationvariants.all { variant ->
      if (variant.buildtype.name == 'release') {
        variant.outputs.all { output ->
          def outputfile = output.outputfile
          if (outputfile !=null && outputfile.name.endswith('.apk')) {
            def filename = "sample_v${defaultconfig.versionname}_${releasetime()}_${variant.flavorname}.apk"
            output.outputfilename = new file(outputfile.parent, filename)
          }
        }
      }
    }
  }
}

补充

build.gradle 和 gradle-wrapper.properties 区别,了解更多:

最后

目前遇到这些坑,欢迎一起吐槽您在升级开发遇到的坑,这次升级又折腾了很久。另外如果需要gradle-4.1-all.zip 文件,公众号「吴小龙同学」聊天界面回复「 gradle-4.1-all 」获取。

ps:下面给大家补充介绍android studio升级到3.0的各种坑。

项目的build.gradle(不是module):

解决方案:

关掉aapt2即可

在project/gradle.properties中添加 android.enableaapt2=false

总结

以上所述是小编给大家介绍的浅析android studio 3.0 升级各种坑(推荐),希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网