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

feat(android): Add android.minWebviewVersion configuration option #5768

Merged
merged 9 commits into from
Jul 20, 2022
9 changes: 5 additions & 4 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public class Bridge {
private static final String BUNDLE_PLUGIN_CALL_BUNDLE_KEY = "capacitorLastPluginCallBundle";
private static final String LAST_BINARY_VERSION_CODE = "lastBinaryVersionCode";
private static final String LAST_BINARY_VERSION_NAME = "lastBinaryVersionName";
private final int MINIMUM_ANDROID_WEBVIEW_VERSION = 60;
private static final String MINIMUM_ANDROID_WEBVIEW_ERROR = "System WebView is not supported";

// The name of the directory we use to look for index.html and the rest of our web assets
Expand All @@ -86,6 +85,8 @@ public class Bridge {
public static final String CAPACITOR_HTTPS_SCHEME = "https";
public static final String CAPACITOR_FILE_START = "/_capacitor_file_";
public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_";
public static final int DEFAULT_ANDROID_WEBVIEW_VERSION = 60;
public static final int MINIMUM_ANDROID_WEBVIEW_VERSION = 55;

// Loaded Capacitor config
private CapConfig config;
Expand Down Expand Up @@ -306,7 +307,7 @@ public boolean isMinimumWebViewInstalled() {
PackageInfo info = WebView.getCurrentWebViewPackage();
String majorVersionStr = info.versionName.split("\\.")[0];
int majorVersion = Integer.parseInt(majorVersionStr);
return majorVersion >= MINIMUM_ANDROID_WEBVIEW_VERSION;
return majorVersion >= config.getMinWebViewVersion();
}

// Otherwise manually check WebView versions
Expand All @@ -318,7 +319,7 @@ public boolean isMinimumWebViewInstalled() {
PackageInfo info = pm.getPackageInfo(webViewPackage, 0);
String majorVersionStr = info.versionName.split("\\.")[0];
int majorVersion = Integer.parseInt(majorVersionStr);
return majorVersion >= MINIMUM_ANDROID_WEBVIEW_VERSION;
return majorVersion >= config.getMinWebViewVersion();
} catch (Exception ex) {
Logger.warn("Unable to get package info for 'com.google.android.webview'" + ex.toString());
}
Expand All @@ -327,7 +328,7 @@ public boolean isMinimumWebViewInstalled() {
PackageInfo info = pm.getPackageInfo("com.android.webview", 0);
String majorVersionStr = info.versionName.split("\\.")[0];
int majorVersion = Integer.parseInt(majorVersionStr);
return majorVersion >= MINIMUM_ANDROID_WEBVIEW_VERSION;
return majorVersion >= config.getMinWebViewVersion();
} catch (Exception ex) {
Logger.warn("Unable to get package info for 'com.android.webview'" + ex.toString());
}
Expand Down
15 changes: 15 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/CapConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.getcapacitor;

import static com.getcapacitor.Bridge.CAPACITOR_HTTP_SCHEME;
import static com.getcapacitor.Bridge.DEFAULT_ANDROID_WEBVIEW_VERSION;
import static com.getcapacitor.Bridge.MINIMUM_ANDROID_WEBVIEW_VERSION;
import static com.getcapacitor.FileUtils.readFile;

import android.content.Context;
Expand Down Expand Up @@ -43,6 +45,7 @@ public class CapConfig {
private boolean webContentsDebuggingEnabled = false;
private boolean loggingEnabled = true;
private boolean initialFocus = true;
private int minWebViewVersion = DEFAULT_ANDROID_WEBVIEW_VERSION;
private String errorPath;

// Embedded
Expand Down Expand Up @@ -125,6 +128,7 @@ private CapConfig(Builder builder) {
this.webContentsDebuggingEnabled = builder.webContentsDebuggingEnabled;
this.loggingEnabled = builder.loggingEnabled;
this.initialFocus = builder.initialFocus;
this.minWebViewVersion = builder.minWebViewVersion;
this.errorPath = builder.errorPath;

// Embedded
Expand Down Expand Up @@ -180,6 +184,7 @@ private void deserializeConfig(@Nullable Context context) {
"android.allowMixedContent",
JSONUtils.getBoolean(configJSON, "allowMixedContent", allowMixedContent)
);
minWebViewVersion = JSONUtils.getInt(configJSON, "android.minWebViewVersion", DEFAULT_ANDROID_WEBVIEW_VERSION);
captureInput = JSONUtils.getBoolean(configJSON, "android.captureInput", captureInput);
webContentsDebuggingEnabled = JSONUtils.getBoolean(configJSON, "android.webContentsDebuggingEnabled", isDebug);

Expand Down Expand Up @@ -279,6 +284,15 @@ public boolean isInitialFocus() {
return initialFocus;
}

public int getMinWebViewVersion() {
if (minWebViewVersion < MINIMUM_ANDROID_WEBVIEW_VERSION) {
Logger.warn("Specified minimum webview version is too low, defaulting to " + MINIMUM_ANDROID_WEBVIEW_VERSION);
return MINIMUM_ANDROID_WEBVIEW_VERSION;
theproducer marked this conversation as resolved.
Show resolved Hide resolved
}

return minWebViewVersion;
}

public PluginConfig getPluginConfiguration(String pluginId) {
PluginConfig pluginConfig = pluginsConfiguration.get(pluginId);
if (pluginConfig == null) {
Expand Down Expand Up @@ -436,6 +450,7 @@ public static class Builder {
private Boolean webContentsDebuggingEnabled = null;
private boolean loggingEnabled = true;
private boolean initialFocus = false;
private int minWebViewVersion = DEFAULT_ANDROID_WEBVIEW_VERSION;

// Embedded
private String startPath = null;
Expand Down
10 changes: 10 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ export interface CapacitorConfig {
* @default true
*/
initialFocus?: boolean;

/**
* The minimum supported webview version on Android supported by your app.
*
* The minimum supported cannot be lower than version `55`, which is required for Capacitor.
*
* @since 4.0.0
* @default 60
*/
minWebViewVersion?: number;
};

ios?: {
Expand Down