ArronJo / sample-webview

This project is a sample for developing android applications using webview.
20 stars 11 forks source link

Hardware acceleration #2

Open dioslibre opened 3 years ago

dioslibre commented 3 years ago

Hey @ArronJo.

I downloaded your WebView Test app to assess if my web app, which is based on OpenLayers, would run decently on an Android device, and it ran flawlessly, your app is awesome.

Anyways, I googled your email to check if you have something on GitHub, and sure enough, I found this repo, cloned it, ran it on my device, but it was slow compared to your app. I discovered that's because of webView.setLayerType(View.LAYER_TYPE_HARDWARE, null), so I added it, still slow, I also added android:hardwareAccelerated="true", still no luck.

Could you please shed some light, or maybe share the code of your app's WebView activity?

Thanks a million times.

dioslibre commented 3 years ago

I applied all the recommended settings from your WebviewTest App, and it's still 5 times slower. here's my code:

// MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        val webView = findViewById<WebView>(R.id.webview)

        webView.settings.javaScriptEnabled = true
        webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
        webView.settings.domStorageEnabled = true
        webView.settings.setGeolocationEnabled(true)
        webView.settings.loadsImagesAutomatically = true
        webView.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webView.settings.useWideViewPort = true
        webView.settings.setSupportZoom(true)

        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        webView.setInitialScale(0)
        webView.scrollBarStyle = WebView.SCROLLBARS_OUTSIDE_OVERLAY
        webView.isScrollbarFadingEnabled = true

        webView.loadUrl("http://192.168.43.95:8080/")
    }

// Manifest

<application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Gaea">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Do you have any suggestions?

ArronJo commented 3 years ago

Hi.

My WebViewTest app code doesn't make much difference either.

// AndroidManifest.xml

<application android:name="com.snc.test.zero.application.SNCApplication" android:allowBackup="true" @./app_backup_rules" @./ic_launcher" android:label="${applicationLabel}" android:supportsRtl="true" android:largeHeap="true" android:usesCleartextTraffic="true" android:extractNativeLibs="false" android:requestLegacyExternalStorage="true" android:preserveLegacyExternalStorage="true" @.***/AppTheme" tools:ignore="GoogleAppIndexingWarning,RtlEnabled,UnusedAttribute">

<meta-data android:name="android.webkit.WebView.EnableSafeBrowsing" android:value="true" />

<activity
    android:name="com.snc.test.webview.activity.MainActivity"
    android:launchMode="singleTop"
    android:configChanges="keyboardHidden|orientation|screenLayout|screenSize"
    android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
    android:alwaysRetainTaskState="true"
    android:clearTaskOnLaunch="true"
    android:finishOnTaskLaunch="true"
    android:exported="false"
    ***@***.***/AppTheme.NoActionBar">

// MainActivity

@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface", "AddJavascriptInterface"}) private static void setup(WebView webView) { WebSettings settings = webView.getSettings();

webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    webView.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_BOUND,

true); } settings.setAllowFileAccess(true); settings.setAllowContentAccess(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { settings.setAllowFileAccessFromFileURLs(true); settings.setAllowUniversalAccessFromFileURLs(true); } settings.setBlockNetworkImage(false); settings.setBlockNetworkLoads(false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { settings.setSafeBrowsingEnabled(false); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { settings.setDisabledActionModeMenuItems(WebSettings.MENU_ITEM_NONE); } settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(false); if (Build.VERSION.SDK_INT < 30) { // Build.VERSION_CODES.R settings.setAppCacheEnabled(false); } settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setDatabaseEnabled(true); settings.setDomStorageEnabled(true); settings.setLoadsImagesAutomatically(true); settings.setLoadWithOverviewMode(false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { settings.setOffscreenPreRaster(false); } settings.setSupportMultipleWindows(true); settings.setUseWideViewPort(true); settings.setSupportZoom(true); settings.setBuiltInZoomControls(true); settings.setDisplayZoomControls(false); settings.setGeolocationEnabled(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { settings.setMediaPlaybackRequiresUserGesture(true); // The default is true. Added in API level 17 }

settings.setUserAgentString(makeUserAgent(webView));

}

2021년 7월 25일 (일) 오전 4:49, Znaiber Youssef @.***>님이 작성:

Hey @ArronJo https://github.com/ArronJo.

I downloaded your WebView Test app to assess if my web app, which is based on openlayers, would run decently on an Android device, and it ran flawlessly, your app is awesome.

Anyways, I googled your email to check if you have something on GitHub, and sure enough, I found this repo, cloned it, ran it on my device, but it was slow compared to your app. I discovered that's because of Layer_type_hardware, so I added it, still slow, I also added android:usesHardwareAcceleration=''true'', still no luck.

Could you please shed some light, or maybe share the code of your app's WebView activity?

Thanks a million times.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ArronJo/sample-webview/issues/2, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEXTPPBMI4UZAZ67YQ3JKX3TZMKM7ANCNFSM5A5YSKZA .

dioslibre commented 3 years ago

I tried again with your code but with no luck, I also tried a bunch of other things from StackOverflow. It's just weird. Everybody saying the webview is slower than chrome, and there is no way around it. I'm giving up.

I just discovered chrome custom tabs, I tried with them and indeed my app runs faster now, so I'm going with that.

Thank you for your help.