如何防止rawproto文件生成或自动删除它们?

问题描述 投票:11回答:1

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

android-studio gradle android-gradle
1个回答
4
投票

我已经被它搞了很长时间了,现在我注意到有几千兆的这个占用我的小SSD,我决定找出一种方法来禁用它。对我来说,占据太多空间之前最烦人的事情是gradlew clean留下了一个build文件夹。

只测试了com.android.tools.build:gradle:3.0.1,所以YMMV。

TL;DR

对于全局应用程序阅读上一节,每个项目在rootProjectbuild.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

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

我沿着小道发现,ProcessProfileWriter.finishAndMaybeWrite创建了文件夹并写入。对方法调用进行回溯我发现ProfilerInitializer.recordingBuildListener控制它是否被调用......并且由BasePluginapply 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());

Applying the magic

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

apply plugin: 'com.android.application'

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

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

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

Applying the magic globally

显然,如果它在一个项目中困扰我们,它将在所有情况下,所以在Gradle's manual之后,在~/.gradle/init.gradle%USERPROFILE%\.gradle\init.gradle(请注意this folder can be relocated with GRADLE_USER_HOME)中创建一个文件,其中包含以下内容:

// 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());
            """)
        }
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.