资源传递性:android.nonTransitiveRClass

QuibblerQuibbler 2025-05-23 约 4 分钟 1791 次阅读

资源传递性:android.nonTransitiveRClass

android.nonTransitiveRClass 是 Android Gradle 插件中的一个属性,用于控制 R 类的生成和使用。 注意:android.nonTransitiveRClass 只适用于 Android Gradle 插件 3.5.0 及以上版本。

当 android.nonTransitiveRClass 设置为 true 时,Gradle 会生成一个非传递的 R 类,这意味着: 只有当前模块的资源会被编译到 R 类中, 依赖模块的资源不会被编译到 R 类中,这可以帮助避免资源冲突和重复定义的问题。 

# 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

这也是默认值,只会在资源所属module的包名下生成对应R文件,其他module引用只能通过原module的包名引用。

当 android.nonTransitiveRClass 设置为 false 时,Gradle 会生成一个传递的 R 类,这意味着: 当前模块的资源和依赖模块的资源都会被编译到 R 类中 。

# 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=false

如遇到Android 不能引用子module资源文件,可以将此设置为false。

如果有资源冲突或重复定义,Gradle 会抛出错误,通常需要将 android.nonTransitiveRClass 设置为 true,以避免资源冲突和重复定义的问题。 

更多字段详见:https://developer.android.google.cn/build/releases/past-releases/agp-8-0-0-release-notes?hl=zh-cn

android.nonFinalResIdstruefalseAGP 8.0 默认会生成包含非最终字段的 R 类。
android.nonTransitiveRClasstruefalseAGP 8.0 仅为当前模块中定义的资源生成 R 类。

相关推荐

获取系统SDK版本、判断手机ROM
精选Code

获取系统SDK版本、判断手机ROM

Build获取系统SDK版本Android中部分API的使用,需要在特定的SDK版本之后才能使用,因此在兼容老版本SDK的时候,经常需要判断API的版本。各种Android版本的对应关系参考《Android各版本对应的SDK版本》判断手机ROM有时候需要判断手机系统的ROM,检测ROM是MIUI、EMUI还是Flyme,可以使用getprop命令,去系统build.prop文件(关于build.p

4.3k
AndroidStudio中各种中文乱码问题
精选Code

AndroidStudio中各种中文乱码问题

AndroidStudio中各种中文乱码问题1、编译Java错误信息乱码在出现这个Annotation processors must be explicitly declared now...问题的时候,正好也发现AndroidStudio Build Output错误信息都乱码。经常遇到各种问题,习惯了,现在遇到的问题,都是以后的答案。 1.1、修改项目build.gradle(无效)在整个p

2.8k
Function Calling 详解:让大模型调用你的函数
精选Code

Function Calling 详解:让大模型调用你的函数

问大模型“北京今天多少度”,它只能用训练数据里的旧知识猜,或者老实承认不知道。但如果告诉它“我这里有个查天气的接口”,它会说:“帮我调用 get_weather,城市参数填北京。”你的程序执行调用,把结果递回去,它再组织成一句自然的回答。Function Calling(函数调用)让模型从“只会聊天”进化为“会办事”——它是大模型连接外部世界的第一块基石,也是一切 Agent 系统的地基。本文按“…

108