Fix AndroidJavaException: java.lang.ClassNotFoundException
caused by Minify in Unity
A guide on how to fix the issue of AndroidJavaException: java.lang.ClassNotFoundException
if you just made an Android build of your Unity project and Minify is enabled.
Table of Contens
Enabling Minify > Release
in Unity’s Publishing Settings helps optimize and reduce the size of your Android application by removing unused code. However, this optimization process can sometimes interfere with certain features.
How to figure out Minify is the culprit?
1.If you upload the build on one of Google Play’s testing track, it automatically generates a pre-launch report for you. Going through it shows the issues present, if any, in your app/game.
This is a hint that something’s wrong in your build due to stripping down of code by Minify.
2. To further pin-point the issue you need to connect your Android device and use logcat to get the logs. If you see logs similar to the below example, then Minify must have removed these classes from the build.
AndroidJavaException: java.lang.ClassNotFoundException: com.google.unity.ads.UnityInterstitialAdCallback
Note: It’s usually in the format,java.lang.ClassNotFoundException: com.example.missingclass
You can further refer to this official documentation for more info on the packages and the classes.
Setting up device and using logcat
As mentioned above, in order to know which classes are missing, you’ll have to connect your device and use logcat.
1. Enable Developer Options
- Open the “Settings” app on your Android device.
- Scroll down and tap on “About phone” or “About device.”
- Look for the “Build number” or “Build version” entry.
- Tap on the “Build number” entry seven times quickly. After a few taps, you should see a countdown indicating the remaining number of taps required.
- Once you’ve tapped seven times, you’ll see a message saying, “You are now a developer!”
2. Access Developer Options
- Go back to the main Settings screen.
- Scroll down and look for a new entry called “Developer options” or “Developer settings.”
- Tap on “Developer options” to open the developer settings menu.
3. Enable USB Debugging
- Inside the Developer options menu, scroll down and locate “USB debugging.”
- Tap on the toggle switch next to “USB debugging” to enable it.
- A warning message will appear explaining the potential risks of enabling USB debugging. Read the message and tap “OK” or “Enable” to proceed.
4. Connect your device to a computer
- Using a USB cable, connect your Android device to a computer.
- If prompted on your device to allow USB debugging, grant permission by tapping “Allow.”
5. Use Logcat
- Type the command to run logcat, just replace the path till the Unity editor version, in this case, replace till 2021.3.16f1
- Windows user, type in the command prompt
"C:\Program Files\Unity\Hub\Editor\2021.3.16f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platform-tools\adb.exe" logcat -s Unity ActivityManager PackageManager dalvikvm DEBUG
- Mac users, type in the terminal
/Applications/Unity/Hub/Editor/2021.3.16f1/PlaybackEngines/AndroidPlayer/SDK/platform-tools/adb logcat -s Unity ActivityManager PackageManager dalvikvm DEBUG
- Press Enter to start displaying the logcat logs.
6. Run your game/app
- Go through your game/app flow till the point where you’re not getting the expected result.
- You’ll see errors similar to this:
AndroidJavaException: java.lang.ClassNotFoundException: com.google.unity.ads.UnityInterstitialAdCallback
How to prevent stripping of the necessary code?
1.Enable custom Proguard file in Unity
Navigate to Project Settings > Player > Publish Settings
and enable the Custom Proguard File
option.
By using a custom Proguard file, we make sure the classes are always included in the build.
Project Settings > Player > Publish Settings >
Custom Proguard File
2. Modify the Proguard Configuration
Unity creates a new file called “proguard-user.txt” underAssets > Plugins > Android
Assets > Plugins > Android > proguard-user.txt
Open this newly created file and add lines in similar fashion for each classNotFoundException you see in the logs.
# Add lines in this below format for each classNotFoundExceptions
# in your logs from logcat.
-keep class com.google.android.gms.tasks.** { *; }
-keep
: Specifies that the class or classes defined by the rule should be preserved and not removed or obfuscated during the code optimization process.class com.google.android.gms.tasks.**
: Refers to all classes and sub-packages under thecom.google.android.gms.tasks
package. The double asterisks (**
) indicate that it includes all sub-packages and classes within the specified package.{ *; }
: Indicates that all methods and fields within the specified classes should be preserved and not removed or obfuscated.
In simpler terms, this ensures that all classes, methods, and fields under the com.google.android.gms.tasks
package are not removed or obfuscated during the code optimization process.
3. Force Resolve Dependencies
To ensure the proper resolution of dependencies, use the Android Resolver tool. Go toAssets > External Dependency Manager > Android Resolver > Force Resolve
Assets > External Dependency Manager > Android Resolver > Force Resolve
4. Build and test it on your Android device.
Keep monitoring the logcat. If everything goes right, you won’t see the errors anymore.