-
-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android support #21
Comments
Android has its own webview, available in java / kotlin. |
@marcomq We are actually looking for ways to build from its webview jni too! Because the inner webview library ABI is not exposed afaik. |
Actually no idea. My android knowledge is pretty basic. I was just interested on how this is planned. |
It shouldn't be hard to support android, maybe should consider more priority on this issue |
@Mohsen7s Btw, we appreciate every helping hand we can get, especially for Android support. |
Hey guys wanna give some update on current progress. It doesn't contain lots of things because I'm focussing on how to make it more structural and ergonomic for users to do everythings they want. The sad thing is we can't create a NDK and use This also affect after webview creation because we could only write FFI function to communicate with koltin code. We can't control the event loop after So for the foreseeable future, the most reasonable todo list I could come up with is following:
Do you think this will be the reasonable progress? Any feedback is welcome! |
Afaik, Activities on android has to be declared in the manifest and can't be created at runtime so I don't think multi-window or multi-activity WRY app is possible and probably shouldn't. I guess we could make a macro that lets users define an entry point for an activity (it can be used multiple times for multiple activities) and we create these activities in the manifest at compile time. I think Mobile support in general should be one window and use a client-side router or any kind of web navigation but that's just me, I don't think I would ever want multiple activities. |
@amrbashir Yes, Activity has to be registered to manifest and cannot be created dynabmically during runtime. One possibility is running build script macro to edit the AndroidManifest.xml |
We can use fragments, they can be dynamically created and I think multiple activities are more resource hungry than using multiple fragments in one activity. Jetpack Compose, which is the new modern toolkit to build native android app using kotlin, also uses fragments for multiple screens rather than activities. |
Quick update for some great news. Init_script and IPC handling are possible! |
Continued in.. |
What's the current status of the issue? Is anyone working on it? What would a complete newbie to the project need to know to help with the issue? |
@Adhalianna we're actively working on it, see https://github.com/tauri-apps/cargo-mobile and the |
Can you elaborate on this? I don't quite understand why you can't subclass NativeActivity and add a webview plus some additional methods to interface with the webview. Saddly the way it's currently handled there is no way to build without a complex build system like gradle, even after building a maven clone in rust and shelling out to kotlin/r8, since the AppCompatActivity requires support for res/drawables/etc. This is how far I've got with trying to get xbuild to build a wry app for android:
|
@wusyong is this the issue you encountered? https://stackoverflow.com/questions/72767602/creating-android-webview-in-nativeactivity-with-jni-in-rust |
well, there are problems, but at least the one in the thread can be fixed by calling // Copyright 2020-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use super::{WebContext, WebViewAttributes, RGBA};
use crate::{application::window::Window, Result};
use jni::objects::JObject;
use ndk::looper::ThreadLooper;
use std::rc::Rc;
pub(crate) struct InnerWebView {
#[allow(unused)]
pub window: Rc<Window>,
}
impl InnerWebView {
pub fn new(
window: Rc<Window>,
attributes: WebViewAttributes,
_pl_attrs: super::PlatformSpecificWebViewAttributes,
_web_context: Option<&mut WebContext>,
) -> Result<Self> {
let context = ndk_context::android_context();
let vm = unsafe { jni::JavaVM::from_raw(context.vm().cast()) }?;
let env = vm.attach_current_thread()?;
let ctx = unsafe { JObject::from_raw(context.context().cast()) };
let class = env.find_class("android/os/Looper")?;
env.call_static_method(class, "prepare", "()V", &[])?.v()?;
println!("Looper.prepare()");
let class = env.find_class("android/webkit/WebView")?;
let webview = env.new_object(class, "(Landroid/content/Context;)V", &[ctx.into()])?;
println!("WebView webview = new WebView(ctx)");
if let Some(url) = &attributes.url {
let url = env.new_string(url)?;
env.call_method(
webview,
"loadUrl",
"(Ljava/lang/String;)V",
&[url.into()]
)?;
println!("webview.loadUrl(url)");
}
/*env
.call_method(
ctx,
"setContentView",
"(Landroid/view/View;)V",
&[webview.into()],
)?
.v()?;
println!("setContentView(webview)");*/
Ok(InnerWebView { window })
}
pub fn print(&self) {}
pub fn eval(&self, _js: &str) -> Result<()> {
Ok(())
}
#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn open_devtools(&self) {}
#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn close_devtools(&self) {}
#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn is_devtools_open(&self) -> bool {
false
}
pub fn zoom(&self, _scale_factor: f64) {}
pub fn set_background_color(&self, _background_color: RGBA) -> Result<()> {
Ok(())
}
}
pub fn platform_webview_version() -> Result<String> {
Ok("1".into())
} |
@dvc94ch I believe we have done this before. The result is Webview implementation doesn't work well with NDK. |
Got it working with gradle. Is the android-activity winit backend going to be adopted by tao? Currently tao does it's own weird thing and seems to be tightly coupled with and built specifically for wry. That would make experimenting with the wry android backend easier and tooling to not have to add special support for tao/wry. |
Yes, itwill be the backend after it's merged in winit. The setup from tao is to connect to our project template from cargo-mobile fork. |
@dvc94ch got a repo/example anywhere of an Android Gradle project that uses |
not really, xbuild handles this all in the background. you can have a look at
|
is there a reason why on android doesn't support custom protocols? can they be supported? |
It has been implemented @dvc94ch . Are you facing any issues? |
I'm not quite following what you implemented and why. I registered a custom asset handler that works on linux, macos, ios but not android (we haven't tested windows). what I'd like to do is this: use include_dir::{include_dir, Dir};
pub static ASSETS: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/assets");
pub fn asset_handler(request: &Request<Vec<u8>>) -> Result<Response<Vec<u8>>> {
let path = request.uri().to_string().replace("asset://", "");
let mime = match path.rsplit_once('.') {
Some((_, "css")) => "text/css",
Some((_, "png")) => "image/png",
_ => "application/octet-stream",
};
let file = if let Some(file) = ASSETS.get_file(path) {
file
} else {
return Response::builder()
.status(StatusCode::NOT_FOUND)
.body(b"Not Found".to_vec())
.map_err(From::from);
};
Response::builder()
.header("Content-Type", mime)
.body(file.contents().to_vec())
.map_err(From::from)
} |
sorry for being thick. I managed to get it working like this: macro_rules! img {
($file:literal) => {{
#[cfg(target_family = "wasm")]
let url = concat!("img/", $file);
#[cfg(target_os = "android")]
let url = concat!("https://asset.localhost/img/", $file);
#[cfg(not(any(target_family = "wasm", target_os = "android")))]
let url = concat!("asset://localhost/img/", $file);
url
}};
} |
It should work. On tauri we do |
Closing as android support have been out for a while now. |
Bellow list is exploring list, not implementation. I'll start implementing once all are feasible.
Evaluate script is meaningless until we can provide some Android window (activity/fragment) library. See this comment for more info.
The text was updated successfully, but these errors were encountered: