tauri-apps/tauri · error

Failed to write kotlin file

Error message

Failed to write kotlin file

What it means

During Android builds, tauri's build script reads Kotlin templates, substitutes {{package}} and {{library}}, and writes each into the Android project's Kotlin output directory (from the TAURI_ANDROID_* env vars). write_if_changed fails when that output directory does not exist or is not writable.

Source

Thrown at crates/tauri/build.rs:318

        });

      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 52e4b6e71d)

Solutions

  1. Regenerate the Android project: tauri android init.
  2. Verify the Kotlin output directory exists and is writable (create_dir_all on it succeeds).
  3. Re-point TAURI_ANDROID_* env vars at the current gen/android folder.
Defensive patterns

Strategy: validation

Validate before calling

// before the android build, make sure the kotlin out dir exists
if let Some(kotlin_out) = std::env::var_os("TAURI_ANDROID_KOTLIN_OUT_DIR") {
    std::fs::create_dir_all(std::path::PathBuf::from(kotlin_out))
        .expect("kotlin out dir must exist and be writable");
}

Prevention

When it happens

Trigger: TAURI_ANDROID_PROJECT_PATH / kotlin out dir env vars pointing at an Android project whose generated Kotlin source directory is missing (partially generated, manually pruned, stale env var).

Common situations: Running the Android build against a hand-modified or partially regenerated gen/android project; env vars left pointing at a moved or deleted folder.

Related errors


AI-assisted analysis of tauri-apps/tauri@52e4b6e71d (2026-08-20). Data as JSON: /api/errors/c0620fec684188dd. Report an issue: GitHub.