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

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

问题描述

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

解决方案

我已经在很长一段时间里,它一直受到这个问题的困扰,现在我注意到我的小型固态硬盘有千兆字节,我决定找出一种方法来禁用它。对我来说,在占用太多空间之前,最烦人的事情是 gradlew clean 留下一个 build 文件夹。



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


$ b

TL; DR



对于全局项目在 rootProject build.gradle 中使用它:



< pre class =lang-groovy prettyprint-override> com.android.build.gradle.internal.profile.ProfilerInitializer.recordingBuildListener =
new com.android.build.gradle.internal.profile .RecordingBuildListener(
com.android.builder.profile.ProcessProfileWriter.get());
//然后`gradlew --stop&& gradlew clean`来验证没有构建文件夹被留下



调查



感谢通过 https://stackoverflow.com/a/43910084/253468 =https://stackoverflow.com/users/190834/jeff-richards> @JeffRichards 提及 ProcessProfileWriterFactory.java ,我在那里放置了一个断点并通过运行 gradlew -Dorg.gradle.debug = true --info 来检查是谁调用它(不要与 - debug

我跟踪了一下,发现 ProcessProfileWriter.finishAndMaybeWrite 创建了)并附加了一个远程调试器。该文件夹和写入。追溯方法调用我发现 ProfilerInitializer.recordingBuildListener 控制它是否被调用...并且它是由 BasePlugin 直接初始化的。 ( apply plugin:'com.android。*')。

所以为了防止任何事情发生,选择尝试禁用警卫,通过预先初始化该静态字段。谢天谢地,Groovy(也就是Gradle)并没有提供*关于JVM可见性修饰符的知识,所以如果没有反思,这里有一条神奇的线:

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

我知道,它有点冗长,但是起作用,如果你导入东西,它看起来会更好:

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



应用魔术



-project build(一个 build.gradle )您必须在之前应用此

  apply plugin:'com.android.application'

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

buildscript {
// ...
依赖关系{
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
//这里的魔术线

确保它在任何 apply插件之前: s,而不在 buildscript block。



全局应用魔术



显然,如果它在一个项目中,我们会在所有情况下遵循 Gradle手册,在〜/ .gradle / init.gradle %USERPROFILE%\.gradle\init.gradle (请注意,此文件夹可以通过 GRADLE_USER_HOME )以下内容:

  // NB:对这个脚本的任何修改都需要一个新的守护进程(`gradlew --stop`或`gradlew --no-daemon< tasks>``)
rootProject {rootProject - > //参见https://stackoverflow.com/a/48087543/253468
//监听项目插件上的生命周期事件
rootProject.plugins.whenPluginAdded {plugin - >
//检查是否有任何Android插件正在被应用(不一定只是'com.android.application')
//这个插件实际上就是为了这个目的:获得通知
if( plugin.class.name =='com.android.build.gradle.api.AndroidBasePlugin'){
logger.info'关闭`build / android-profile / profile - *。(rawproto | json)`generation 。'
//在buildscript的上下文中执行hack,而不是在这个initscript
中新增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());

}
}
}


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?

解决方案

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.

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

TL;DR

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

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.

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.*').

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());

Applying the magic

In a single-project build (one build.gradle) you must apply this before

apply plugin: 'com.android.application'

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

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

Applying the magic globally

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天全站免登陆