gradle7 编译时排除R.java

gradle7 编译时排除R.java,第1张

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

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

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)
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/905903.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存