Skip to content

Commit

Permalink
Add configuration for the Android cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Jan 8, 2024
1 parent 8cfba9e commit b25774e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/manual/src/kotlin/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The generated Kotlin modules can be configured using a `uniffi.toml` configurati
| `generate_immutable_records` | `false` | Whether to generate records with immutable fields (`val` instead of `var`). |
| `custom_types` | | A map which controls how custom types are exposed to Kotlin. See the [custom types section of the manual](../udl/custom_types.md#custom-types-in-the-bindings-code)|
| `external_packages` | | A map of packages to be used for the specified external crates. The key is the Rust crate name, the value is the Kotlin package which will be used referring to types in that crate. See the [external types section of the manual](../udl/ext_types_external.md#kotlin)
| `android` | `false` | Used to toggle on Android specific optimizations
| `android_cleaner` | `android` | Use the [`android.system.SystemCleaner`](https://developer.android.com/reference/android/system/SystemCleaner) instead of [`java.lang.ref.Cleaner`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/Cleaner.html)


## Example
Expand Down
10 changes: 10 additions & 0 deletions uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ pub struct Config {
custom_types: HashMap<String, CustomTypeConfig>,
#[serde(default)]
external_packages: HashMap<String, String>,
#[serde(default)]
android: bool,
#[serde(default)]
android_cleaner: Option<bool>,
}

impl Config {
pub(crate) fn android_cleaner(&self) -> bool {
self.android_cleaner.unwrap_or(self.android)
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ internal interface UniffiLib : Library {
{% if ci.contains_object_types() %}
// The cleaner for the whole library.
internal val CLEANER: Cleaner by lazy {
{%- if config.android_cleaner() %}
SystemCleaner.cleaner()
{%- else %}
Cleaner.create()
{%- endif %}
}
{%- endif %}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- self.add_import("java.lang.ref.Cleaner") }}
{%- if config.android_cleaner() %}{{- self.add_import("android.system.SystemCleaner") }}{%- endif %}
{{- self.add_import("java.util.concurrent.atomic.AtomicLong") }}
{{- self.add_import("java.util.concurrent.atomic.AtomicBoolean") }}

Expand Down

0 comments on commit b25774e

Please sign in to comment.