Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

MultiDex and ClassNotFound exception upon app start (crash)

Discussion in 'Android' started by Biggix, May 1, 2018.

  1. Biggix

    Biggix

    Joined:
    Dec 8, 2014
    Posts:
    44
    Hi guys,

    What could be the *correct* procedure of building for Android, using Gradle, Multidex and Unity 2017.3?

    My app builds and runs fine on iOS but I can't get it to run on Android no matter what I do.

    I've spent more than 2 days on this, without any positive results.

    Using Appodeal and Cross Native, I'm over the 65K Dex limit, so I have to use Multidex. So I've made my custom .gradle file which has the multidex attribute set to true.

    But I still get this (edited to save space -- some lines deleted but the idea is clear):

    Code (CSharp):
    1.  
    2.  
    3. E/Unity ( 6280): AndroidJavaException: java.lang.NoClassDefFoundError: com.voxelbusters.nativeplugins.features.cloudservices.serviceprovider.google.GooglePlayCloudService
    4. E/Unity ( 6280): java.lang.NoClassDefFoundError: com.voxelbusters.nativeplugins.features.cloudservices.serviceprovider.google.GooglePlayCloudService
    5.  
    6. AndroidJavaException: java.lang.ClassNotFoundException: com.voxelbusters.nativeplugins.features.gameservices.GameServicesHandler
    7.  
    8. Caused by: java.lang.ClassNotFoundException: Didn't find class "com.voxelbusters.nativeplugins.features.gameservices.GameServicesHandler" on path: DexPathList[[zip file "/data/app/com.bonjackpot.impasto-1/base.apk"],nativeLibraryDirectories=[/data/app/com.bonjackpot.impasto-1/lib/arm, /vendor/lib, /system/li
    9.  
    10. AndroidJavaException: java.lang.ClassNotFoundException: com.voxelbusters.nativeplugins.features.medialibrary.MediaLibraryHandler
    11.  
    12. Caused by: java.lang.ClassNotFoundException: Didn't find class "com.voxelbusters.nativeplugins.features.medialibrary.MediaLibraryHandler" on path: DexPathList[[zip file "/data/app/com.bonjackpot.impasto-1/base.apk"],nativeLibraryDirectories=[/data/app/com.bonjackpot.impasto-1/lib/arm, /vendor/lib, /system/li
    13.  
    14. E/Unity ( 6280): AndroidJavaException: java.lang.ClassNotFoundException: com.voxelbusters.nativeplugins.features.reachability.NetworkReachabilityHandler
    15. E/Unity ( 6280): java.lang.ClassNotFoundException: com.voxelbusters.nativeplugins.features.reachability.NetworkReachabilityHandler
    16.  
    17. E/Unity ( 6280): AndroidJavaException: java.lang.ClassNotFoundException: com.voxelbusters.nativeplugins.features.webview.WebViewHandler
    18. E/Unity ( 6280): java.lang.ClassNotFoundException: com.voxelbusters.nativeplugins.features.webview.WebViewHandler
    19.  
    20. Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/JsonObject;
    21. E/AndroidRuntime( 6280): at com.voxelbusters.nativeplugins.features.billing.core.datatypes.BillingProduct.getJsonObject(BillingProduct.java:20)
    22.  
    23.  
    There *should* be an option MultiDexKeepFile but it just doesn't do anything (it is seems to be ignored as there are no errors during the .apk assemly even if I delete the file or put some garbage there)

    I would like to ask an official Unity team member about the correct usage of MultiDexKeepFile with Unity and if that's not an option, how else my issue could be solved? Thanks!
     
  2. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    Sorry there's no official messaging on this. If you want to use multidex - you're on your own. (I know quite few occasions when it is working fine for our users though!)

    Something must be wrong with the configuration.
     
  3. Romcam

    Romcam

    Joined:
    Feb 1, 2018
    Posts:
    2
    Same problem for me. Biggix, have you found a solution?
     
  4. mahdi-malv

    mahdi-malv

    Joined:
    Jul 14, 2019
    Posts:
    4
    With the advance of Android system, the libraries are getting larger and method count is increasing overtime. This is a highly considerable issue. @Yuri-Habets I think this worthes thinking.


    Multidex causes methods to be separated into multiple packs.

    What I've seen is that if the class exists, but in another dex file other than first one, Unity can not find it and will throw ClassNotFoundException.

    Till now I have found no solution for this.
     
  5. fisherkat3

    fisherkat3

    Joined:
    Feb 25, 2020
    Posts:
    1
    For anyone who comes across this like I did - there is an answer! (I'm on Unity 2018.4.26f1)

    Assuming you have multidex set-up properly (android docs are great for this), your real problem is the multiDexKeepFile you specified is just not doing anything! (or multiDexKeepProguard!)

    Why? It's in the wrong directory!

    Chances are you put your multidex config in the directory with your maintemplate.gradle (default Assets/Plugins/Android/)

    Ah-ha! But! At build time Unity does a switcheroo on you and creates the build.gradle in Temp/gradleout/ directory, this is where the multiDexKeepFile needs to be to be found by gradle!

    Okay, but how do I fix it? This is created at build time after all!

    Unity added IPostGenerateGradleAndroidProject which you can hook into to copy your multiDexKeepFile over into the temp gradle directory! It provides the path where the build.gradle will be -- just copy your multidex config files to this path. (Note there was a bug with the path in certain Unity versions)

    Also note if you use debug & production builds, you'll want to add the mutliDexKeepFile file('multidex-config.txt') to both the debug and release sections

    Like so:

    Code (CSharp):
    1.     class GradleProjectFixup : UnityEditor.Android.IPostGenerateGradleAndroidProject
    2.     {
    3.         public int callbackOrder { get { return 0; } }
    4.  
    5.         public void OnPostGenerateGradleAndroidProject(string path)
    6.         {
    7.             string srcDir = "Assets/Editor";
    8.             string[] filesToCopy = { "multidex-config.txt", "multidex-config.pro" };
    9.  
    10.             // Copy files to gradle directory so they can be referenced by build.gradle
    11.             foreach (string filename in filesToCopy)
    12.             {
    13.                 string srcPath = Path.Combine(srcDir, filename).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
    14.                 string destPath = Path.Combine(path, filename).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
    15.                 Debug.LogFormat("Copying: {0} -> {1}", srcPath, destPath);
    16.                 File.Copy(srcPath, destPath, true);
    17.             }
    18.         }
    19.     }

    Resources:
    https://docs.unity3d.com/ScriptRefe...oject.OnPostGenerateGradleAndroidProject.html
    https://docs.unity3d.com/Manual/android-gradle-overview.html
    https://developer.android.com/studio/build/multidex
     
    Last edited: Nov 4, 2021