【重学Android】03.高版本 Android Studio 不能使用引用库资源ID的问题

问题背景

由于直接下载的最新版本Android Studio,然后直接创建的新项目,因此默认的工程配置相比以前的老版本有了不少的变化,Gradle的新版本使用,导致一些配置项也发生了变化,加上谷歌针对gradle.properties文件的一些配置也有了变化。

而这次我因为使用了一些第三方库,然后有些资源想偷懒,直接引用第三方库中的,结果发现代码自动提示怎么都弹不出来,一开始还以为工程出问题了,但是几番折腾后发现还是不行,这才意识到可能是哪个配置出了问题。并且一直提示如下信息:

bash 复制代码
Attribute value must be constant

解决

经过一番搜索之后,发现根本原因是因为新版本 Android Studio 将 R 类中的资源设置成了非 final 修饰的变量,这就是导致 switch case 中无法作为判断条件的原因。

然后在gradle.properties文件中会看到这样一段描述和一个配置项。

bash 复制代码
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonFinalResIds=true

就是这两个配置项导致AS不会将第三方库中的资源ID编译为Final类型的。我们只需要将值改为false,然后再进行同步即可。

相关推荐
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿2 天前
Android MediaPlayer 笔记
android
Jony_2 天前
Android 启动优化方案
android
阿巴斯甜2 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇2 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android