ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.09k stars 1k forks source link

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

Closed ryaa closed 7 months ago

ryaa commented 7 months ago

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

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 commented 7 months ago

@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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

@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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

No more issue with 5.7.2. Thanks.

ionitron-bot[bot] commented 6 months ago

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.