java.lang.ClassNotFoundException:在路径上找不到类“.MainActivity”:DexPathList react-native [重复]

Posted

技术标签:

【中文标题】java.lang.ClassNotFoundException:在路径上找不到类“.MainActivity”:DexPathList react-native [重复]【英文标题】:java.lang.ClassNotFoundException: Didn't find class ".MainActivity" on path: DexPathList react-native [duplicate] 【发布时间】:2019-02-19 00:48:15 【问题描述】:

我正在使用 react-native 编写移动应用程序。即使我没有触及代码的任何本机部分,应用程序在运行后就开始停止。我对 react native 组件做了一些更改,但我没有使用任何新的东西。我在 android Studio 中调试过,错误如下:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: tedu.edu.tr, PID: 23741
                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfotedu.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"

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

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>

【问题讨论】:

不,不幸的是我尝试了所有这些 您找到解决方案了吗?我遇到了完全相同的问题。如果我清理 android 缓存,它会第一次工作,但如果我再次尝试 react-native run-android,构建会失败。每次我想运行 android 时我都必须清理缓存 @hyuklee 是的,我用下面的答案解决了 【参考方案1】:

您应该查看AndroidManifest.xml。似乎它正在尝试运行一个名为MainActivity.java 的类,但您的主类名为MainApplication.java。重命名MainActivity时,也要在AndroidManifest.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

【讨论】:

事实并非如此。我正在使用反应原生框架。它创建了两个类,一个用于 MainApplication,一个用于 mainActivity。我将分享 androidManifest 文件【参考方案2】:

我项目的一位合作者删除了 MainActivity.java 类中的包名。太可怕了。永远不要删除包名。再次添加它解决了它。

【讨论】:

我不能给你足够的支持。 MainActivity.java 中删除了哪个包名? 类文件@Lucky_girl顶部写的包名 我的情况的绝对答案。谢谢 和我有类似的问题。我创建了一个名称的应用程序,后来更改了应用程序包名称但没有更改java文件夹下的文件夹名称(java/com/oldpackage/name)。到目前为止,我添加了几个包,但在添加 rn-fetch-blob 之前没有任何问题。有了这个答案的提示,我将 java dir 下的文件夹名称更改为 java/com/newpackage/name。现在工作正常。谢谢哥们:)。

以上是关于java.lang.ClassNotFoundException:在路径上找不到类“.MainActivity”:DexPathList react-native [重复]的主要内容,如果未能解决你的问题,请参考以下文章