AGP 升级问题续集来了,不看血亏,真是骚,android内录音频

本文详细分析了不同版本Android Gradle Plugin (AGP) 对R类处理的变化,从3.1.2到4.1.0,探讨了R类在子模块和主模块的产物差异,以及字节码特点。通过对比不同版本,揭示了Google在提升R类构建效率方面的改进,包括减少javac编译步骤和优化资源合并。同时,文章指出高版本AGP在ASM生成字节码时可能出现的常量池溢出问题,为理解和解决相关问题提供了深入见解。
摘要由CSDN通过智能技术生成

public static int test_layout; //只有定义,没再随机赋值了,依旧保持非 final

private id() {

}

}

public static final class string {

public static int lib_test_string;

private string() {

}

}

}

app 主 module 的产物分析

接着我们再来看看 app 主模块下的情况,编译后主 module 的产物结构图解:

在这里插入图片描述

上图中先看看build/intermediates/compile_and_runtime_not_namespaced_r_class_jar/debug/cn/yan/libr/R.class下的 libR 的 final R class,如下:

//已知结论

//相对 lib module 下的中间产物来说,这里 lib 的 R 已经变为 final 了,且给了明确的赋值

package cn.yan.libr;

public final class R {

private R() {

}

public static final class id {

public static final int test_layout = 2130837504; //已赋值具体值

private id() {

}

}

public static final class string {

public static final int lib_test_string = 2131099649;

private string() {

}

}

}

然后再来看看build/intermediates/compile_and_runtime_not_namespaced_r_class_jar/debug/cn/yan/testr/R.class下 app 的 final R class,如下:

//已知结论

//这里主 module 的 R 也是 final 且有明确值,此外已经合并了子 module 下的 R 属性到这个类

package cn.yan.testr;

public final class R {

private R() {

}

//来自子模块cn.yan.libr.R$id.class下的属性

public static final class id {

public static final int test_layout = 2130837504;

private id() {

}

}

//主module和子module的各自属性合并

public static final class string {

public static final int app_name = 2131099648;

public static final int lib_test_string = 2131099649;

private string() {

}

}

}

AGP3.5.0 版本的 R


gradle 构建脚本版本如下:

//【工匠若水 加微信 yanbo373131686 联系我,关注微信公众号:码农每日一题 未经允许严禁转载 https://blog.csdn.net/yanbober】

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

dependencies {

classpath “com.android.tools.build:gradle:3.5.0”

}

}

libR 子 module 的产物分析

如下是编译后子 module 的产物结构图解:

在这里插入图片描述

可以看到,上面 libR 模块中的R.jar里面的R.class内容如下:

package cn.yan.libr;

public final class R {

private R() {

}

public static final class id {

public static int test_layout;

private id() {

}

}

public static final class string {

public static int lib_test_string;

private string() {

}

}

}

app 主 module 的产物分析

接着我们再来看看 app 主模块下的情况,编译后主 module 的产物结构图解:

在这里插入图片描述

在这里插入图片描述

我们可以看到,他与上面 AGP4.1.0 版本最大的区别就是 app module 的构建产物不一样,AGP3.5.0 版本会先生成build/generated/not_namespaced_r_class_sources/debug/r/cn/yan/libr/R.javabuild/generated/not_namespaced_r_class_sources/debug/r/cn/yan/testr/R.java两个源码 java 文件,其中都是 final static 且具有明确值的;接着对他们通过 javac 编译生成了build/intermediates/javac/debug/classes/cn/yan/libr/R.classbuild/intermediates/javac/debug/classes/cn/yan/testr/R.class两个 class 文件,记住,这里 class 是 javac 生成的。

上图中先看看build/intermediates/javac/debug/classes/cn/yan/libr/R.class下的 libR 的 final R class,如下:

package cn.yan.libr;

public final class R {

private R() {

}

public static final class string {

public static final int lib_test_string = 2131099649;

private string() {

}

}

public static final class id {

public static final int test_layout = 2130837504;

private id() {

}

}

}

然后再来看看build/intermediates/javac/debug/classes/cn/yan/testr/R.class下 app 的 final R class,如下:

package cn.yan.testr;

public final class R {

public R() {

}

//主module和子module合并属性

public static final class string {

public static final int app_name = 2131099648;

public static final int lib_test_string = 2131099649;

public string() {

}

}

public static final class id {

public static final int test_layout = 2130837504;

public id() {

}

}

}

AGP3.1.2 版本的 R


gradle 构建脚本版本如下:

//【工匠若水 加微信 yanbo373131686 联系我,关注微信公众号:码农每日一题 未经允许严禁转载 https://blog.csdn.net/yanbober】

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

dependencies {

classpath “com.android.tools.build:gradle:3.1.2”

}

}

libR 子 module 的产物分析

如下是编译后子 module 的产物结构图解:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值