如何防止 rawproto 文件生成或自动删除它们? [英] How to prevent rawproto file generation or delete them automatically?

查看:23
本文介绍了如何防止 rawproto 文件生成或自动删除它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android gradle 插件会在 build/android-profile 目录中生成大量的 .rawproto 文件.它们是做什么用的?有没有办法禁用这种疯狂或自动删除它们?

Android gradle plugin generates tons of .rawproto files in build/android-profile directory. What are they used for? Is there a way to disable this madness or automatically delete them?

推荐答案

我已经被它困扰了很长时间,现在我注意到有数 GB 的数据占用了我的小 SSD,我决定想办法禁用它.对我来说,在占用太多空间之前最烦人的事情是 gradlew clean 留下一个 build 文件夹.

I've been bugged by it for a long time, and now that I noticed there's gigabytes of this hogging my smallish SSD, I've decided to figure out a way to disable it. For me the most annoying thing before occupying too much space was gradlew clean leaving a build folder behind.

仅使用 com.android.tools.build:gradle:3.0.1 进行测试,所以 YMMV.

Only tested with com.android.tools.build:gradle:3.0.1, so YMMV.

对于 global 应用程序阅读最后一节,每个项目在 rootProjectbuild.gradle 中使用它:

For global application read last section, per-project use this in rootProject's build.gradle:

com.android.build.gradle.internal.profile.ProfilerInitializer.recordingBuildListener =
        new com.android.build.gradle.internal.profile.RecordingBuildListener(
            com.android.builder.profile.ProcessProfileWriter.get());
// and then `gradlew --stop && gradlew clean` to verify no build folder is left behind

调查

感谢 https://stackoverflow.com/a/43910084/253468@JeffRichards 提到 ProcessProfileWriterFactory.java,我在那里放置了一个断点并通过运行 gradlew -Dorg.gradle.debug=true --info(不要与 --debug 混淆)并附加远程调试器.

Investigation

Thanks to https://stackoverflow.com/a/43910084/253468 linked by @JeffRichards mentioning ProcessProfileWriterFactory.java, I've put a breakpoint there and checked who's calling it by running gradlew -Dorg.gradle.debug=true --info (not to be confused with --debug) and attaching a remote debugger.

我按照跟踪发现 ProcessProfileWriter.finishAndMaybeWrite 创建文件夹并写入.回溯方法调用,我发现 ProfilerInitializer.recordingBuildListener 控制它是否被调用......并且由 BasePlugin 直接初始化(apply plugin: 'com.android.*').

I followed the trail and found that ProcessProfileWriter.finishAndMaybeWrite creates the folder and writes. Backtracing on method calls I found that ProfilerInitializer.recordingBuildListener controls whether it's called ... and that is initialized directly by BasePlugin (apply plugin: 'com.android.*').

因此,为了防止发生任何事情,我选择尝试通过预初始化该静态字段来禁用防护.谢天谢地,Groovy(以及 Gradle)没有给出关于 JVM 可见性修饰符的 *,所以没有反射这里是神奇的线:

So in order to prevent anything from happening I opted to try to disable the guard, by pre-initialized that static field. Thankfully Groovy (and hence Gradle) doesn't give a * about JVM visibility modifiers, so without reflection here's the magic line:

com.android.build.gradle.internal.profile.ProfilerInitializer.recordingBuildListener =
    new com.android.build.gradle.internal.profile.RecordingBuildListener(
        com.android.builder.profile.ProcessProfileWriter.get());

我知道,它有点冗长,但它确实有效,而且如果你导入它看起来会更好:

I know, it's a bit verbose, but it works, and if you import stuff it looks better:

ProfilerInitializer.recordingBuildListener = new RecordingBuildListener(ProcessProfileWriter.get());

应用魔法

在单项目构建(一个build.gradle)中,您必须应用此之前

apply plugin: 'com.android.application'

在多项目构建中(大多数模板项目:app 文件夹、settings.gradle 和许多 build.gradles)我建议你在 buildscript 块周围应用它:

In multi-project builds (most template projects: app folder, settings.gradle, and many build.gradles) I suggest you apply it around the buildscript block:

buildscript {
    // ...
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}
// magic line here

确保它在任何 apply plugin: 之前,而不是在 buildscript 块内.

Make sure it's before any apply plugin:s, and not inside a buildscript block.

显然,如果它在一个项目中困扰我们,那么在所有情况下都会困扰我们,因此请遵循 Gradle 的手册,在 ~/.gradle/init.gradle%USERPROFILE%\.gradle\init.gradle 中创建一个文件(注意您可以使用GRADLE_USER_HOME重新定位此文件夹) 的内容如下:

Obviously if it bothers us in one project, it will in all cases, so following Gradle's manual, create a file in ~/.gradle/init.gradle or %USERPROFILE%\.gradle\init.gradle (mind you this folder can be relocated with GRADLE_USER_HOME) with the following contents:

// NB: any changes to this script require a new daemon (`gradlew --stop` or `gradlew --no-daemon <tasks>`)
rootProject { rootProject -> // see https://stackoverflow.com/a/48087543/253468
    // listen for lifecycle events on the project's plugins
    rootProject.plugins.whenPluginAdded { plugin ->
        // check if any Android plugin is being applied (not necessarily just 'com.android.application')
        // this plugin is actually exactly for this purpose: to get notified
        if (plugin.class.name == 'com.android.build.gradle.api.AndroidBasePlugin') {
            logger.info 'Turning off `build/android-profile/profile-*.(rawproto|json)` generation.'
            // execute the hack in the context of the buildscript, not in this initscript
            new GroovyShell(plugin.class.classLoader).evaluate("""
                com.android.build.gradle.internal.profile.ProfilerInitializer.recordingBuildListener =
                    new com.android.build.gradle.internal.profile.RecordingBuildListener(
                        com.android.builder.profile.ProcessProfileWriter.get());
            """)
        }
    }
}

这篇关于如何防止 rawproto 文件生成或自动删除它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