Skip to content

Commit b2fd2fb

Browse files
committed
start-bindings fixes
I noticed a couple issues when I tried to create bindings for `fairy-bridge`.
1 parent a0f46b4 commit b2fd2fb

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

docs/howtos/adding-a-new-component.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ dependencies {
7373
}
7474
```
7575

76+
### Async dependencies
77+
78+
If your components exports async functions, add the following to your `android/build.gradle` file:
79+
80+
```
81+
dependencies {
82+
implementation libs.kotlin.coroutines
83+
}
84+
```
85+
7686
### Hand-written code
7787

7888
You can include hand-written Kotlin code alongside the automatically

tools/start-bindings/src/android.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ pub fn generate_android(crate_name: String, description: String) -> Result<()> {
2424
println!();
2525
write_file(
2626
BuildGradle {
27-
crate_name: crate_name.clone(),
27+
// configureUniFFIBindgen replaces underscores rather than hyphens
28+
crate_name: crate_name.replace("-", "_"),
29+
kotlin_module_name: crate_name.replace("-", ""),
2830
}
2931
.render()?,
3032
&android_root.join("build.gradle"),
@@ -42,7 +44,7 @@ pub fn generate_android(crate_name: String, description: String) -> Result<()> {
4244
"kotlin",
4345
[(
4446
"package_name",
45-
format!("mozilla.appservices.{crate_name}").into(),
47+
format!("mozilla.appservices.{}", crate_name.replace("-", "_")).into(),
4648
)],
4749
)?;
4850
add_cargo_toml_dependency(
@@ -115,12 +117,13 @@ fn write_file(contents: impl AsRef<str>, path: &Utf8Path) -> Result<()> {
115117

116118
fn update_megazord_lib_rs(lib_path: &Utf8Path, crate_name: &str) -> Result<()> {
117119
let content = read_to_string(lib_path)?;
120+
let crate_name_rust = crate_name.replace("-", "_");
118121
let mut lines: Vec<String> = content.split("\n").map(str::to_string).collect();
119122
let first_use_line = lines
120123
.iter()
121124
.position(|line| line.starts_with("pub use"))
122125
.ok_or_else(|| anyhow!("Couldn't find a `pub use` line in {lib_path}"))?;
123-
lines.insert(first_use_line, format!("pub use {crate_name};"));
126+
lines.insert(first_use_line, format!("pub use {crate_name_rust};"));
124127
write_file(lines.join("\n"), lib_path)?;
125128
Command::new("cargo")
126129
.args(["fmt", "-pmegazord"])
@@ -179,6 +182,7 @@ fn buildconfig_needs_update(path: &Utf8Path, crate_name: &str) -> Result<bool> {
179182
#[template(path = "build.gradle", escape = "none")]
180183
struct BuildGradle {
181184
crate_name: String,
185+
kotlin_module_name: String,
182186
}
183187

184188
const ANDROID_MANIFEST: &str =

tools/start-bindings/templates/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply from: "$appServicesRootDir/build-scripts/component-common.gradle"
22
apply from: "$appServicesRootDir/publish.gradle"
33

44
android {
5-
namespace 'org.mozilla.appservices.{{ crate_name }}'
5+
namespace 'org.mozilla.appservices.{{ kotlin_module_name }}'
66
}
77

88
ext.configureUniFFIBindgen("{{ crate_name }}")

0 commit comments

Comments
 (0)