栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

gradle7 编译时排除R.java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gradle7 编译时排除R.java

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)
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/874429.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号