Skip to content

Commit ae08c0c

Browse files
committed
doc: add Android setup guide
1 parent 05dd8a0 commit ae08c0c

1 file changed

Lines changed: 81 additions & 9 deletions

File tree

README.md

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,87 @@ InputBox::new()
107107

108108
## Backends
109109

110-
| Backend | Platform | How it works | Extra setup |
111-
| ----------- | ----------- | ---------------------------------------------- | ----------------------------- |
112-
| `PSScript` | Windows | PowerShell + WinForms, no extra install needed | None |
113-
| `JXAScript` | macOS | `osascript` JXA, built into the OS | None |
114-
| `Android` | Android | AAR + JNI to show an Android AlertDialog | Include AAR in APK |
115-
| `IOS` | iOS | UIKit alert | None |
116-
| `OHOS` | OpenHarmony | NAPI + ArkTS dialog | See [OHOS Setup](#ohos-setup) |
117-
| `Yad` | Linux | [`yad`](https://github.com/v1cont/yad) | Install `yad` |
118-
| `Zenity` | Linux | `zenity` — fallback on GNOME systems | Install `zenity` |
110+
| Backend | Platform | How it works | Extra setup |
111+
| ----------- | ----------- | ---------------------------------------------- | ----------------------------------- |
112+
| `PSScript` | Windows | PowerShell + WinForms, no extra install needed | None |
113+
| `JXAScript` | macOS | `osascript` JXA, built into the OS | None |
114+
| `Android` | Android | AAR + JNI to show an Android AlertDialog | See [Android Setup](#android-setup) |
115+
| `IOS` | iOS | UIKit alert | None |
116+
| `OHOS` | OpenHarmony | NAPI + ArkTS dialog | See [OHOS Setup](#ohos-setup) |
117+
| `Yad` | Linux | [`yad`](https://github.com/v1cont/yad) | Install `yad` |
118+
| `Zenity` | Linux | `zenity` — fallback on GNOME systems | Install `zenity` |
119+
120+
### Android Setup
121+
122+
You need to include AAR in your Android project to use the Android backend. The AAR is bundled with the crate, but you need to configure your Gradle build to find it.
123+
124+
#### Gradle Setup
125+
126+
Inside of your project's `settings.gradle` file, add the following code and Maven repository definition.
127+
128+
`$PATH_TO_DEPENDENT_CRATE` is the relative path to the Cargo manifest (`Cargo.toml`) of any crate in your workspace that depends on `inputbox` from the location of your `settings.gradle` file:
129+
130+
```groovy
131+
import groovy.json.JsonSlurper
132+
133+
dependencyResolutionManagement {
134+
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
135+
repositories {
136+
// ...Other repositories...
137+
maven {
138+
url = findCrateBundledProject("inputbox-android")
139+
metadataSources.artifact()
140+
}
141+
}
142+
}
143+
144+
String findCrateBundledProject(packageName) {
145+
def dependencyText = providers.exec {
146+
commandLine("cargo", "metadata", "--format-version", "1", "--filter-platform", "aarch64-linux-android", "--manifest-path", "$PATH_TO_DEPENDENT_CRATE/Cargo.toml")
147+
}.standardOutput.asText.get()
148+
149+
def dependencyJson = new JsonSlurper().parseText(dependencyText)
150+
def manifestPath = file(dependencyJson.packages.find { it.name == packageName }.manifest_path)
151+
return new File(manifestPath.parentFile, "maven").path
152+
}
153+
```
154+
155+
Then, wherever you declare your dependencies, add the following:
156+
157+
```groovy
158+
implementation "moe.mivik:inputbox:latest.release"
159+
```
160+
161+
#### Crate initialization
162+
163+
The `jni` crate must be initialized before the crate can interact with the JVM. Add the following code to your native library's initialization function:
164+
165+
```rust,ignore
166+
// `jni` crate v0.22 or higher
167+
#[export_name = "Java_com_orgname_android_rust_init"]
168+
extern "system" fn java_init(
169+
env: EnvUnowned,
170+
_class: JClass,
171+
) {
172+
let result = env.with_env(|env| -> jni::errors::Result<()> {
173+
inputbox::backend::Android::initialize(env)?;
174+
// ...
175+
});
176+
// ...
177+
}
178+
179+
// Earlier versions of `jni` or other JNI bindings
180+
#[export_name = "Java_com_orgname_android_rust_init"]
181+
extern "system" fn java_init(
182+
env: JNIEnv,
183+
_class: JClass,
184+
) {
185+
unsafe {
186+
inputbox::backend::Android::initialize_raw(env.as_raw() as *mut _)?;
187+
}
188+
// ...
189+
}
190+
```
119191

120192
### OHOS Setup
121193

0 commit comments

Comments
 (0)