Skip to content
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

[Bug]: Application crashes on Android after upgrading to the latest 5.7.1 version #7309

Closed
1 of 3 tasks
ryaa opened this issue Mar 4, 2024 · 9 comments · Fixed by #7308
Closed
1 of 3 tasks

[Bug]: Application crashes on Android after upgrading to the latest 5.7.1 version #7309

ryaa opened this issue Mar 4, 2024 · 9 comments · Fixed by #7308
Labels

Comments

@ryaa
Copy link
Contributor

ryaa commented Mar 4, 2024

Capacitor Version

alexryltsov@Alexs-MacBook-Pro-2 chore-781-upgrade-to-latest-capacitor-5x-and-native-dependencies-to-latest-versions % npx cap doctor
💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 5.7.1
@capacitor/core: 5.7.1
@capacitor/android: 5.7.1
@capacitor/ios: 5.7.1

Installed Dependencies:

@capacitor/cli: 5.5.0
@capacitor/core: 5.7.1
@capacitor/android: 5.7.1
@capacitor/ios: 5.7.1

[success] iOS looking great! 👌
[success] Android looking great! 👌

Other API Details

alexryltsov@Alexs-MacBook-Pro-2 chore-781-upgrade-to-latest-capacitor-5x-and-native-dependencies-to-latest-versions % npm --version
10.5.0

alexryltsov@Alexs-MacBook-Pro-2 chore-781-upgrade-to-latest-capacitor-5x-and-native-dependencies-to-latest-versions % node --version
v20.11.1

Platforms Affected

  • iOS
  • Android
  • Web

Current Behavior

Application crashes on Android after upgrading to the latest 5.7.1 version

2024-03-04 17:57:05.341 21704-21752 cr_AwBgThreadClient     io.ionic.starter                     E  Client raised exception in shouldInterceptRequest. Re-throwing on UI thread.
2024-03-04 17:57:05.342 21704-21704 cr_AwBgThreadClient     io.ionic.starter                     E  The following exception was raised by shouldInterceptRequest:
2024-03-04 17:57:05.343 21704-21704 AndroidRuntime          io.ionic.starter                     D  Shutting down VM
2024-03-04 17:57:05.353 21704-21704 AndroidRuntime          io.ionic.starter                     E  FATAL EXCEPTION: main
                                                                                                    Process: io.ionic.starter, PID: 21704
                                                                                                    java.lang.IndexOutOfBoundsException: No group 1
                                                                                                    	at java.util.regex.Matcher.group(Matcher.java:589)
                                                                                                    	at java.util.regex.Matcher.appendEvaluated(Matcher.java:917)
                                                                                                    	at java.util.regex.Matcher.appendReplacementInternal(Matcher.java:890)
                                                                                                    	at java.util.regex.Matcher.appendReplacement(Matcher.java:1040)
                                                                                                    	at java.util.regex.Matcher.replaceFirst(Matcher.java:1468)
                                                                                                    	at java.lang.String.replaceFirst(String.java:2757)
                                                                                                    	at com.getcapacitor.JSInjector.getInjectedStream(JSInjector.java:75)
                                                                                                    	at com.getcapacitor.WebViewLocalServer.handleLocalRequest(WebViewLocalServer.java:411)
                                                                                                    	at com.getcapacitor.WebViewLocalServer.shouldInterceptRequest(WebViewLocalServer.java:201)
                                                                                                    	at com.getcapacitor.BridgeWebViewClient.shouldInterceptRequest(BridgeWebViewClient.java:23)
                                                                                                    	at WV.g6.a(chromium-TrichromeWebViewGoogle6432.aab-stable-616717833:86)
                                                                                                    	at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle6432

Expected Behavior

The app works as expected and does not crash

Project Reproduction

https://github.com/ryaa/capacitor-android-5.7.1-app-crash

Additional Information

The problem seems to be related with the changes in JSInjector.java file in the recent @capacitor/android release v5.7.1. The problem is that some plugins (specifically cordova-plugin-advanced-http plugin) might embeds some scripts that has some special chars that needs to be properly escaped - see below
Screenshot 2024-03-04 at 6 42 17 PM
and
Screenshot 2024-03-04 at 6 42 28 PM

If this is not done, if the replacement string, passed to replaceFirst method, contains $1 or other $-prefixed sequences, the replaceFirst method will interpret them as references to capture groups and, since there might be no such a capture group, there will be an IndexOutOfBoundsException as shown above. This PR should fix this problem

@dodomui
Copy link

dodomui commented Mar 6, 2024

@ryaa The PR above doesn't solved my problem. The problem still persist and even after I downgrade to 5.7.0 and 5.6.0.
Help

@aheadfullofcode
Copy link

aheadfullofcode commented Mar 6, 2024

If I revert to the following code, the app build works.

String js = "<script type=\"text/javascript\">" + getScriptString() + "</script>"; String html = this.readAssetStream(responseStream); if (html.contains("<head>")) { html = html.replace("<head>", "<head>\n" + js + "\n"); } else if (html.contains("</head>")) { html = html.replace("</head>", js + "\n" + "</head>"); } else { Logger.error("Unable to inject Capacitor, Plugins won't work"); } return new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));

@ryaa
Copy link
Contributor Author

ryaa commented Mar 6, 2024

If I revert to the following code, the app build works.

The problem that PR fixes is not related to the app build but it fixes the problem when the app is launched and immediately crashes with the error shown in the original description.

@ryaa
Copy link
Contributor Author

ryaa commented Mar 6, 2024

@ryaa The PR above doesn't solved my problem. The problem still persist and even after I downgrade to 5.7.0 and 5.6.0. Help

Please make sure to sync the native project after the you install a different version of @capacitor/android. The problem, as I believe, was introduced in 5.7.1 and everything should work fine in 5.7.0 or below

@dodomui
Copy link

dodomui commented Mar 7, 2024

If I revert to the following code, the app build works.

String js = "<script type=\"text/javascript\">" + getScriptString() + "</script>"; String html = this.readAssetStream(responseStream); if (html.contains("<head>")) { html = html.replace("<head>", "<head>\n" + js + "\n"); } else if (html.contains("</head>")) { html = html.replace("</head>", js + "\n" + "</head>"); } else { Logger.error("Unable to inject Capacitor, Plugins won't work"); } return new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));

This method solved my problem. Thanks @aheadfullofcode
@ryaa Thanks. Will try with the new PR also.

@ryaa
Copy link
Contributor Author

ryaa commented Mar 7, 2024

Will try with the new PR also.

The new @capacitor/android v5.7.2 has already been released with the appropriate fix. You can install and use it.

@Madserker
Copy link

Hello,
I did the migration to Capacitor 6 (using the tag "next" in package.json) and seems like I have the same problem, will there be a new version too for Capacitor 6 with this fix?

@dodomui
Copy link

dodomui commented Mar 13, 2024

No more issue with 5.7.2.
Thanks.

Copy link

ionitron-bot bot commented Apr 12, 2024

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.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants