Activity class 未发现异常

Activity class not found exception

我正尝试在 Android 18 的旧设备上 运行 一个 android 项目。该项目在 Android >21 上运行良好,但在 Android 18 我收到以下错误:

03-11 01:22:31.147 2343-2608/? E/Watchdog: !@Sync 466 03-11 01:23:01.152 2343-2608/? E/Watchdog: !@Sync 467 03-11 01:23:16.602 24016-24016/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.MainActivity" on path: /data/app/com.example-1.apk at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) at android.app.ActivityThread.access0(ActivityThread.java:159) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.MainActivity" on path: /data/app/com.example-1.apk at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64) at java.lang.ClassLoader.loadClass(ClassLoader.java:501) at java.lang.ClassLoader.loadClass(ClassLoader.java:461) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)  at android.app.ActivityThread.access0(ActivityThread.java:159)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:176)  at android.app.ActivityThread.main(ActivityThread.java:5419)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:525)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)  at dalvik.system.NativeStart.main(Native Method)  03-11 01:23:17.052 2343-2441/? E/android.os.Debug: !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error 03-11 01:23:23.087 2698-2698/? E/Launcher: Error finding setting, default accessibility to not found: accessibility_enabled 03-11 01:23:23.147 2343-2441/? E/ViewRootImpl: sendUserActionEvent() mView == null 03-11 01:23:23.162 2343-4810/? E/EnterpriseContainerManager: ContainerPolicy Service is not yet ready!!! 03-11 01:23:23.377 24110-24132/? E/Babel: canonicalizeMccMnc: invalid mccmnc 03-11 01:23:23.377 24110-24132/? E/Babel: canonicalizeMccMnc: invalid mccmnc nullnull 03-11 01:23:31.152 2343-2608/? E/Watchdog: !@Sync 468

我的 Android 清单如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

为什么找不到主class?我怎样才能使应用与 Android 18 兼容?

编辑 1: 我尝试在 MainActivity 中添加 multidex 功能:

public class MainActivity extends AppCompatActivity implements IResults {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
...

在gradle中:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 24
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        // Enabling multidex support.
        multiDexEnabled true
    }
...

dependencies {
    // Generic dependencies
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'

    // Support libraries
    compile "com.android.support:appcompat-v7:$supportLibraryVersion"
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
    compile 'com.android.support:multidex:1.0.1'
...

但还是没有运气。还有其他解决方法吗?

你必须使用MultidexApplication

<application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

或将 MultiDex.install(this); 放入您的 Application class