Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude temporary Realm files from Gradle build

Realm provides a database viewer called "Realm Studio" that allows users to browse the contents of their database. When the application is used to view a database, it creates several "temporary" files in the directory of the database, namely:

  • A .realm.lock file.
  • A .realm.note file.
  • A .realm.management directory containing:
    • A access_control.control.mx file.
    • A access_control.new_commit.cv file.
    • A access_control.pick_writer.cv file.
    • A access_control.write.mx file.

In the context of Android, a preexisting Realm database is sometimes shipped with an application by placing the database in the /assets directory. Thus, when the Realm Studio is used to view this database, the aforementioned files are generated in /assets. For unknown reasons, this causes Gradle to hang indefinitely after the :app:generateDebugAssets task, apparently at the :app:mergeDebugAssets task.

As such, i'd like to find a way to exclude these files from the build. I've tried several methods, such as:

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

and other methods, like:

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'

to no avail.

How can one instruct Gradle to exclude these files when running a build?

EDIT: Small snippet of repeated output from ./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.
...
like image 905
Orbit Avatar asked Nov 27 '18 00:11

Orbit


3 Answers

I think the problem is that you did not delete the .realm.management directory.

android {

    ...

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

assets directory contents:

enter image description here

assets directory contents in apk:

enter image description here

like image 173
aminography Avatar answered Oct 23 '22 15:10

aminography


guess you might be using an elder version of Realm Studio, because according to issue #842 and pull request #847, those directories should meanwhile be properly cleaned up - and also report, in case not - instead of stalling. the current version 3.12 should have that fixed (basically any release after the 18th of June). if that should not help, the best you can do is to report it below issue #842; or file a new one issue, which is referencing issue #842. also check the file-system permissions, that the user which runs gradle is permitted to delete there; manually moving files out of the way "might" help, so the current user may re-create them and then subsequently is able to delete his own files.

both methods using gradle are workarounds - because it should not happen in the first place.

in order to obtain further information "why it stalls", run this command in the terminal:

./gradlew -d app:generateDebugAssets

respectively on Windows (the question does neither indicate the OS nor the Realm Studio version):

gradlew.bat -d app:generateDebugAssets

edit: one could even delete these files before each build:

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
    }
}

which changes the task graph to:

:app:checkDebugClasspath
:app:cleanupRealm
:app:preBuild
:app:preDebugBuild
like image 28
Martin Zeitler Avatar answered Oct 23 '22 15:10

Martin Zeitler


Try that :

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

From aapt binary from build-tools folder :

./aapt
[...]
--ignore-assets
   Assets to be ignored. Default pattern is:
   !.svn:!.git:!.ds_store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~
like image 4
ToYonos Avatar answered Oct 23 '22 15:10

ToYonos