欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

浅谈Android Studio 3.0 的一些小变化

程序员文章站 2023-11-20 16:46:22
前言 一大早还在北京拥挤的地铁里,我的cto闫哥在微信里给我发了一条信息:android studio 3.0发布了。 为什么会这么关注android studio 3...

前言

一大早还在北京拥挤的地铁里,我的cto闫哥在微信里给我发了一条信息:android studio 3.0发布了。

为什么会这么关注android studio 3.0 的版本发布呢?主要是因为公司即将开发的新app准备使用kotlin语言,而android studio 3.0 已经把kotlin的语言支持内置进去了,这样就省去了很多的麻烦,如果你还没接触过kotlin语言,可以去百度一下 他们的官网,如果你现在使用的java语言,那么你真是太幸运了,因为kotlin对于你来说,将会非常简单,例如像我这样的,两三天就可以几乎应付大部分的开发了。

这里就不对kotlin语言做过多的描述了,今天的重点,是我升级到android studio 3.0 以后的故事。

正文

来到公司打开电脑,升级android studio到3.0版本,编译目前的工程。哎呀呀我擦擦,为什么报了好多的错?别着急,我们慢慢解决这些问题。

浅谈Android Studio 3.0 的一些小变化

android studio的自带gradle版本是4.1,插件版本是3.0.0,所以如果你使用的是老版本,就会出现一些小的兼容问题,我们看看报了哪些错误呢:

问题1

error:(72, 0) cannot set the value of read-only property 'outputfile' for apkvariantoutputimpl_decorated{apkdata=main{type=main, fullname=appdebug, filters=[]}} of type com.android.build.gradle.internal.api.apkvariantoutputimpl.

outputfile是只读属性,不可以对他进行修改

看一下我的gradle里面的代码:

// 定义生成的apk的名称
def apkname;

buildtypes {
 release {
  ...
  // 定义release版本生成的apk的名字
  apkname = "xxx" + version_name + "_release.apk";
 }
 debug {
  ...
  // 定义debug版本生成的apk的名字
  apkname = "ugirls_" + version_name + "_debug.apk";
 }
}

// 修改apk build的名字
android.applicationvariants.all { variant ->
 variant.outputs.each { output ->
  def outputfile = output.outputfile
  if (outputfile != null && outputfile.name.endswith('.apk')) {
    //这里使用之前定义apk文件名称
    output.outputfile = new file(outputfile.parent, apkname)
  }
 }
}

这段代码的功能是修改build命令生成的apk的名称,因为outputfile变成只读属性,所以报错。

修改后:

// 之前代码保持不变
// 修改apk build的名字
android.applicationvariants.all { variant ->
 variant.outputs.all {
  if (outputfilename.endswith('.apk')) {
   //这里使用之前定义apk文件名称
   outputfilename = apkname
  }
 }
}

把each修改为all,然后通过outputfilename修改生成的apk的名称。

如果你提示没有找到all方法或者是未找到outputfilename,你可以先把这个功能注释掉,等其他问题都解决了,再打开就可以解决这个问题了。

问题2

error:all flavors must now belong to a named flavor dimension. learn more at https://d.android.com/r/tools/flavordimensions-missing-error-message.html

所有的flavor属性应当属于同一个命名空间

看着问题似乎有点深奥,其实就是需要我们为flavors设置一个版本,统一使用相同版本的flavors。

defaultconfig {
 targetsdkversion:***
 minsdkversion :***
 versioncode:***
 versionname :***
 //为flavor设置一个版本,命名是随意的
 flavordimensions "versioncode"
}

问题3

浅谈Android Studio 3.0 的一些小变化

有些库不能被正常引用,例如我使用的multidex,在上面的截图中已经提示我们如何解决这个问题

buildscript {
 repositories {
  ...
  // 添加google库的依赖
  google()
 }
 dependencies {
  ...
 }
}

问题4

error:(2638) error: style attribute '@android:attr/windowenteranimation' not found.

提示我们找不到@android:attr/windowenteranimation,因为已经不支持@开头使用android自带的属性,我们只要把@符号删掉就可以了。

修改前:

<style name="toaststyle" parent="android:animation">
  <item name="@android:windowenteranimation">@anim/push_fade_in</item>
  <item name="@android:windowexitanimation">@anim/push_fade_out</item>
</style>

修改后:

<style name="toaststyle" parent="android:animation">
  <item name="android:windowenteranimation">@anim/push_fade_in</item>
  <item name="android:windowexitanimation">@anim/push_fade_out</item>
</style>

问题5

error:execution failed for task ':app:javaprecompileappdebug'.
annotation processors must be explicitly declared now. the following dependencies on the compile classpath are found to contain annotation processor. please add them to the annotationprocessor configuration.
- butterknife-6.1.0.jar (com.jakewharton:butterknife:6.1.0)
alternatively, set android.defaultconfig.javacompileoptions.annotationprocessoroptions.includecompileclasspath = true to continue with previous behavior. note that this option is deprecated and will be removed in the future.
see for more details.

好多的错误日志啊,其实最关键的只有前两行:

使用注解编译库,需要显示的声明,而我正在使用的butterknife是含有注解编译功能的,但是并没有声明。

解决办法:

android {

 defaultconfig {
 // 声明需要使用注解功能
  javacompileoptions { annotationprocessoroptions { includecompileclasspath = true } }
  ...
 }
}

其他的变化

通过刚才的修改,我的工程已经运行起来了,但是发现了android studio 3.0 的几个小变化。

变化1

warning:one of the plugins you are using supports java 8 language features. to try the support built into the android plugin, remove the following from your build.gradle:
apply plugin: 'me.tatarka.retrolambda'

从警告上看,希望我移除这个插件,于是我到官网上查看了一下信息:

if android studio detects that your project is using jack, retrolambda, or dexguard, the ide uses java 8 support provided by those tools instead.

如果android studio发现你的工程中使用jack ,retrolambda 或dexguard,编辑器会使用java8支持,替换这个工具。
因为我使用me.tatarka.retrolambda第三方框架,所以就出现了这个,我们只要删除相关的配置就可以了。

变化2

提示有更高版本你的第三方框架:

浅谈Android Studio 3.0 的一些小变化

上面的截图显示,gson有更高的版本2.8.3,提示我升级gson。这就省去了我们去github上查看是否版本更新的时间,非常的方便。

总结

这就是我今天遇到的问题及解决方案,如果之前有更多问题再补充。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。