appcompat:1.1.0 사용중인 경우, Android OS Ver. lollipop 에서 웹뷰 오류가 발생한다

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo ...
... Binary XML file ... webview

간단한 해결 법은 두 개정도.

1st. minSdk 상향 조정 : 21 -> 23

2st. lollipop 분기 처리

 - 단, 분기 처리는 하위 버전을 분기하여 관리하는 품이 하나 더 늘어나는 단점이 있다.

implementation 'androidx.appcompat:appcompat:1.1.0'
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebView;

public class LollipopFixedWebView extends WebView {

    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    // To fix Android Lollipop WebView problem create a new configuration on that Android version only
    private static Context getFixedContext(Context context) {
        if (Build.VERSION.SDK_INT == 21 || Build.VERSION.SDK_INT == 22) // Android Lollipop 5.0 & 5.1
            return context.createConfigurationContext(new Configuration());
        return context;
    }
}

1. Warning Msg:

'instrumentationRegistry' is deprecated. Deprecated in Java

'AndroidUnit4' is deprecated. Deprecated in Java


2. solve : 

1) build.gradle (module) 파일 > 

1.1) testImplementation 'junit:junit:4.12'  -> androidTestImplementation 'androidx.test.ext:junit:1.1.0' 대체

1.2) Gradle files have changed since last project sync. A project sync ... Sync Now 클릭

android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
}

dependencies {
...
// testImplementation 'junit:junit:4.12' 제거
androidTestImplementation 'androidx.test.ext:junit:1.1.0' // 추가 : junit:junit 대체
androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
}


2) ExampleInstrumentedTest.kt >

2.1) InstrumentationRegistry.getTargetContext() -> InstrumentationRegistry.getInstrumentation().targetContext 대체


2.2) import 변경

//import android.support.test.InstrumentationRegistry -> androidx.test.platform.app.InstrumentationRegistry 대체

//import android.support.test.runner.AndroidJUnit4 -> androidx.test.ext.junit.runners.AndroidJUnit4 대체


package com.example.com

//import android.support.test.InstrumentationRegistry // deprecated
//import android.support.test.runner.AndroidJUnit4 // deprecated
import androidx.test.ext.junit.runners.AndroidJUnit4 // 추가 : deprecated 대체
import androidx.test.platform.app.InstrumentationRegistry //추가 : deprecated 대체

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
// val appContext = InstrumentationRegistry.getTargetContext() // 제거
val appContext = InstrumentationRegistry.getInstrumentation().targetContext // 추가 : deprecated 대체
assertEquals("com.example.com", appContext.packageName)
}
}


+ Recent posts