AndroidX Webkit

2ddbd112dbc8c6572fe79821584c1465?s=47 Kurun
November 27, 2019

AndroidX Webkit

2ddbd112dbc8c6572fe79821584c1465?s=128

Kurun

November 27, 2019
Tweet

Transcript

  1. AndroidX Webkit ू·ΕKotlin޷͖ʂKotlinѪ޷ձ Vol.17 (2019/11/27) @kurun_pan

  2. • ຊۀ ‣ Linux Software Engineer ✓ Linux kernel, Driver,

    Middleware, Application ͳͲඞཁͳΒԿͰ΋ ✓ ࠷ۙ͸UIϑϨʔϜϫʔΫؔ࿈ • ϓϥΠϕʔτ ‣ ੵۃతʹษڧձʹࢀՃˍొஃத ‣ ओʹAndroidΞϓϦΛ։ൃ Self-introduction @kurun_pan @Kurun-pan
  3. • Webkit֓ཁ ‣ Webkitͱ͸ʁ ‣ WebKitͱͷҧ͍΍ΞʔΩςΫνϟͳͲ • AndroidX WebKitͷ৽ػೳ ‣

    ຊ೔།ҰͷKotlinཁૉɻKotlinཁૉ͕ബͯ͘͢Έ·ͤΜ… • ͦͷଞ ຊ೔ͷ಺༰
  4. • AndroidX JetpackϥΠϒϥϦͷҰͭ ‣ Support Libraryͷஔ͖׵͑ ✓ API Level 14Ҏ্ͷαϙʔτ

    • ԼҐޓ׵ੑҎ֎ʹఏڙ͞ΕΔػೳ ‣ WebViewʹؔΘΔศརͳઃఆ • ެࣜ৘ใ ‣ APIϦϑΝϨϯε ✓ https://developer.android.com/reference/androidx/webkit/package-summary ‣ ϦϦʔεϊʔτ ✓ https://developer.android.com/jetpack/androidx/releases/webkit ‣ ιʔείʔυ ✓ https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/webkit Webkitͱ͸ʁ
  5. ϨϯμϦϯάΤϯδϯ ϕʔε 4BGBSJ 8FC,JU  ,)5.- $ISPNF #MJOL 8FC,JU 'JSFGPY

    (FDLP 4FSWPʹஔ͖׵͑த  ͱ͍͏͜ͱͰɺandroidx.webkitͱ͍͏໊લ͕एׯࠞཚΛট͔͘΋ ʢandroid.webkitͷ࣌୅͸࢓ํ͕ແ͍ʹͯ͠΋ɺAndroidX΋ಉ͡Ͱ͔͢ʣ WebkitͱWebKitͷҧ͍ AndroidͷWebkitͱɺAndroidҎ֎ͷWebKit͸ҧ͍·͢
  6. HTMLϨϯμϦϯάΤϯδϯͷ੎ྗਤ 8FC,JU #MJOL (FDLP 4BGBSJ $ISPNF 'JSFGPY 14ϒϥ΢β .JDSPTPGU&EHF 0QFSB

    ϚΠφʔͳϒϥ΢βͳΒʜ
  7. • WebView ϨϯμϦϯάΤϯδϯ ‣ Android ʙ4.3ɿWebKit ‣ Android 4.4ʙɿBlink •

    WebViewͷόΠϯσΟϯάઌ ‣ Android ʙ4.4ɿAndroid OS૊ΈࠐΈ ‣ Android ʙ6.0ɿSystem WebViewಠཱʢapkͰಠཱ഑৴͞ΕɺPlayετΞͰߋ৽ʣ ‣ Android 7.0ʙɿChrome or System WebViewʢChrome༏ઌʣ ‣ Android 10ɿSystem WebView ✓ System WebViewͱChrome WebView͕ڞ௨Խ͞ΕɺTrichromeͱݺ͹Ε͍ͯΔΒ͍͠ - ϥΠϒϥϦ໊ࣗମ͸·ͨલͷlibmonochrome.so - https://chromium.googlesource.com/chromium/src.git/+/master/docs/ android_native_libraries.md Androidʹ͓͚ΔWebkitͷྺ࢙
  8. AndroidX WebkitΞʔΩςΫνϟ BOESPJEYXFCLJU 6TFS"QQMJDBUJPO $ISPNJVN "OESPJE8FC7JFX'SBNFXPSL $ISPNF'SBNFXPSL #MJOLMJCSBSZ TP 

    +/* /BUJWF8FC$PSF 4LJB %HSBQIJDTϥΠϒϥϦ /BUJWF+BWB4DSJQU$PSF /BUJWF#MJOL*OUFSGBDF "1* ڊେͳ ϥΠϒϥϦ "OESPJEҎ߱ ೖΓޱ͕ด࠯ʁ )8 (16
  9. • ҎԼ͸android.webkitʹ͸ͳ͍ɺ৽͍͠ػೳͰ͢ ‣ WebViewFeature ‣ ProxyController ‣ WebViewAssetLoader ‣ Dark

    Mode Controlʢ࣍ͷόʔδϣϯͰରԠ༧ఆʣ AndroidX Webkit৽ػೳ
  10. Build.gradle dependencies { implementation “androidx.webkit:webkit:1.1.0” } ࢖͍࢝ΊΔʹ͸·ͣҎԼΛ௥Ճ͠·͠ΐ͏

  11. WebViewFeature if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { WebView.startSafeBrowsing(this.applicationContext) { value ->

    Log.d(TAG, "WebView.startSafeBrowsing: $value") } } if (WebViewFeature.isFeatureSupported(WebViewFeature.START_SAFE_BROWSING)) { WebViewCompat.startSafeBrowsing(this.applicationContext) { value -> Log.d(TAG, "WebViewCompat.startSafeBrowsing: $value") } } όʔδϣϯίʔυ͔Βɺػೳαϙʔτ༗ແͰ൑அ΁ɻ͜Ε͸خ͍͠
  12. ProxyController if (WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE)) { // proxy1.com͕ࣦഊͨ͠Βɺ࣍͸proxy2.comͷΑ͏ʹ্͔Βॱ൪ʹࢼ͍ͯ͘͠ val proxyConfig = ProxyConfig.Builder()

    .addProxyRule("proxy1.com") .addProxyRule("proxy2.com", ProxyConfig.MATCH_HTTP) .addProxyRule("proxy3.com", ProxyConfig.MATCH_HTTPS) .addBypassRule("www.google.*") // ϓϩΩγઃఆআ֎ͷϗετ .build() // Executor val executor = Executor { Log.d(TAG, "${Thread.currentThread().name} : executor") } // ϓϩΩγઃఆมߋ͕ड෇͞Εͨ࣌ʹݺ͹ΕΔʁݺ͹Εͳ͍Α͏ͳ… val listener = Runnable { Log.d(TAG, "${Thread.currentThread().name} : listener") } // WebViewͷϓϩΩγઃఆΛγεςϜઃఆ͔Β্ॻ͖ ProxyController.getInstance().setProxyOverride(proxyConfig, executor, listener) // γεςϜͷઃఆʹ໭͢ ProxyController.getInstance().clearProxyOverride(executor, listener) } ϓϩΩγઃఆ͕؆୯ʹઃఆग़དྷΔΑ͏ʹʂʢ൓ө͸֘౰ΞϓϦͷWebVievͷΈʣ ࠓ·Ͱ͸8FC7JFXͷϓϩΩγઃఆͷඪ४తͳํ ๏͸ແ͔ͬͨͰ͢ΑͶʁʢग़དྷΔํ๏͸͋Γˣʣ IUUQTTUBDLPWFSqPXDPNRVFTUJPOT XFCWJFXBOESPJEQSPYZ
  13. WebViewAssetLoader val assetLoader = WebViewAssetLoader.Builder() .addPathHandler("/assets/", AssetsPathHandler(this)) // main/assetsσΟϨΫτϦͷొ࿥ .addPathHandler("/res/",

    ResourcesPathHandler(this)) // main/resσΟϨΫτϦͷొ࿥ .build() webView.webViewClient = object : WebViewClient() { // ϦΫΤετURLΛϑοΫͯ͠ϩʔΧϧͷϑΝΠϧΛදࣔɻWebVieͰಡΈࠐΜͩશϦΫΤετͷURL͸ඞͣ͜͜Λ௨Δ override fun shouldInterceptRequest( view: WebView, request: WebResourceRequest ): WebResourceResponse? { return assetLoader.shouldInterceptRequest(request.url) } } // ΞϓϦͷσϑΥϧτυϝΠϯ͸"appassets.androidplatform.net" // main/assets/www/index.htmlΛϩʔυ webView.loadUrl("https://appassets.androidplatform.net/assets/www/index.html") ϩʔΧϧHTMLϑΝΠϧͷϩʔυํ๏͸͜Ε·Ͱͱେͯ͠มΘΒͳ͍
  14. Dark Mode Control for WebView if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { //WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_AUTO)

    //WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_OFF) WebSettingsCompat.setForceDark(webView.settings, WebSettingsCompat.FORCE_DARK_ON) } ΋͏͙͢ϦϦʔε͞ΕΔ࣍ظόʔδϣϯͷ1.2.0ͰWebViewͷμʔΫϞʔυ͕Մೳग़དྷΔΒ͍͠ʂ ύϥϝʔλ આ໌ '03$&@%"3,@"650 γεςϜઃఆʹै͏ '03$&@%"3,@0'' ڧ੍0'' '03$&@%"3,@0/ ڧ੍0/
  15. • ϩʔΧϧίϯςϯπʢʴϦϯΫઌͷݻఆαΠτఔ౓ʣͷදࣔ ‣ WebViewΛ࢖ͬͨํָ͕ • ϒϥ΢βͱͯ͠ෆಛఆଟ਺ͷαΠτΛදࣔ ‣ Chrome Custom TabsΛར༻ͨ͠ํ͕҆શͰָ

    ‣ ϒϥ΢β૬౰ͷڍಈΛ͢Δʹ͸͔ͳΓ࡞ΓࠐΈ͕ඞཁͳ͸ͣ ‣ ηΩϡϦςΟ؍఺Ͱ͸ɺWebViewΛ࢖͏ͷ͸͋·Γྑ͘ͳ͍ WebView or Chrome Custom Tabs ?
  16. ࠷ޙʹ • AndroidX Webkit͸ߋ৽ස౓͕ߴ͘ɺൃల్্ ‣ android.webkitʹ͔͠ͳ͍ػೳ΋͋ΔͨΊɺ྆ํ࢖͏͜ͱʹ ‣ ͦͯ͠৘ใ΋গͳ͍ ✓ ࠓճͷ಺༰͸ޙ΄ͲQiitaʹιʔείʔυ

    ؚΊͯ·ͱΊ༧ఆͰ͢ • Chrome (Blink) ͷϨϯμϦϯάपΓʹ͍ͭͯɺͲ͔͜ͷษڧձͰLT͠ ͍ͨͳͱࢥ͍ͬͯ·͢