gradle7 编译时排除R.java

sourceSets 已经不适合gradle7以上的版本了

以下方法可以,但会导致资源编译失败, 适合空activity的APP
build.gradle中添加

import java.nio.file.Files

project.afterEvaluate {
    android.applicationVariants.forEach { variant ->
        def variantCapped = variant.name.capitalize()
        def variantLowered = variant.name.toLowerCase()
        println("afterEvaluate applicationVariants: ${variantCapped} - ${variantLowered} - ${variant.outputs[0].outputFile}")

        def replaceRJar = task("removeRjava${variantCapped}") {
            doFirst {
                println("write blank jar to R.jar")
                def r_file = new File("${project.buildDir}/intermediates/compile_and_runtime_not_namespaced_r_class_jar/${variantLowered}/R.jar")
                if (!r_file.exists()) {
                    r_file.parentFile.mkdirs()
                }
                Files.write(r_file.toPath(), new byte[]{
                        (byte) 0x50, (byte) 0x4B, (byte) 0x05, (byte) 0x06, (byte) 0x00, (byte) 0x00,
                        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
                })
            }
        }

        //在processResourcesTask和compileJavaWithJavac之间插入清除r.jar任务
        def processResourcesTask = tasks.findByName("process${variantCapped}Resources")
        def compileJavaWithJavac = tasks.findByName("compile${variantCapped}JavaWithJavac")
        replaceRJar.mustRunAfter(processResourcesTask)
        compileJavaWithJavac.dependsOn(replaceRJar)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值