Android Studio 升级3.0 ,填坑记录

本文记录了升级Android Studio 3.0过程中遇到的问题,包括outputFile属性只读、flavorDimensions缺失、库引用问题、注解处理器声明、AAPT2错误以及Java 8支持。解决方案包括修改gradle代码、设置flavorDimensions、添加注解处理器声明、调整依赖版本等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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里面的代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

// 定义生成的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变成只读属性,所以报错。

修改后:

?

1

2

3

4

5

6

7

8

9

10

// 之前代码保持不变

// 修改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。

?

1

2

3

4

5

6

7

8

defaultConfig {

 targetSdkVersion:***

 minSdkVersion :***

 versionCode:***

 versionName :***

 //为flavor设置一个版本,命名是随意的

 flavorDimensions "versionCode"

}

问题3

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

?

1

2

3

4

5

6

7

8

9

10

buildscript {

 repositories {

  ...

  // 添加google库的依赖

  google()

 }

 dependencies {

  ...

 }

}

问题4

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

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

修改前:

?

1

2

3

4

<stylename="ToastStyle"parent="android:Animation">

  <itemname="@android:windowEnterAnimation">@anim/push_fade_in</item>

  <itemname="@android:windowExitAnimation">@anim/push_fade_out</item>

</style>

修改后:

?

1

2

3

4

<stylename="ToastStyle"parent="android:Animation">

  <itemname="android:windowEnterAnimation">@anim/push_fade_in</item>

  <itemname="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 https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.

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

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

解决办法:

?

1

2

3

4

5

6

7

8

android {

 

 defaultConfig {

 // 声明需要使用注解功能

  javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }

  ...

 }

}

问题6:

Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error:

  •  
Error:Execution failed for task ':app:mergeDebugResources'.
> Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
  • 在项目的gradle.properties中:
android.enableAapt2=false

其他的变化

通过刚才的修改,我的工程已经运行起来了,但是发现了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

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

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


 

总结

 

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

 

欢迎小伙伴们加入 技术交流群: 168786059                          

加群请备注(技术交流)

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值