java.lang.NoClassDefFoundError: Failed resolution

2019-08-19 05:13发布

问题:

I am going to enter a phone number, but when I type in the number, the app crash immediately. The following error occurred

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.xbdl.dmtoilet, PID: 3155
              java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/view/KeyEventCompat;
                  at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:526)
                  at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
                  at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319)
                  at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
                  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
                  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
                  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3434)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.view.KeyEventCompat" on path: DexPathList[[zip file "/data/app/com.xbdl.dmtoilet-1/base.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.xbdl.dmtoilet-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.xbdl.dmtoilet-1/lib/x86, /vendor/lib, /system/lib]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                  at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:526) 
                  at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59) 
                  at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319) 
                  at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310) 
                  at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127) 
                  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089) 
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642) 
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695) 
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661) 
                  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787) 
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669) 
                  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844) 
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642) 
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695) 
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661) 
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669) 
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642) 
                  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922) 
                  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896) 
                  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857) 
                  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3434) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                Suppressed: java.lang.ClassNotFoundException: android.support.v4.view.KeyEventCompat
                  at java.lang.Class.classForName(Native Method)
                  at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                  at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                        ... 28 more
               Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
I/Process: Sending signal. PID: 3155 SIG: 9

But when I remove the editText input type, I can type the characters normally , But when I enter numbers and backspace the key will still cause the app to flick back

The configuration information as follow

config = [
        buildToolsVersion: "26.0.2",
        compileVersion   : 26,
        minSdkVersion    : 21,
        targetVersion    : 26,
        versionCode      : 1,
        versionName      : "1.0",
        packageName      : "com.xbdl.dmtoilet",
]

lib = [
        support_version : "27.0.0",
]

EditText code as follow

 <EditText
        android:id="@+id/phoneEditText"
        style="@style/style_et.login"
        android:layout_height="63dp"
        android:drawableStart="@mipmap/ic_phone"
        android:hint="@string/text_hint_input_phone"
        android:inputType="phone"
        android:paddingStart="16dp"
        app:layout_constraintBottom_toTopOf="@+id/pwdLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="" />

<style name="style_et">
    <item name="android:layout_height">@dimen/dimen_height_button</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:theme">@style/MyEditText</item>
    <item name="android:textSize">@dimen/dimen_font_text_small_2</item>
    <item name="android:drawablePadding">@dimen/dimen_distance_min</item>
</style>

<style name="style_et.login">
    <item name="android:background">@null</item>
</style>

<style name="MyEditText" parent="Theme.AppCompat.Light">
    <item name="colorControlNormal">#EDEDEE</item>
    <item name="colorControlActivated">#009AF7</item>
</style>

I created a new activity, using the above layout, I can enter numbers, but the backspace key to enter the app crash . This is all the code

class TestActivity : AppCompatActivity() {

  companion object {
      fun doAction(mContext: Activity) {
          val intent = Intent(mContext, TestActivity::class.java)
          mContext.startActivity(intent)
      }
  }

  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_login)
  }
}

complete layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/outSizeView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".ui.user.LoginActivity">

<ImageView
    android:id="@+id/logoView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:contentDescription="@null"
    app:layout_constraintBottom_toTopOf="@+id/inputView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ico_logo" />

<RelativeLayout
    android:id="@+id/bottomAction"
    android:layout_width="match_parent"
    android:layout_height="27dp"
    android:layout_marginBottom="33dp"
    android:layout_marginEnd="33dp"
    android:layout_marginStart="33dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="500dp">

    <TextView
        android:id="@+id/signUpTextView"
        style="@style/style_text_action"
        android:layout_alignParentEnd="true"
        android:text="@string/text_action_register"
        android:textColor="@color/colorMain" />

    <TextView
        android:id="@+id/forgotPwdTextView"
        style="@style/style_text_action"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="0dp"
        android:text="@string/text_action_forget_password" />

</RelativeLayout>

<Button
    android:id="@+id/signInButton"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_marginBottom="130dp"
    android:layout_marginEnd="50dp"
    android:layout_marginStart="50dp"
    android:background="@drawable/button_login_selector"
    android:text="@string/text_action_sign_in"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/bottomAction"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<LinearLayout
    android:id="@+id/inputView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="43dp"
    android:layout_marginEnd="33dp"
    android:layout_marginStart="33dp"
    android:background="@drawable/bg_gray"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/signInButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <EditText
        android:id="@+id/phoneEditText"
        style="@style/style_et.login"
        android:layout_height="63dp"
        android:drawableStart="@mipmap/ic_phone"
        android:hint="@string/text_hint_input_phone"
        android:inputType="phone"
        android:paddingStart="16dp"
        app:layout_constraintBottom_toTopOf="@+id/pwdLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="" />

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@android:color/white" />

    <RelativeLayout
        android:id="@+id/pwdLayout"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        app:layout_constraintBottom_toTopOf="@+id/signInButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <EditText
            android:layout_height="match_parent"
            android:id="@+id/passwordEditText"
            style="@style/style_et.login"
            android:layout_centerVertical="true"
            android:drawablePadding="8dp"
            android:drawableStart="@mipmap/ic_password"
            android:hint="@string/text_hint_input_pwd"
            android:inputType="textPassword"
            android:paddingStart="16dp"
            tools:text="" />

        <ImageView
            android:id="@+id/hidePwdButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp"
            android:contentDescription="@null"
            android:paddingBottom="@dimen/dimen_corner"
            app:srcCompat="@mipmap/ic_eye_close" />


        <ImageView
            android:id="@+id/clearPwdButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="12dp"
            android:layout_toStartOf="@id/hidePwdButton"
            android:contentDescription="@null"
            android:paddingBottom="@dimen/dimen_corner"
            app:srcCompat="@mipmap/ic_clear" />

    </RelativeLayout>
</LinearLayout>

回答1:

I've found the reason, because when support_version is 27, the KeyEventCompat class is removed in support-v4 . We can see that in the dispatchKeyEvent method of AppCompatActivity as follow

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (KeyEventCompat.isCtrlPressed(event) &&
            event.getUnicodeChar(event.getMetaState() & ~KeyEvent.META_CTRL_MASK) == '<') {
        // Capture the Control-< and send focus to the ActionBar
        final int action = event.getAction();
        if (action == KeyEvent.ACTION_DOWN) {
            final ActionBar actionBar = getSupportActionBar();
            if (actionBar != null && actionBar.isShowing() && actionBar.requestFocus()) {
                mEatKeyUpEvent = true;
                return true;
            }
        } else if (action == KeyEvent.ACTION_UP && mEatKeyUpEvent) {
            mEatKeyUpEvent = false;
            return true;
        }
    }
    return super.dispatchKeyEvent(event);
}

Executed to KeyEventCompat.isCtrlPressed (event), because can not find the class, so cause the app to crash

The solution is as follows:

  • Create a package called android.support.v4.view in your code
  • Copy the class KeyEventCompat and KeyEventCompatHoneycomb to this package
  • Rebuild project