googlesamples/unity-jar-resolver

[Bug] [Android] Kotlin error on Unity 2021.3.41f1

victor-kallai opened this issue · 15 comments

[REQUIRED] Please fill in the following fields:

  • Unity editor version: 2021.3.41f1
  • External Dependency Manager version: 1.2.182
  • Source you installed EDM4U: UMP
  • Features in External Dependency Manager in use: Android Resolver
  • Plugins SDK in use: Firebase 12.1.0, Admob, Facebook, GoogleSignIn, GoogleMobileAds
  • Platform you are using the Unity editor on: Windows

[REQUIRED] Please describe the issue here:

I'm raising this issue for better visibility. I know you are already aware of this issue from [here].(#699 (comment))

When building on Android with Unity 1.2.182 the build fails with ERROR:D8: com.android.tools.r8.kotlin.H.
The temporary fix is to change the minSdkVersion from 22 to 24, but that is not desirable.

I have in the project Firebase 12.1.0, Admob, Facebook, GoogleSignIn, GoogleMobileAds.

Please answer the following, if applicable:

What's the issue repro rate? 100%

What happened? How can we make the problem occur?
apk build fail with error:
ERROR:D8: com.android.tools.r8.kotlin.H

Thanks for bringing this up, @victor-kallai. Yeah, that's interesting, the AGP versions should be independent of the minSdkVersion, so the new changes shouldn't affect this, but looks like this isn't the case. We'll investigate this more and come back here once we have updates to share.

@argzdev Hello! I'm sure you guys working on your maximum, but is it there any status update?

Deadline for Google Play is August 31 to increase the target SDK to v34. We managed to build the project with 2021.3.42f1 and min SDK 24, but increasing min version is almost not possible in our case, due to big amount of users on that versions.

Sorry, we're still investigating this, @vladyslav-androshchuk.

While waiting for a fix, regarding the deadline for Google Play, you may request an extension as discussed in the Unity forum:

The deadline for targeting API 34 is August 31 - what if I don’t have enough time?

  • We have informed Google about the impact of this AGP issue to Unity Creators. You are allowed to request an extension until November using the usual Google Play Console / Policy pages 16. Usually this opens around the beginning of August. Please mention Unity 2021 and AGP/Legacy Library issues in your extension request.

Sorry, we're still investigating this, @vladyslav-androshchuk.

While waiting for a fix, regarding the deadline for Google Play, you may request an extension as discussed in the Unity forum:

The deadline for targeting API 34 is August 31 - what if I don’t have enough time?

  • We have informed Google about the impact of this AGP issue to Unity Creators. You are allowed to request an extension until November using the usual Google Play Console / Policy pages 16. Usually this opens around the beginning of August. Please mention Unity 2021 and AGP/Legacy Library issues in your extension request.

Thank you! We'll make request for all our apps.

I did a bit of testing, and I noticed something odd. Whenever I export the project, and open it in Android Studio, the issue goes away. With a little bit of tinkering around in the Unity Editor settings, I noticed that enabling Project Settings > Publishing Settings > Minify > Release/Debug somehow corrects this issue.

I believe the underlying cause could be that the r8 tool with this version of AGP might be faulty.
Let me know if this workaround helps!

I did a bit of testing, and I noticed something odd. Whenever I export the project, and open it in Android Studio, the issue goes away. With a little bit of tinkering around in the Unity Editor settings, I noticed that enabling Project Settings > Publishing Settings > Minify > Release/Debug somehow corrects this issue.

I believe the underlying cause could be that the r8 tool with this version of AGP might be faulty. Let me know if this workaround helps!

Just tried this and it worked! even keeping min api SDK to 22 :D

Thanks a lot for taking the time with this, really helped!

I did a bit of testing, and I noticed something odd. Whenever I export the project, and open it in Android Studio, the issue goes away. With a little bit of tinkering around in the Unity Editor settings, I noticed that enabling Project Settings > Publishing Settings > Minify > Release/Debug somehow corrects this issue.

I believe the underlying cause could be that the r8 tool with this version of AGP might be faulty. Let me know if this workaround helps!

Unfortunately, this work around does not work for us. Our binary builds and runs but only shows a black screen after the initial splash screen.

