![](/img/trans.png)
[英]Unable to instantiate application java.lang.ClassNotFoundException
[英]Unable to instantiate application: java.lang.ClassNotFoundException:
在以下情況下會出現此問題:
我從 Android Studio 運行應用程序,應用程序運行正常
然后我刪除了應用程序並嘗試從.../app/build/outputs/apk/debug.apk
我的操作系統是 Ubuntu 16.04,oracle jdk
但在 Windows 7 上一切正常(apk 可以從.../app/build/outputs/apk/debug.apk
並正常運行)
出現此錯誤:
FATAL EXCEPTION: main
Process: com.example, PID: 21084
java.lang.RuntimeException: Unable to instantiate application com.example.MyApp: java.lang.ClassNotFoundException: Didn't find class "com.example.MyApp" on path: DexPathList[[zip file "/data/app/com.example-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example-1/lib/arm, /vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:676)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289)
at android.app.ActivityThread.access$1800(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.MyApp" on path: DexPathList[[zip file "/data/app/com.example-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example-1/lib/arm, /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.app.Instrumentation.newApplication(Instrumentation.java:1004)
at android.app.LoadedApk.makeApplication(LoadedApk.java:666)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6289)
at android.app.ActivityThread.access$1800(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1860)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Suppressed: java.lang.ClassNotFoundException: com.example.MyApp
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)
... 12 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
我的畢業證:
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'io.fabric'
apply plugin: 'android-apt'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
jackOptions {
enabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dexOptions {
javaMaxHeapSize "8g"
jumboMode = true
preDexLibraries = false
}
signingConfigs {
//release
release {
storeFile file("/keys/apk_key.jks")
storePassword "apk_key"
keyAlias "apk_key"
keyPassword "apk_key"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
debuggable false
}
debug {
signingConfig signingConfigs.release
debuggable true
}
}
dexOptions {
javaMaxHeapSize "3G"
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'mockito-extensions/org.mockito.plugins.MockMaker'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
...
}
我也試圖:
Invalidate Cache/Restart
Clean Project
Rebuild project
謝謝@Dileep Patel 我遇到了同樣的問題。 應用程序僅在從 Android Studio 安裝時才有效,如果通過 adb 命令安裝則崩潰。 使緩存無效/重新啟動並禁用即時運行(Android Studio->首選項->即時運行,取消選中“啟用實例運行”)在我的情況下有效。
在應用級gradle中添加multidex
compile 'com.android.support:multidex:1.0.1'
啟用多重索引
defaultConfig {
multiDexEnabled true
}
最后在應用程序類中添加此代碼
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
我只想說,我知道這真的很愚蠢,我知道問題已經得到解答,但我更改了 App 類的名稱並忘記在清單中更改它,出於某種原因,該應用程序仍在構建中。 無論如何,請確保您的項目清單中有正確的應用程序類名稱:
<application
android:name=".CorrectAppClassHere"
//...
將此添加到 gradle 幫助了我
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
如果你有API級別20以上的測試設備,你可以把你的minSdkVersion改成21,而不是加multidex,因為API級別20及以上都有multidex
<application
android:name=".App"
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">
在您的清單文件中,只需將allowBackup
更改為false 。
然后運行應用程序,你會得到一個異常。
然后將其改回true ,這完全修復了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.