我在 Android 中面临 ClassNotFoundException
Posted
技术标签:
【中文标题】我在 Android 中面临 ClassNotFoundException【英文标题】:I am facing ClassNotFoundException in Android 【发布时间】:2021-05-16 19:15:57 【问题描述】:我正在尝试创建一个使用 Firebase 的聊天应用。但是突然间我遇到了这个错误。
02-13 10:23:02.533446 733 733 E androidRuntime: FATAL EXCEPTION: main
02-13 10:23:02.533446 733 733 E AndroidRuntime: Process: com.mycompany.myapp3, PID: 733
02-13 10:23:02.533446 733 733 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate application com.mycompany.myapp3.MyApplication: java.lang.ClassNotFoundException: Didn't find class "com.mycompany.myapp3.MyApplication" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp3-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.myapp3-1/lib/arm, /system/lib, /vendor/lib, /system/vendor/lib]]
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.app.LoadedApk.makeApplication(LoadedApk.java:807)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5477)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.app.ActivityThread.-wrap2(ActivityThread.java)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.os.Looper.loop(Looper.java:203)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6251)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
02-13 10:23:02.533446 733 733 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mycompany.myapp3.MyApplication" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp3-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.myapp3-1/lib/arm, /system/lib, /vendor/lib, /system/vendor/lib]]
02-13 10:23:02.533446 733 733 E AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.app.Instrumentation.newApplication(Instrumentation.java:992)
02-13 10:23:02.533446 733 733 E AndroidRuntime: at android.app.LoadedApk.makeApplication(LoadedApk.java:801)
02-13 10:23:02.533446 733 733 E AndroidRuntime: ... 9 more
build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android
compileSdkVersion 27
defaultConfig
applicationId "com.mycompany.myapp3"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildTypes
debugg
debuggable true
dependencies
implementation 'com.google.android.gms:play-services-analytics:15+'
implementation 'com.google.firebase:firebase-storage:15+'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.google.firebase:firebase-auth:15+'
implementation 'com.google.firebase:firebase-database:15+'
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp3" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".StartActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegisterActivity" />
<activity
android:name=".ResetPasswordActivity" />
<activity
android:name=".LoginActivity" />
<activity
android:name=".MainActivity" />
<activity
android:name="ChatActivity" />
<activity
android:name=".UserProfileActivity" />
<!--<activity
android:name=".ImageActivity" />-->
<activity
android:name=".DebugActivity"
android:screenOrientation="portrait"/>
<meta-data
android:name="android.max_aspect"
android:value="4.0"/>
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
</manifest>
我不知道该如何解决。如果你能帮忙,我就当学徒。
注意:拜托,我是这个领域的初学者。我真的需要帮助。如果我错过了分享任何信息,请告诉我,我会添加信息。
谢谢。
【问题讨论】:
试试这个答案,它有类似的问题***.com/a/45320244/9454904 也许 proguard 正在制造这个问题。试试***.com/questions/15476315/… 【参考方案1】:您似乎还没有在AndroidManifest
文件中注册您的应用程序类。
像这样注册
<application
android:name=".MyApplication">
【讨论】:
【参考方案2】:对于 android studio 4+,您需要在 build.gradle (app) 中像这样启用 Java 8:
android
defaultConfig
...
compileOptions
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
【讨论】:
以上是关于我在 Android 中面临 ClassNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章
Ionic Cordova Build Android 期间面临错误