googlesamples / unity-jar-resolver

Unity plugin which resolves Android & iOS dependencies and performs version management
Other
1.24k stars 339 forks source link

[Bug][Android] Build failure after 2021.3.41f1 (AGP updated from 4.2.2 to 7.4.2) #699

Closed manugildev closed 1 month ago

manugildev commented 1 month ago

[READ] For Firebase Unity SDK issues, please report to Firebase Unity Sample

Once you've read this section and determined that your issue is appropriate for this repository, please delete this section.

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the issue here:

Please answer the following, if applicable:

What's the issue repro rate? 100%

What happened? How can we make the problem occur? .apk doesn't build because of gradle failures.

FAILURE: Build completed with 3 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':launcher:checkReleaseDuplicateClasses'.
> Could not resolve all files for configuration ':launcher:releaseRuntimeClasspath'.
   > Could not find com.google.firebase:firebase-analytics-unity:12.1.0.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/com/google/firebase/firebase-analytics-unity/12.1.0/firebase-analytics-unity-12.1.0.pom
       - https://repo.maven.apache.org/maven2/com/google/firebase/firebase-analytics-unity/12.1.0/firebase-analytics-unity-12.1.0.pom
       - file:/Users/manuel.gil/Downloads/Unity/firebase/New Unity Project/Library/Bee/Android/Prj/Mono2x/Gradle/unityLibrary/libs/firebase-analytics-unity-12.1.0.jar
       - file:/Users/manuel.gil/Downloads/Unity/firebase/New Unity Project/Library/Bee/Android/Prj/Mono2x/Gradle/unityLibrary/libs/firebase-analytics-unity.jar
     Required by:
         project :launcher > project :unityLibrary
   > Could not find com.google.firebase:firebase-app-unity:12.1.0.
     Searched in the following locations:
       - https://dl.google.com/dl/android/maven2/com/google/firebase/firebase-app-unity/12.1.0/firebase-app-unity-12.1.0.pom
       - https://repo.maven.apache.org/maven2/com/google/firebase/firebase-app-unity/12.1.0/firebase-app-unity-12.1.0.pom
       - file:/Users/manuel.gil/Downloads/Unity/firebase/New Unity Project/Library/Bee/Android/Prj/Mono2x/Gradle/unityLibrary/libs/firebase-app-unity-12.1.0.jar
       - file:/Users/manuel.gil/Downloads/Unity/firebase/New Unity Project/Library/Bee/Android/Prj/Mono2x/Gradle/unityLibrary/libs/firebase-app-unity.jar
     Required by:
         project :launcher > project :unityLibrary
     ...

Unity updated Gradle from 6.7.1 to 7.5.1 and AGP from 4.2.2 to 7.1.2 in 2021.3.41f1. This is the root cause of the issue. For Gradle 7+ the plugin repositories need to be added to settingsTemplate.gradle, not to mainTemplate.gradle.

This condition in GradleTemplateResolver.cs needs to be updated to also include versions >= than 2021.3.41f1.

https://github.com/googlesamples/unity-jar-resolver/blob/8e4659acea42f83d33d34e31e48e4fdbcaa91ea0/source/AndroidResolver/src/GradleTemplateResolver.cs#L166

An even better way of handling it would be to base that condition in the AGP version, instead of Unity version, and make it return true when AGP >= 7.0.0.

More Info

→ See initial report on the Unity forums. → Reprodution project: failing_edm4u_2021_3_41f1.zip

manugildev commented 1 month ago

Unity 2021.3.41f1 doesn't have the ability to add a custom settingsTemplate.gradle from the Player > Publishing UI, however adding a custom settingsTemplate.gradle to Assets/Plugins/Android will be correctly picked up and used during build.

Unity 2021.3.42f1 will come with the ability of creating a Custom Settings Template from the UI, same as in 2022.3 and Unity-6.

Default settingsTemplate.gradle lives in:

<EDITOR_LOCATION>/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates/settingsTemplate.gradle

example:

/Applications/Unity/Hub/Editor/2021.3.41f1/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates/settingsTemplate.gradle

