简体   繁体   中英

Unity Android: Can't build Firebase and Mapbox Project

I'm currently trying to build a Unity project for Android, which utilizes Mapbox , Firebase , Unity's in-built AR Foundation, and a bunch of smaller packages from the Asset Store, which cannot be the source of the problem. I built the project before adding Firebase successfully, but now I get the following error:

D8: Program type already present: com.google.gson.FieldNamingPolicy$5

(full version posted here pastebin because it's a lengthy one)

Version Numbers:
Unity 2019.3.0f6
Firebase Unity SDK 6.14.0
Mapbox Unity SDK 2.1.1

As far as I can tell, the problem is based on Mapbox and Firebase having dependency issues with each other. However, I have no clue how to fix them.
I tried every plausible solution I could find, including custom-grade templates, Gradle settings, trying internal builds on an older version of Unity (2018.3.14f1), exporting the project to Android Studio, then building from there and more.
None of this worked for me, sadly.

Are there any other methods I could try to fix this?

Thank you in advance!

PS: If I try to disable R8 as proposed in the error message, I get a java.io.IOException about a read error related to the "Duplicate jar entry [com/google/d/d$5.class]", but in general, no matter what fix I try, I usually end up with a different error message than with the other approaches.

I have an idea about what may be happening, but don't have an immediate way to test this solution.

It looks like there are two copies of the Google gson library included in your build. Inspecting mapbox-unity-sdk_v2.1.1 , I see that there is a file gson-2.8.5.jar included in the unity package. Since the build is apparently including gson as a side effect, you may be good just deleting this file. But I suspect that the support libraries that are also included may cause an issue.

Given this, I would suggest three avenues of remediation:

1) you may want to use the External Dependency Manager for Unity to resolve these files for you. This is included in the Firebase SDK (and is a completely open source project so you may want to request that MapBox use it in the future).

I would recommend reading this article to fully understand what's happening, but what you want to do is create a MapboxDependencies.xml file in an Editor folder (ex: Assets/Mapbox/Editor/MapboxDependencies.xml ) and include all the dependencies under Mapbox/Core/Plugins/Android :

<dependencies>
    <androidPackages>
        <androidPackage spec="com.google.code.gson:gson:2.8.6"/>
    </androidPackages>
</dependencies>

And delete the related jar/aar files when you're done.

Note that you will have to track down maven repositories for these, otherwise I would include a complete xml file for you.

Now libraries that may cause you issues are all the support libraries. It looks like Mapbox may still be using the old support libraries prior to Android Jetpack . There are mappings here , so I'll take a first stab at their Dependencies.xml entries for you:

<dependencies>
    <androidPackages>
        <androidPackage spec="com.google.code.gson:gson:2.8.6"/>
        <androidPackage spec="androidx.appcompat:appcompat:1.1.0"/>
        <androidPackage spec="androidx.legacy:legacy-support-core-ui:1.1.0"/>
        <androidPackage spec="androidx.legacy:legacy-support-core-utils:1.1.0"/>
        <androidPackage spec="androidx.media:media:1.1.0"/>
        <androidPackage spec="androidx.legacy:legacy-support-v4:1.1.0"/>
        <androidPackage spec="androidx.vectordrawable:vectordrawable:1.1.0"/>
    </androidPackages>
</dependencies>

The other problem you would typically run into is that under normal circumstances, you'd have to recompile all the libraries that relied on the old support libraries to reference the new androidx ones. Jetpack did come with a tool called Jetifier that should do this automatically if you enable it in the External Dependency Manager:

Jetifier 选项的屏幕截图

Although you may run into this issue on some configurations of Unity 2019.3 and later (workaround in the issue thread).

2) Alternatively you can add a mainTemplate.gradle to achieve the same goals. I would recommend using the ExternalDependencyManager, but by deleting the aar/jar libraries and adding them as dependencies in mainTemplate you'll effectively achieve the same outcome.

3) If you resolve your dependencies without mainTemplate.gradle - all of Firebase's aar/jar files will get added to Assets/Plugins/Android/Firebase . If you deleted all of the conflicts with Mapbox libraries, you may get to the same point. This feels a bit more dangerous to me and the support libraries may be harder to work with (you'd have to track down the androidx libraries on your own for instance) - but might remove some of the moving pieces if the dependency managers are new to you.

I hope that all helps!

--Patrick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM