gpt4 book ai didi

java - Android Multidex 运行时异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:48 29 4
gpt4 key购买 nike

我正在尝试解决运行具有超过 65k 方法的 Android 应用程序的问题。
我已经关注了有关 Multidex 支持的 Google 文档,但是我仍然无法成功运行它。似乎问题只出现在小于 21 的 SDK 上,因为当我指定 minSdkVersion 21 时,它运行良好,但是一旦我将它设置为 minSdkVersion 15,我就得到了以下异常。

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate application custom.package.name.MyApplication: java.lang.ClassNotFoundException: Didn't find class "custom.package.name.MyApplication" on path: DexPathList[[zip file "/data/app/custom.package.name-2/base.apk"],nativeLibraryDirectories=[/data/app/custom.package.name-2/lib/arm, /vendor/lib, /system/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:578)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4680)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: java.lang.ClassNotFoundException: Didn't find class "custom.package.name.MyApplication" on path: DexPathList[[zip file "/data/app/custom.package.name-2/base.apk"],nativeLibraryDirectories=[/data/app/custom.package.name-2/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:981)
at android.app.LoadedApk.makeApplication(LoadedApk.java:573)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4680) 
at android.app.ActivityThread.-wrap1(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Suppressed: java.io.IOException: Failed to open dex files from /data/app/custom.package.name-2/base.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:295)
at dalvik.system.DexFile.<init>(DexFile.java:80)
at dalvik.system.DexFile.<init>(DexFile.java:59)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:279)
at dalvik.system.DexPathList.makePathElements(DexPathList.java:248)
at dalvik.system.DexPathList.<init>(DexPathList.java:120)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:65)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:58)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:376)
at android.app.LoadedApk.makeApplication(LoadedApk.java:568)
... 9 more
Suppressed: java.lang.ClassNotFoundException: custom.package.name.MyApplication
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

致命异常前的 Logcat

W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/custom.package.name-2/base.apk --oat-file=/data/dalvik-cache/arm/data@app@custom.package.name-2-2@base.apk@classes.dex) because non-0 exit status
W/art: Failure to verify dex file '/data/app/ccustom.package.name-2-2/base.apk': Invalid method name: '_clinit@1433781298707#0'
W/System: ClassLoader referenced unknown path: /data/app/custom.package.name-2-2/lib/arm

这是我到目前为止所做的:

  • 创建了名为 MyApplication 的类,它扩展了 Application 并在 AndroidManifest.xml 文件中指定了它

  • 添加对 build.gradle 文件的依赖

    编译 'com.android.support:multidex:1.0.1'

  • 在 build.gradle 中启用 multidex

    multiDexEnabled true

  • 在 MyApplication 类中启用 multidex
    @覆盖
    protected void attachBaseContext(上下文基础){
    super.attachBaseContext(基础);
    MultiDex.install(这个);
    }

在此先感谢您的帮助!

最佳答案

试试这个,这对我有用,几乎是从当前项目中复制的....

build.gradle

android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "my.package.name"
minSdkVersion 16
targetSdkVersion 23
multiDexEnabled = true
versionCode 3
versionName "0.1.2"
renderscriptTargetApi 14
renderscriptSupportModeEnabled true
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:multidex:1.0.1'
}

应用类

public class MyApp extends MultiDexApplication {

@Override
public void onCreate() {
super.onCreate();
}
}

list

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

...

<application
android:name=".MyApp"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

...

值得注意的是,在我添加 READ 权限并扩展 MultiDexApplication 之前,我遇到了类似的问题

关于java - Android Multidex 运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34214755/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com