For reference, this is the default contents of settingsTemplate.gradle:

pluginManagement {
    repositories {
        **ARTIFACTORYREPOSITORY**
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

include ':launcher', ':unityLibrary'
**INCLUDES**

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        **ARTIFACTORYREPOSITORY**
        google()
        mavenCentral()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}
manugildev commented 1 month ago

Also, users are experiencing this error:

ERROR:D8: com.android.tools.r8.kotlin.H

It seems to be caused by an old Kotlin version being used by some plugins, such as Firebase or Google Ads. Although setting the minSdkVersion from 22 to 24 solves the issue, this solution is not desirable for most users.

argzdev commented 1 month ago

Thanks for reporting, @manugildev. I was able to reproduce the issue. Let me reach out to our engineers and see what we can do here.

manugildev commented 1 month ago

@argzdev Thanks!

As mentioned before, the most complete solution would be to modify the UnityChangeMavenRepoInSettingsTemplate method in GradleTemplateResolver.cs: https://github.com/googlesamples/unity-jar-resolver/blob/8e4659acea42f83d33d34e31e48e4fdbcaa91ea0/source/AndroidResolver/src/GradleTemplateResolver.cs#L163-L168

and instead use AndroidGradlePluginVersion when AGP version is >= 7.0.0:

https://github.com/googlesamples/unity-jar-resolver/blob/8e4659acea42f83d33d34e31e48e4fdbcaa91ea0/source/AndroidResolver/src/PlayServicesResolver.cs#L600-L604

Another less elegant and more bug-prone solution would be something like:

(GetUnityVersionMajorMinor() >= 2021.3.41f1 && GetUnityVersionMajorMinor() < 2022.0) || GetUnityVersionMajorMinor() >= 2022.2f;
argzdev commented 1 month ago

Thank you for this suggestion, we really appreciate every contribution! I'll create a pull request with your changes, and get some feedback from our engineers.

vladyslav-androshchuk commented 1 month ago

Hello!

We have the same problem in preparing for Google own requirements. I suppose we are waiting this to be resolved on the next week?

I really appreciate your time. We have 10+ projects and loaded development pipeline, with less than 1 month till deadline we have less and less time to finish.

If we can help in any way - let me know!

a-maurice commented 1 month ago

Thanks for raising this issue, and helping track down a sensible solution for it. I just pushed out a new release that checks against the AGP version instead of Unity version, https://github.com/googlesamples/unity-jar-resolver/releases/tag/v1.2.182

As for the issue with ** DIR_UNITYPROJECT** not being replaced correctly in 2021.3.41f1, unfortunately I couldn't come up with a quick enough solution that didn't indirectly cause different issues without delaying this fix till next week. However, a couple of workarounds do exist:

  1. In your Unity project, in the Android Resolver settings (Assets > External Dependency Manager > Android Resolver > Settings), there is an option "Use Full Custom Local Maven Repo Path", "When building Android app through Unity". Checking that on will make it just use the current path of the Unity project, instead of relying on DIR_UNITYPROJECT. There are potentially some issues with that, depending on your build infrastructure, but that is the most straightforward workaround.

    image
  2. When this issue came up before, a user put together a script that should fix it. https://github.com/googlesamples/unity-jar-resolver/issues/478#issuecomment-980615877 I would've liked to include a script like this with the release, but because of how EDM4U ships (prebuilt binaries) adding a direct dependency on UnityEditor.Android can cause issues. But it is easy enough to copy that logic into a new file in your project, and it should be good to go.

Hopefully this will be fixed with the next 2021 release, and again, thanks for the assistance in identifying this issue.

manugildev commented 1 month ago

@a-maurice

As for the issue with ** DIR_UNITYPROJECT** not being replaced correctly in 2021.3.41f1, unfortunately I couldn't come up with a quick enough solution that didn't indirectly cause different issues without delaying this fix till next week. However, a couple of workarounds do exist

Unity 2021.3.42f1 (unityhub://2021.3.42f1) has now been released. This version solves the ** DIR_UNITYPROJECT** not being replaced issue.