如何防止rawproto文件生成或自动删除它们?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何防止rawproto文件生成或自动删除它们?相关的知识,希望对你有一定的参考价值。
android gradle插件在.rawproto
目录中生成大量的build/android-profile
文件。它们用于什么?有没有办法禁用这种疯狂或自动删除它们?
我已经被它搞了很长时间了,现在我注意到有几千兆的这个占用我的小SSD,我决定找出一种方法来禁用它。对我来说,占据太多空间之前最烦人的事情是gradlew clean
留下了一个build
文件夹。
只测试了com.android.tools.build:gradle:3.0.1
,所以YMMV。
TL;DR
对于全局应用程序阅读上一节,每个项目在rootProject
的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
感谢由https://stackoverflow.com/a/43910084/253468提到的@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());
Applying the magic
在单项目构建(一个build.gradle
)中,您必须先应用此项目
apply plugin: 'com.android.application'
在多项目构建中(大多数模板项目:app
文件夹,settings.gradle
和许多build.gradle
s)我建议你在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%.gradleinit.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());
""")
}
}
}
以上是关于如何防止rawproto文件生成或自动删除它们?的主要内容,如果未能解决你的问题,请参考以下文章
如何复制或删除bash中的特定行并将它们创建到新文件中[重复]