Android Multidex RuntimeException
Asked Answered
K

2

7

I am trying to solve a problem with running Android app that has over 65k methods.
I have followed Google docs about Multidex support, however I'm still unable to run it successfully. It seems that the problem appears only on SDK less than 21, because when I specify minSdkVersion 21, it works well, however once I set it to minSdkVersion 15, I am getting following exception.

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 before fatal exception

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@[email protected]@[email protected]) 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

Here is what I've done so far:

  • created class called MyApplication that extends Application and specified it in AndroidManifest.xml file

  • added dependency to build.gradle file

    compile 'com.android.support:multidex:1.0.1'

  • enabled multidex in build.gradle

    multiDexEnabled true

  • enabled multidex in MyApplication class @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }

Thanks in advance for any help!

Kapok answered 11/12, 2015 at 1:27 Comment(2)
have you tried using extends MultiDexApplication, instead of extends Application?Octo
Yes, I have tried, but the still same issueKapok
E
2

Problem is because of this obfuscator:

    <dependency>
        <groupId>net.java.truelicense</groupId>
        <artifactId>truelicense-obfuscate</artifactId>
        <version>${truelicense.obfuscate.version}</version>
    </dependency>
Ermine answered 16/11, 2016 at 18:24 Comment(2)
Exactly, this is that lib. ThanksKapok
In which file can we add this code. It would be helpful in can you can help @Asprelis.Montfort
O
5

Try this, this is working for me, pretty much copied from a current project....

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'
}

Application Class

public class MyApp extends MultiDexApplication {

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

Manifest

<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" >

...

It is worth noting that I had a similar issue until i added the READ permission and extends MultiDexApplication

Octo answered 11/12, 2015 at 1:49 Comment(8)
Thanks for response! Well, I have already tried to use MultiDexAppllication as well, but the same issue. Note: i am using read_external_storage as well :/Kapok
@Creck are you also using WRITE_EXTERNAL permission? it may be that the min version numbers don't match upOcto
Yes I do. But from exception it seems that it has nothing to do with external storage at all, as apk is stored in /data/app/...Kapok
@Creck yea just asking because i did have the same exact issue, with same exact exception, due to that reason. It may also be that you need to clean the generated build files from the project and rebuild. does it say anything useful in the stacktrace just before the fatal exception?Octo
I have tried to rebuild, but no success. Also, I have update my question with log from logcat before exceptionKapok
@Creck ok i have tried to recreate the issue, using pieces of your code that were different from mine and it still worked for me, only thing i can really suggest from here is to make sure that the multidexEndabled is true in the correct build configs (that its actually true when built) and if so manually deleting all the generated files, aside from that im not seeing anything else thats different between our two code bases, out of curiosity what sdk version is the device your testing on?Octo
I am testing it on Nexus 5 with Android M installed (sdk 23). I will try to reduce number of methods as much as possible. Anyway thanks for tipsKapok
could you please zip that "test" project, upload it somewhere and send me a link ? I would like to compile it on my PC whether its something wrong with AS or ... thanks!Kapok
E
2

Problem is because of this obfuscator:

    <dependency>
        <groupId>net.java.truelicense</groupId>
        <artifactId>truelicense-obfuscate</artifactId>
        <version>${truelicense.obfuscate.version}</version>
    </dependency>
Ermine answered 16/11, 2016 at 18:24 Comment(2)
Exactly, this is that lib. ThanksKapok
In which file can we add this code. It would be helpful in can you can help @Asprelis.Montfort

© 2022 - 2024 — McMap. All rights reserved.