Hi @DevSean, could you share your stacktrace so we can investigate this further? Also any chance you could share an MCVE, it'll help us out a lot investigating this issue.

Hi @argzdev, we don’t actually use Unity-jar-resolver internally, we export to Android studio and then build from there (so no MCVE).

We are seeing the same r8 error:
ERROR:D8: com.android.tools.r8.kotlin.H

Additionally, changing the minimum sdk version does work (certainly not an ideal solution).

As you mentioned, I believe this is the actual issue:

I believe the underlying cause could be that the r8 tool with this version of AGP might be faulty.

As for the minify errors, the game is (unsurprisingly) unable to find specific classes as they have been stripped.

My company has opened a premium support ticket with Unity regarding this issue, if there are any updates I’ll happily share them here.

Thanks for the additional information, @DevSean. From my testing, the ERROR:D8: com.android.tools.r8.kotlin.H is only present when using AGP 7.4.2 and Gradle 7.5.1 (This is the version that is tied up with the Unity Editor 2021 and 2022). Since you've exported your project, you could try using the Tools > AGP Upgrade Assistant from the menu section of Android Studio, which will update your AGP and Gradle to version 8+ onwards. I believe that should resolve your issue without the need to enable minify.

@argzdev Thanks for the suggestion, I had considered manually increasing the template versions to see if the issue gets resolved, previously we were able to build targeting API 34 with an older Unity version and an older Gradle version.

For obvious reasons we would prefer to match the versions provided by Unity if possible.

Either way, I hope this all gets resolved soon. Thank you for your investigations and findings also.

Hi @argzdev, we don’t actually use Unity-jar-resolver internally, we export to Android studio and then build from there (so no MCVE).

We are seeing the same r8 error: ERROR:D8: com.android.tools.r8.kotlin.H

Additionally, changing the minimum sdk version does work (certainly not an ideal solution).

As you mentioned, I believe this is the actual issue:

I believe the underlying cause could be that the r8 tool with this version of AGP might be faulty.

As for the minify errors, the game is (unsurprisingly) unable to find specific classes as they have been stripped.

My company has opened a premium support ticket with Unity regarding this issue, if there are any updates I’ll happily share them here.

After I enabled minify debug/release we also had runtime errors for missing classes.

I didn't mention it in my previous post but you probably need to add a custom proguard file to prevent those classes from being stripped.

Project settings -> Player (Android) -> Custom Proguard File

And depending on what classes are missing you will have to add them. For me it was regarding google ads and ump so I added :


-keep public class com.google.android.ump.**{
   public *;
}

-keep class com.google.unity.** {
   *;
}

-keep public class com.google.android.gms.ads.**{
   public *;
}

-keep public class com.google.ads.**{
   public *;
}

You might be already aware of this and your error might be something else, but just incase someone else finds this

@argzdev Thank you for the fix, enabling the Project Settings > Publishing Settings > Minify > Release works.

Setting the minSdkVersion to 22 is still not possible due to Firebase 12.2.0 requiring at least version 23, here is the known issue.

But I'm happy that it works with minSdkVersion 23.

To not have any classes stripped by proguard I set this in the custom proguard-users.txt as a quick fix.

-dontobfuscate
-dontshrink
-dontoptimize

# Keep all classes
-keep class ** { *; }

Awesome, glad that works @victor-kallai. Thanks for sharing your update and extra details for the fix!

Since the r8 tooling is embedded in this version of AGP, we can only rely on this workaround until the AGP and Gradle version is raised to version 8+, which isn't anytime soon. That said, there's not much we can do on the unity-jar-resolver side.

I'll go ahead and close this thread for now. If anyone has better mitigations, feel free to share! Thanks!

In case it helps someone, this is another workaround that can be tried:

  1. Create a custom baseProjectTemplate.gradle
    • Player Settings > Publishing > Create Custom Base Gradle Template
  2. Open newly created Assets/Plugins/Android/baseProjectTemplate.gradle.
  3. Force use of a newer r8 version
buildscript {
    repositories {
        maven {
            url = uri("https://storage.googleapis.com/r8-releases/raw")
        }
    }
    dependencies {
        classpath("com.android.tools:r8:8.3.37")
    }
}

source: https://stackoverflow.com/a/78795333