tauri-apps/tauri · error

Failed to write kotlin file

Error message

Failed to write kotlin file

What it means

Build-script panic raised when write_if_changed fails to persist a generated Kotlin file into the codegen output directory. The Tauri build script generates Android Kotlin sources from templates and writes them to OUT_DIR (only overwriting when content changed to avoid rebuild churn). A failure here means the generated file could not be written.

Solutions

  1. Free disk space and confirm the filesystem is writable
  2. Fix ownership/permissions of the target directory (e.g. sudo chown -R $(whoami) target)
  3. Delete the target directory and rebuild to regenerate a clean OUT_DIR
  4. Avoid running multiple cargo builds concurrently against the same target dir

Example fix

// before
sudo cargo tauri android build   # target/ now owned by root
// after
sudo chown -R $(whoami) target
cargo tauri android build
Defensive patterns

Strategy: validation

Validate before calling

let dir = std::path::Path::new(&out_dir);
assert!(dir.is_dir(), "OUT_DIR missing");
let probe = dir.join(".write-test");
std::fs::write(&probe, b"").expect("OUT_DIR not writable");
let _ = std::fs::remove_file(&probe);

Prevention

When it happens

Trigger: The kotlin output directory (out_path) is not writable: OUT_DIR missing or deleted mid-build, disk full, read-only filesystem, or permission denied on the destination path.

Common situations: Out-of-disk errors on CI runners, target/ directory owned by root after a sudo build, a read-only mounted workspace, or concurrent builds racing on the same OUT_DIR.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of tauri-apps/tauri@460ec35447 (2026-09-18). Data as JSON: /api/errors/c0620fec684188dd. Report an issue: GitHub.

Appendix: source

Thrown at crates/tauri/build.rs:312

        });

      let kotlin_files_path =
        PathBuf::from(env_var("CARGO_MANIFEST_DIR")).join("mobile/android-codegen");
      println!("cargo:rerun-if-changed={}", kotlin_files_path.display());
      let kotlin_files =
        fs::read_dir(kotlin_files_path).expect("failed to read Android codegen directory");

      for file in kotlin_files {
        let file = file.unwrap();

        let content = fs::read_to_string(file.path())
          .expect("failed to read kotlin file as string")
          .replace("{{package}}", &package)
          .replace("{{library}}", &library);

        let out_path = kotlin_out_dir.join(file.file_name());
        // Overwrite only if changed to not trigger rebuilds
        write_if_changed(&out_path, &content).expect("Failed to write kotlin file");

        println!("cargo:rerun-if-changed={}", out_path.display());
      }
    }

    if let Some(project_dir) = env::var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
      let package_unescaped = env::var("TAURI_ANDROID_PACKAGE_UNESCAPED")
        .unwrap_or_else(|_| env_var("WRY_ANDROID_PACKAGE").replace('`', ""));
      let tauri_proguard =
        include_str!("./mobile/proguard-tauri.pro").replace("$PACKAGE", &package_unescaped);
      std::fs::write(
        project_dir.join("app").join("proguard-tauri.pro"),
        tauri_proguard,
      )
      .expect("failed to write proguard-tauri.pro");
    }

    let lib_path =

View on GitHub (pinned to 460ec35447)