从Gradle构建中排除临时Realm文件

时间:2018-11-27 00:02:28

标签: android gradle android-gradle realm build.gradle

Realm提供了一个名为“ Realm Studio”的数据库查看器,允许用户浏览其数据库的内容。当使用该应用程序查看数据库时,它将在数据库目录中创建几个“临时”文件,即:

  • 一个.realm.lock文件。
  • 一个.realm.note文件。
  • 一个.realm.management目录,其中包含:
    • 一个access_control.control.mx文件。
    • 一个access_control.new_commit.cv文件。
    • 一个access_control.pick_writer.cv文件。
    • 一个access_control.write.mx文件。

在Android上下文中,有时会将应用程序附带一个预先存在的Realm数据库,方法是将该数据库放在/assets目录中。因此,当使用Realm Studio查看该数据库时,上述文件将在/assets中生成。出于未知原因,这导致Gradle在:app:generateDebugAssets任务之后无限期挂起,显然挂在:app:mergeDebugAssets任务上。

因此,我想找到一种从构建中排除这些文件的方法。我尝试了几种方法,例如:

applicationVariants.all { variant ->
    if (variant.buildType.name == 'debug') {
        variant.mergeAssets.doLast {
            delete(fileTree(dir: variant.mergeAssets.outputDir, includes: ['**/*.cv', '**/*.mx', '**/*.lock', '**/*.note']))
        }
    }
}

和其他方法,例如:

sourceSets.main.assets.exclude 'appData.realm.management'
sourceSets.main.assets.exclude 'appData.realm.management/access_control.control.mx'
sourceSets.main.assets.exclude 'appData.realm.management/access_control.new_commit.cv'
sourceSets.main.assets.exclude 'appData.realm.management/access_control.pick_writer.cv'
sourceSets.main.assets.exclude 'appData.realm.management/access_control.write.mx'
sourceSets.main.assets.exclude 'appData.realm.lock'
sourceSets.main.assets.exclude 'appData.realm.note'

无济于事。

运行构建时,如何指示Gradle排除这些文件?

编辑:来自./gradlew -d app:mergeDebugAssets的重复输出的小片段:

[org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
[org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event {Total: 8589934592, Free: 2572066816}
[org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: {Total: 8589934592, Free: 2572066816}
[org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event {Maximum: 1431830528, Committed: 308281344}
[org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event {Total: 8589934592, Free: 2567909376}
[org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: {Total: 8589934592, Free: 2567909376}
[org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event {Maximum: 1431830528, Committed: 308281344}
[org.gradle.launcher.daemon.server.Daemon] DaemonExpirationPeriodicCheck running
[org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
[org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event {Total: 8589934592, Free: 2564087808}
[org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: {Total: 8589934592, Free: 2564087808}
[org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event {Maximum: 1431830528, Committed: 308281344}
[org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event {Total: 8589934592, Free: 2560860160}
[org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: {Total: 8589934592, Free: 2560860160}
[org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event {Maximum: 1431830528, Committed: 308281344}
[org.gradle.launcher.daemon.server.Daemon] DaemonExpirationPeriodicCheck running
[org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
[org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
...

3 个答案:

答案 0 :(得分:5)

我认为问题在于您没有删除.realm.management目录。

android {

    ...

    applicationVariants.all { variant ->
        if (variant.buildType.name == 'debug') {
            variant.mergeAssets.doLast {
                delete(fileTree(dir: variant.mergeAssets.outputDir, includes: ['**/.realm.management', '**/*.lock', '**/*.note']))
            }
        }
    }
}

定义目录内容:

enter image description here

在apk中分配目录内容:

enter image description here

答案 1 :(得分:5)

猜测您可能使用的是Realm Studio的旧版本, 因为根据issue #842pull request #847,应同时清理这些目录-并报告(以防万一)-而不是停顿。当前版本3.12应该已修复(基本上是6月18日之后的任何发行版)。如果这样做没有帮助,则您最好的办法是在问题#842下报告该问题;或提出一个新的问题,该问题参考问题#842。还检查文件系统权限,以确保运行gradle的用户可以在那里删除;手动移开文件可能会失去帮助,因此当前用户可以重新创建它们,然后可以删除自己的文件。

这两种使用gradle的方法都是解决方法-因为它首先不应该发生。

为了获取“为什么停顿”的更多信息,请在终端中运行以下命令:

./gradlew -d app:generateDebugAssets

分别在Windows上(问题既不表示操作系统也不表示Realm Studio版本):

gradlew.bat -d app:generateDebugAssets

编辑:甚至可以在每次构建之前删除这些文件:

task cleanupRealm(type: Delete) {
    delete project.projectDir.path + "/src/main/assets/.realm.management"
    delete project.projectDir.path + "/src/main/assets/appData.realm.lock"
    delete project.projectDir.path + "/src/main/assets/appData.realm.note"
}

tasks.whenTaskAdded { task ->
    if (task.name == "preDebugBuild" || task.name == "preReleaseBuild") {
        task.dependsOn cleanupRealm
    }
}

将任务图更改为:

:app:checkDebugClasspath
:app:cleanupRealm
:app:preBuild
:app:preDebugBuild

答案 2 :(得分:4)

尝试:

android {
    aaptOptions {
        ignoreAssetsPattern "!*.cv:!*.mx:!*.lock:!*.note" 
    }
}

来自aapt文件夹中的build-tools二进制文件:

./aapt
[...]
--ignore-assets
   Assets to be ignored. Default pattern is:
   !.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~