You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Capacitor on Android detected as web platform after redirecting with allowNavigation #7454
After redirecting to an external URL that is allowed in the server.allowNavigation config setting, an android app reports being on a web platform instead of android.
This can be seen by calling Capacitor.getPlatform() (which returns android before redirecting and web after redirecting) or by trying to use a native API, which will either fallback to the web version of the API, or fail to run at all
All this works on iOS, where after redirecting, the app can still use native iOS APIs
Expected Behavior
After redirecting, the android app should still have access to all the native Android APIs (and report itself as being an Android platform)
Reproduction details are in the repo, but the basic reproduction steps are as follows:
Serve a valid capacitor app on your computer (i.e. with npx serve .)
Add your computer's IP to the server.allowNavigation capacitor config setting
Load the application on your (real or simulated) device
Notice that Capacitor.getPlatform() returns android on android and ios on iOS
Redirect to the hosted server (any method works, can be window.location.assign(), window.location.href = "", window.location.replace(), or redirecting through a form
Notice that Capacitor.getPlatform() now returns web on android but still ios on iOS
Here are videos showing the issue. Even though these were recorded on a simulator, the results are identical to a physical device
android.movios.mov
Our use case is to have one app that connects to different backends on different subdomains, very similar to Slack. So far everything is working except the android app loses access to its native APIs
Reacted by Porter Severtson, Matthias Schwarz, Cowboy Ho, Thomas Heigl, Koen, Michael Khabarov, khamaileon, Henrique Malheiros, nth, Ipunk Vizard and 8 more
So it looks like there are two parts to this issue: redirecting to a local address, and capacitor not injecting the JS bridge into external pages. Again, both of these are only relevant to Android, the iOS side works perfectly
For the first issue, if we try to navigate to a computer on the local network (http, through ip address or [computername].local) we would get the issue described above where the platform would be seen as web and the plugins would use the web version or fail entirely. If we instead redirect to a remotely hosted server (https), we get a slightly different issue.
If we add our remote server to the server.allowNavigation config and redirect to it using the steps above, the platform is reported as android correctly, but all plugins are completely broken:
Investigating this issue, we found the root cause to be the JavaScript injection in Bridge.java. We notice that if WebViewFeature.DOCUMENT_START_SCRIPTis supported, the javascript is only injected into the base URL (if it is NOT supported (i.e by commenting the if guard out), the injector always works). If the addDocumentStartJavaScript() function were changed to inject on all allowedOriginRules (the base URL and everything in the server.allowNavigation setting) instead of just the base URL, all plugins work again.
// Start the local web serverJSInjectorinjector = getJSInjector();
if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
WebViewCompat.addDocumentStartJavaScript(webView, injector.getScriptString(), allowedOriginRules);
injector = null;
}
localServer = newWebViewLocalServer(context, this, injector, authorities, html5mode);
I am new to capacitor and have no idea if this would have any side effects, but this seems to fix our issue completely and bring parity with the iOS version.
So to summarize:
The original issue still exists while redirecting to a computer on a local network (or just over http, I don't have the capability to test that right now)
There also appears to be a bug when redirecting to a remote server (again, maybe just over https) caused by the native bridge not injecting capacitor's Javascript on remote pages. A fix we found was to make the change above, but I'd rather ask if it is the correct approach before creating a pull request.
Reacted by Porter Severtson, Matthias Schwarz, Robin Zaagsma, Jarred Hawkins, Thomas Heigl, Cowboy Ho, Koen, Kirk Delderman, Ipunk Vizard, iqmeta GmbH and 1 more
I'm experiencing this as well. I use the allowNavigation to switch between different stages of the app, and this prevents that workflow. Thank you for all of the digging you did, the detailed explanation, and workaround!
We are experiencing this issue as well, and I cannot thank you enough for having found this workaround 🙇 I can confirm that changing the addDocumentStartJavaScript call to just use allowedOriginRules fixes the issues for https requests on local networks. Using the fallback jsInjector does not however.
I have hacked together a quick "patch" that we call in the MainActivity, to avoid editing the Bridge.Java file directly (as it gets messy with version control). Here it is in case anyone wants to use it (call it after onCreate and before loading any other pages):
classMainActivity : BridgeActivity() {
overridefunonCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
/** * ...*/
patchJSInjection();
}
/** * ...*/privatefunpatchJSInjection() {
try {
if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
val getJsInjector = bridge::class.java.declaredMethods.single { it.name =="getJSInjector" };
getJsInjector.isAccessible =true;
val injector = getJsInjector.invoke(bridge);
val getScriptString = injector::class.java.declaredMethods.single { it.name ==="getScriptString" };
val scriptString = getScriptString.invoke(injector) asString;
val allowedOrigins:MutableSet<String> =mutableSetOf();
// Add origins that the Capacitor JS Bridge should be injected into
allowedOrigins.add("https://www.foo.bar");
WebViewCompat.addDocumentStartJavaScript(bridge.webView, scriptString, allowedOrigins)
}
}catch (e:Exception) {
Log.e("Error", e.message ?:"");
}
}
}
I have hacked together a quick "patch" that we call in the MainActivity, to avoid editing the Bridge.Java file directly (as it gets messy with version control). Here it is in case anyone wants to use it (call it after onCreate and before loading any other pages):
@Vadinci huge thanks for that snippet. I hacked together similar in java:
In our case the issue was, that the script did not get injected on allowNavigation pages because it is not provided to the addDocumentStartJavaScript function:
This issue has been labeled as type: bug. This label is added to issues that that have been reproduced and are being tracked in our internal issue tracker.
I have hacked together a quick "patch" that we call in the MainActivity, to avoid editing the Bridge.Java file directly (as it gets messy with version control). Here it is in case anyone wants to use it (call it after onCreate and before loading any other pages):
@Vadinci huge thanks for that snippet. I hacked together similar in java:
Thank you so much for this. I was thinking it had something to do with the JS code.
One mention: minSdkVersion will need to get bumped to 24, from the default 22. Just for the Java syntax sugar.
My allowedOrigins collection looks like this, as I have simple domain names in allowNavigation:
Closing this out in favor of #8661, where the active discussion and plan live. We're taking the fix in Capacitor 10 since it counts as a breaking change. Thanks for the original report and the reproduction!
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.
Activity
Dylancyclone commentedon May 8, 2024
So it looks like there are two parts to this issue: redirecting to a local address, and capacitor not injecting the JS bridge into external pages. Again, both of these are only relevant to Android, the iOS side works perfectly
For the first issue, if we try to navigate to a computer on the local network (http, through ip address or
[computername].local) we would get the issue described above where the platform would be seen asweband the plugins would use the web version or fail entirely. If we instead redirect to a remotely hosted server (https), we get a slightly different issue.If we add our remote server to the
server.allowNavigationconfig and redirect to it using the steps above, the platform is reported asandroidcorrectly, but all plugins are completely broken:Investigating this issue, we found the root cause to be the JavaScript injection in Bridge.java. We notice that if
WebViewFeature.DOCUMENT_START_SCRIPTis supported, the javascript is only injected into the base URL (if it is NOT supported (i.e by commenting the if guard out), the injector always works). If theaddDocumentStartJavaScript()function were changed to inject on allallowedOriginRules(the base URL and everything in theserver.allowNavigationsetting) instead of just the base URL, all plugins work again.Here's the diff:
Original:
capacitor/android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Lines 254 to 269 in f8264cc
New:
I am new to capacitor and have no idea if this would have any side effects, but this seems to fix our issue completely and bring parity with the iOS version.
So to summarize:
The original issue still exists while redirecting to a computer on a local network (or just over http, I don't have the capability to test that right now)
There also appears to be a bug when redirecting to a remote server (again, maybe just over https) caused by the native bridge not injecting capacitor's Javascript on remote pages. A fix we found was to make the change above, but I'd rather ask if it is the correct approach before creating a pull request.
danbritt commentedon May 30, 2024
I'm experiencing this as well. I use the allowNavigation to switch between different stages of the app, and this prevents that workflow. Thank you for all of the digging you did, the detailed explanation, and workaround!
Vadinci commentedon Jun 3, 2024
We are experiencing this issue as well, and I cannot thank you enough for having found this workaround 🙇 I can confirm that changing the
addDocumentStartJavaScriptcall to just useallowedOriginRulesfixes the issues for https requests on local networks. Using the fallbackjsInjectordoes not however.I have hacked together a quick "patch" that we call in the MainActivity, to avoid editing the Bridge.Java file directly (as it gets messy with version control). Here it is in case anyone wants to use it (call it after
onCreateand before loading any other pages):jarredhawkins commentedon Jun 18, 2024
@Vadinci huge thanks for that snippet. I hacked together similar in java:
Cow258 commentedon Jun 20, 2024
I got same issue too.
To resolve this issue, I apply a patch to
Bridge.javaafter it has been builtrfe-css commentedon Jul 15, 2024
In our case the issue was, that the script did not get injected on allowNavigation pages because it is not provided to the addDocumentStartJavaScript function:
i also recommend using https://www.npmjs.com/package/patch-package for all these patches until it is being resolved.
ionitron-bot commentedon Oct 10, 2024
This issue has been labeled as
type: bug. This label is added to issues that that have been reproduced and are being tracked in our internal issue tracker.schniper commentedon Oct 19, 2024
Thank you so much for this. I was thinking it had something to do with the JS code.
One mention: minSdkVersion will need to get bumped to 24, from the default 22. Just for the Java syntax sugar.
My allowedOrigins collection looks like this, as I have simple domain names in allowNavigation:
yupengfei commentedon Dec 1, 2024
patchJSInjection Not work, hope fix
arieftb commentedon Jan 16, 2025
I face this issue too when navigate to different sub domain.
Hope this issue can be fixed.
Is patchJSInjection solution still work ?
arieftb commentedon Jan 16, 2025
OK the patchJSInjection solution still work. Thanks
rally25rs commentedon Mar 23, 2025
adrianomarquess commentedon Mar 31, 2025
I had the same problem in a production app, this solution worked for me 🙌🏾
eric-horodyski commentedon Oct 21, 2026
Closing this out in favor of #8661, where the active discussion and plan live. We're taking the fix in Capacitor 10 since it counts as a breaking change. Thanks for the original report and the reproduction!
ionitron-bot commentedon Nov 20, 2026
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.