【重学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,然后再进行同步即可。

相关推荐
程序员码歌39 分钟前
【零代码AI编程实战】AI灯塔导航-总结篇
android·前端·后端
书弋江山2 小时前
flutter 跨平台编码库 protobuf 工具使用
android·flutter
任磊abc3 小时前
vscode无法检测到typescript环境解决办法
ide·vscode·typescript
来来走走4 小时前
Flutter开发 webview_flutter的基本使用
android·flutter
Jerry说前后端5 小时前
Android 组件封装实践:从解耦到架构演进
android·前端·架构
louisgeek6 小时前
Android OkHttp Interceptor
android
大王派来巡山的小旋风6 小时前
Kotlin基本用法三
android·kotlin
Jerry说前后端7 小时前
Android 移动端 UI 设计:前端常用设计原则总结
android·前端·ui
bytebeats7 小时前
Jetpack Compose 1.9: 核心新特性简介
android·android jetpack
Icey_World7 小时前
Mysql笔记-错误条件\处理程序
android