Error running default wry template
Describe the bug When I generate a wry template using the latest cargo-mobile2 (or version 0.5.1 as suggested here : https://github.com/tauri-apps/cargo-mobile2/issues/210#issuecomment-1728503050), I get this error : e: /home/riccardo/dioxus-mobile-test/gen/android/app/src/main/kotlin/com/example/dioxus_mobile_test/MainActivity.kt: (3, 22): Unresolved reference: WryActivity
Steps To Reproduce
- Setup Android SDK, NDK...
- Add Rust targets
- Create a new Rust project and
cdinto it - Run
cargo install --git https://github.com/tauri-apps/cargo-mobile2(orcargo install --git https://github.com/tauri-apps/cargo-mobile2 --tag tauri-mobile-v0.5.1) - Run
cargo mobile init - Run
cargo android run
Expected behavior The code to compile and run.
~~Screenshots~~ ~~If applicable, add screenshots to help explain your problem.~~
Platform and Versions (please complete the following information):
Host OS: ArchLinux x86_64
Target OS: aarch64-linux-android
Rustc: rustc 1.74.1 (a28077b28 2023-12-04)
Ouput of cargo mobile doctor:
For version 0.5.1 :
[!] cargo-mobile v0.5.1
• Contains commits up to "Publish New Versions (#158)\n"
• Installed at "~/.cargo/.tauri-mobile"
✗ Failed to get OS info: Failed to find `VERSION` in
"/etc/os-release": "NAME=\"Arch Linux\"\nPRETTY_NAME=\"Arch
Linux\"\nID=arch\nBUILD_ID=rolling\nANSI_COLOR=\"38;2;23;147;209\"
\nHOME_URL=\"https://archlinux.org/\"\nDOCUMENTATION_URL=\"https:/
/wiki.archlinux.org/\"\nSUPPORT_URL=\"https://bbs.archlinux.org/\"
\nBUG_REPORT_URL=\"https://bugs.archlinux.org/\"\nPRIVACY_POLICY_U
RL=\"https://terms.archlinux.org/docs/privacy-policy/\"\nLOGO=arch
linux-logo\n"
• rustc v1.74.1 (a28077b28 2023-12-4)
[✔] Android developer tools
• SDK v26.1.1 installed at "~/Android/Sdk"
• NDK v26.1.10909125 installed at "~/Android/Sdk/ndk/26.1.10909125"
[✔] Connected devices
• Redmi Note 8
For version 0.9.1 :
[!] cargo-mobile v0.9.1
• Contains commits up to "Publish New Versions (#266)\n"
• Installed at "~/.cargo/.cargo-mobile2"
✗ Failed to get OS info: Failed to find `VERSION` in "/etc/os-release": "NAME=\"Arch Linux\"\nPRETTY_NAME=\"Arch
Linux\"\nID=arch\nBUILD_ID=rolling\nANSI_COLOR=\"38;2;23;147;209\"\nHOME_URL=\"https://archlinux.org/\"\nDOCUMENTATION_URL=\"https://
wiki.archlinux.org/\"\nSUPPORT_URL=\"https://bbs.archlinux.org/\"\nBUG_REPORT_URL=\"https://bugs.archlinux.org/
\"\nPRIVACY_POLICY_URL=\"https://terms.archlinux.org/docs/privacy-policy/\"\nLOGO=archlinux-logo\n"
• rustc v1.74.1 (a28077b28 2023-12-4)
[✔] Android developer tools
• SDK v26.1.1 installed at "~/Android/Sdk"
• NDK v26.1.10909125 installed at "~/Android/Sdk/ndk/26.1.10909125"
[✔] Connected devices
• Redmi Note 8
~~Additional context~~ ~~Add any other context about the problem here.~~
:thinking:
I ran "cargo android apk build" and then "cargo android run" and it worked :thinking:
I think I also reinstalled the cargo-mobile2 CLI just after cargo mobile init :thinking:
I got an error few seconds after launching but it works :thinking:
Starting: Intent { cmp=com.example.truc/.MainActivity }
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.os.Parcel.createExceptionOrNull(Parcel.java:2376)
at android.os.Parcel.createException(Parcel.java:2360)
at android.os.Parcel.readException(Parcel.java:2343)
at android.os.Parcel.readException(Parcel.java:2285)
at android.hardware.input.IInputManager$Stub$Proxy.injectInputEvent(IInputManager.java:1012)
at android.hardware.input.InputManager.injectInputEvent(InputManager.java:907)
at com.android.commands.input.Input.injectKeyEvent(Input.java:355)
at com.android.commands.input.Input.access$100(Input.java:41)
at com.android.commands.input.Input$InputKeyEvent.sendKeyEvent(Input.java:203)
at com.android.commands.input.Input$InputKeyEvent.run(Input.java:190)
at com.android.commands.input.Input.onRun(Input.java:107)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:60)
at com.android.commands.input.Input.main(Input.java:71)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:463)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.input.InputManagerService.injectInputEventInternal(InputManagerService.java:790)
at com.android.server.input.InputManagerService.injectInputEvent(InputManagerService.java:764)
at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:459)
at android.os.Binder.execTransactInternal(Binder.java:1154)
at android.os.Binder.execTransact(Binder.java:1123)
--------- beginning of main
--------- beginning of system
logcat: Unexpected EOF!
This means that either the device shut down, logd crashed, or this instance of logcat was unable to read log
messages as quickly as they were being produced.
If you have enabled significant logging, look into using the -G option to increase log buffer sizes.
error: IO error
command ["/home/riccardo/Android/Sdk/platform-tools/adb", "logcat",
"-v", "color", "-s", "truc:W"] exited with code 1
But when I create a new blank wry project, cargo mobile init and cargo android run I get the same error :thinking: :
e: /home/riccardo/dioxus-test/gen/android/app/src/main/kotlin/com/example/dioxus_test/MainActivity.kt: (3, 22): Unresolved reference: WryActivity
And running cargo android apk build doesn't work neither :thinking:
For a project that compiles, gen/android/app/src/main/kotlin/com/example/truc/ contains the following files :
Ipc.kt RustWebChromeClient.kt
Logger.kt RustWebViewClient.kt
MainActivity.kt RustWebView.kt
PermissionHelper.kt WryActivity.kt
proguard-wry.pro
But for the other project, gen/android/app/src/main/kotlin/org/slt/org only contains MainActivity.kt
It seems like cargo-mobile2 doesn't generate some files needed like WryActivity.kt :thinking:
I can reproduce the issue on macOS. I tested a few different versions of wry with the wry template. The default wry version 0.28 does work for me with the mobile template. Any version after 0.34.0 fails to link to WryActivity. I followed these instructions when updating wry. Here is the repo with the reproduction. To run the project I used these commands:
cargo android build
cargo android open
// Start an pixel 7 pro API 33 emulator
cargo android run
Thanks I'll try that asap !
Unfortunately I get the same error Here are the things I did :
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk"
instead of
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
otherwise it would cause a Kotlin error then
wget https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip -o cmdline-tools.zip
unzip cmdline-tools.zip
cd cmdline-tools
mkdir latest
mv bin latest/
mv lib latest/
mv NOTICE.txt latest/
mv source.properties latest/
cd ..
mkdir ~/.android
mv cmdline-tools ~/.android
then
export ANDROID_HOME="$HOME/.android"
~/.android/cmdline-tools/latest/bin/sdkmanager "platforms;android-33" "platform-tools" "ndk;25.0.8775105" "build-tools;33.0.0"
then
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk"
export ANDROID_HOME="$HOME/.android"
export NDK_HOME="$ANDROID_HOME/ndk/25.0.8775105"
then
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
then
cargo install --git https://github.com/tauri-apps/cargo-mobile2
then
mkdir hello
cd hello
cargo mobile init
then
cargo android run
and then I get this error
e: /home/riccardo/hello/gen/android/app/src/main/kotlin/app/tauri/hello/MainActivity.kt: (3, 22): Unresolved reference: WryActivity
In fact I didn't follow the [https://github.com/ealmloff/docsite/blob/7f57a13d28e4d5d47c6fd9e3022fb1d2989aee1b/docs-src/0.4/en/getting_started/mobile.md](Mobile App guide) PR you opened :sweat_smile: I'll try that asap :sweat_smile:
Ah same error :
e: /home/riccardo/dioxus-mobile-test/gen/android/app/src/main/kotlin/app/tauri/dioxus_mobile_test/MainActivity.kt: (3, 22): Unresolved reference: WryActivity
With this lib.rs, it compiles, but it crashes :
use anyhow::Result;
#[cfg(target_os = "android")]
fn init_logging() {
android_logger::init_once(
android_logger::Config::default()
.with_min_level(log::Level::Trace)
.with_tag("dioxus-mobile-test"), // This name may be different for your application
);
}
#[cfg(not(target_os = "android"))]
fn init_logging() {
env_logger::init();
}
#[cfg(any(target_os = "android", target_os = "ios"))]
fn stop_unwind<F: FnOnce() -> T, T>(f: F) -> T {
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
Ok(t) => t,
Err(err) => {
eprintln!("attempt to unwind out of `rust` with err: {:?}", err);
std::process::abort()
}
}
}
#[cfg(any(target_os = "android", target_os = "ios"))]
fn _start_app() {
stop_unwind(|| main().unwrap());
}
#[no_mangle]
#[inline(never)]
#[cfg(target_os = "android")]
pub extern "C" fn start_app() {
#[cfg(target_os = "android")]
//android_binding!(com_example, dioxus_mobile_test, _start_app); // This code may be different for your application
tao::android_binding!(
com_example,
wry_app,
WryActivity,
wry::android_setup, // pass the wry::android_setup function to tao which will invoke when the event loop is created
_start_app
);
wry::android_binding!(com_example, ttt);
}
use dioxus::prelude::*;
pub fn main() -> Result<()> {
// Right now we're going through dioxus-desktop but we'd like to go through dioxus-mobile
// That will seed the index.html with some fixes that prevent the page from scrolling/zooming etc
dioxus_desktop::launch::launch(
app,
Vec::new(),
// Note that we have to disable the viewport goofiness of the browser.
// Dioxus_mobile should do this for us
dioxus_desktop::Config::default().with_custom_index(r#"<!DOCTYPE html>
<html>
<head>
<title>Dioxus app</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<!-- CUSTOM HEAD -->
</head>
<body>
<div id="main"></div>
<!-- MODULE LOADER -->
</body>
</html>
"#.into()),
);
Ok(())
}
fn app() -> Element {
//let items = use_ref(cx, || vec![1, 2, 3]);
log::debug!("Hello from the app");
rsx! {
div {
h1 { "Hello, Mobile"}
div { margin_left: "auto", margin_right: "auto", width: "200px", padding: "10px", border: "1px solid black",
button {
onclick: move|_| {
println!("Clicked!");
// let mut items_mut = items.write();
// let new_item = items_mut.len() + 1;
// items_mut.push(new_item);
// println!("Requested update");
},
"Add item"
}
// for item in items.read().iter() {
// div { "- {item}" }
// }
}
}
}
}
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.os.Parcel.createExceptionOrNull(Parcel.java:2376)
at android.os.Parcel.createException(Parcel.java:2360)
at android.os.Parcel.readException(Parcel.java:2343)
at android.os.Parcel.readException(Parcel.java:2285)
at android.hardware.input.IInputManager$Stub$Proxy.injectInputEvent(IInputManager.java:1012)
at android.hardware.input.InputManager.injectInputEvent(InputManager.java:907)
at com.android.commands.input.Input.injectKeyEvent(Input.java:355)
at com.android.commands.input.Input.access$100(Input.java:41)
at com.android.commands.input.Input$InputKeyEvent.sendKeyEvent(Input.java:203)
at com.android.commands.input.Input$InputKeyEvent.run(Input.java:190)
at com.android.commands.input.Input.onRun(Input.java:107)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:60)
at com.android.commands.input.Input.main(Input.java:71)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:463)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.input.InputManagerService.injectInputEventInternal(InputManagerService.java:790)
at com.android.server.input.InputManagerService.injectInputEvent(InputManagerService.java:764)
at android.hardware.input.IInputManager$Stub.onTransact(IInputManager.java:459)
at android.os.Binder.execTransactInternal(Binder.java:1154)
at android.os.Binder.execTransact(Binder.java:1123)
Sorry for the long delay, I have updated the wry template in #281
Got similar error trying to run just the sample hello on Windows 10.
error: failed to run custom build command for `wry v0.37.0`
Caused by:
process didn't exit successfully: `D:\compiler\hello\target\debug\build\wry-211e92e477c67e7e\build-script-build` (exit code: 101)
--- stdout
cargo:rerun-if-env-changed=WRY_ANDROID_PACKAGE
cargo:rerun-if-env-changed=WRY_ANDROID_LIBRARY
cargo:rerun-if-env-changed=WRY_ANDROID_KOTLIN_FILES_OUT_DIR
--- stderr
thread 'main' panicked at C:\Users\xxxx\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wry-0.37.0\build.rs:37:11:
Failed to canonicalize `WRY_ANDROID_KOTLIN_FILES_OUT_DIR` path D:\compiler\hello\gen\android/app/src/main/kotlin/com/example
cargo mobile doctor
[✔] cargo-mobile v0.12.2
• Contains commits up to "Publish New Versions (#344)\n"
• Installed at "C:\\Users\\xxxx\\.cargo\\.cargo-mobile2"
• Windows 10 v10.0
• rustc v1.77.2 (25ef9e3d8 2024-4-9)
[✔] Android developer tools
• SDK v26.1.1 installed at "C:\\Users\\xxxx\\AppData\\Local\\Android\\Sdk"
• NDK v26.1.10909125 installed at "C:\\Users\\xxxx\\AppData\\Local\\Android\\Sdk\\ndk\\26.1.10909125"
[✔] Connected devices
(M2101K6G) Note 10 Pro
Also noticed that we can't actually do a cargo mobile init on existing tauri apps right? I was expecting it to update existing tauri app config though at least to match because if we do have existing desktop app it won't be easily migrated into the mobile version?