java.lang.ClassNotFoundException: Didn't find class “.MainActivity“on path: DexPathList react-native

2022-09-03 02:23:56

我正在使用react-native编写移动应用程序。即使我没有触及代码的任何本机方面,App 在运行后就开始停止。我在 react 原生组件中做了一些更改,但我没有使用任何新内容。我在Android工作室中调试了它,这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: tedu.edu.tr, PID: 23741
                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tedu.edu.tr/tedu.edu.tr.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "tedu.edu.tr.MainActivity" on path: DexPathList[[zip file "/data/app/tedu.edu.tr-Q-Z69MlFdgprlVraa6TSjg==/base.apk"],nativeLibraryDirectories=[/data/app/tedu.edu.tr-Q-Z69MlFdgprlVraa6TSjg==/lib/arm, /data/app/tedu.edu.tr-Q-Z69MlFdgprlVraa6TSjg==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1596)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6565)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                   Caused by: java.lang.ClassNotFoundException: Didn't find class "tedu.edu.tr.MainActivity" on path: DexPathList[[zip file "/data/app/tedu.edu.tr-Q-Z69MlFdgprlVraa6TSjg==/base.apk"],nativeLibraryDirectories=[/data/app/tedu.edu.tr-Q-Z69MlFdgprlVraa6TSjg==/lib/arm, /data/app/tedu.edu.tr-Q-Z69MlFdgprlVraa6TSjg==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
                      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
                      at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
                      at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2711)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1596) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6565) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

最糟糕的问题是每个人都不断建议清洁项目或启用multidex,这些都是由我完成的,但没有效果。

这是我的应用程序级别:build.gradle

android {
compileSdkVersion rootProject.ext.compileSdkVersion

defaultConfig {
    applicationId "tedu.edu.tr"
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode 15
    versionName "2.0.1"
    multiDexEnabled true
    ndk {
        abiFilters "armeabi-v7a", "x86"
    }
}

splits {
    abi {
        reset()
        enable enableSeparateBuildPerCPUArchitecture
        universalApk false  // If true, also generate a universal APK
        include "armeabi-v7a", "x86"
    }
}
buildTypes {
    release {
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), 
"proguard-rules.pro"
        // signingConfig signingConfigs.release
    }
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        // For each separate APK per architecture, set a unique version code as described here:
        // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
        def versionCodes = ["armeabi-v7a":1, "x86":2]
        def abi = output.getFilter(OutputFile.ABI)
        if (abi != null) {  // null for the universal-debug, universal-release variants
            output.versionCodeOverride =
                    versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
        }
    }
}
buildToolsVersion '27.0.3'
}

dependencies {
compile project(':react-native-file-viewer')
compile project(':rn-fetch-blob')
compile project(':react-native-vector-icons')
compile project(':react-native-randombytes')
compile project(':react-native-firebase')
implementation project(':react-native-firebase')
implementation project(':react-native-vector-icons')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+"  // From node_modules
implementation "com.google.android.gms:play-services-base:15.0.1"
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation "com.google.firebase:firebase-messaging:17.1.0"
implementation 'me.leolin:ShortcutBadger:1.1.21@aar' // <-- Add this line if you wish to use badge on Android
implementation 'com.android.support:multidex:1.0.3'
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
apply plugin: 'com.google.gms.google-services'

还有其他 build.gradle:

buildscript {
repositories {
    google()
    jcenter()
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.1.3'
    classpath 'com.google.gms:google-services:4.0.1'

    // NOTE: Do not place your application dependencies here; they 
belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    google()
    mavenLocal()
    jcenter()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
}
}

ext {
buildToolsVersion = "26.0.3"
minSdkVersion = 16
compileSdkVersion = 26
targetSdkVersion = 26
supportLibVersion = "26.1.0"
}

最后是我的文件(我添加了方法,以便我使用MultiDex):MainApplication.javaattachBaseContext()

package tedu.edu.tr;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.facebook.react.ReactApplication;
import com.vinzscam.reactnativefileviewer.RNFileViewerPackage;
import com.RNFetchBlob.RNFetchBlobPackage;
import com.bitgo.randombytes.RandomBytesPackage;
import io.invertase.firebase.RNFirebasePackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import io.invertase.firebase.messaging.RNFirebaseMessagingPackage;
import 
 io.invertase.firebase.notifications.RNFirebaseNotificationsPackage; 
  // <-- Add this line
 import java.util.Arrays;
 import java.util.List;

public class MainApplication extends Application implements 
    ReactApplication {

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(new MainReactPackage(), new RNFileViewerPackage(),
                new RNFetchBlobPackage(), new VectorIconsPackage(), new RandomBytesPackage(),
                new RNFirebasePackage(), new RNFirebaseMessagingPackage(), new RNFirebaseNotificationsPackage());
    }

    @Override
    protected String getJSMainModuleName() {
        return "index";
    }
};

@Override
public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
}

@Override
public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
}

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}
}

抱歉,如果这里有一个错误。由于与此斗争了2天,我筋疲力尽。编辑:我在可能的重复问题中尝试了所有这些。他们都没有帮助。这是我的AndroidManifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tedu.edu.tr">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />                                               
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />                                              
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:allowBackup="false"
  android:theme="@style/AppTheme">
  <meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_tedu" />
  <meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/bluelight" />
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
   android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
 <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
   <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
 <intent-filter>
  <action android:name="com.google.firebase.MESSAGING_EVENT" />
 </intent-filter>
 </service>
 <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
 <intent-filter>
  <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
 </intent-filter>
 </service>
 <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
</application>

</manifest>

答案 1

伙计们,我的一个项目的合作者删除了MainActivity中的包名称.java类。太可怕了。切勿删除包名称。再次添加它解决了它。


答案 2

您应该查看 .它似乎正在尝试运行一个名为 的类,但您的主类被称为 。重命名 时,还应在文件中进行重命名,我们声明需要启动的活动。AndroidManifest.xmlMainActivity.javaMainApplication.javaMainActivityAndroidManifest.xml

https://www.quora.com/When-developing-Android-applications-do-you-keep-the-main-activity-named-MainActivity-java-or-do-you-change-it-to-something-else


推荐