Facebook-sdk-for-unity: Android - Crash on startup caused by incompatibilities in dependencies

Created on 3 Aug 2019  ·  25Comments  ·  Source: facebook/facebook-sdk-for-unity

Hello,

we're using Facebook SDK along with Firebase SDK and after updating to Unity 2019.2 I was forced to update Firebase SDK to version 6.2.2 (because of errors in the Unity editor) and it now appears to be incompatible with Facebook SDK because of issue in resolving dependencies. Here's a warning I get from Play Services Resolver while resolving:

Some conflicting dependencies were found.
The following dependency versions were modified:
com.android.support:appcompat-v7:25.3.1 --> androidx.appcompat:appcompat:1.0.0
com.android.support:cardview-v7:25.3.1 --> androidx.cardview:cardview:1.0.0
com.android.support:customtabs:25.3.1 --> androidx.browser:browser:1.0.0
com.android.support:support-v4:25.3.1 --> androidx.legacy:legacy-support-v4:1.0.0

It results in a crash on startup on Android.

I haven't digged into it a lot but I guess updating Play Services Resolver to some newer version and fixing dependencies here: https://github.com/facebook/facebook-sdk-for-unity/blob/master/Facebook.Unity.Editor/android/AndroidSupportLibraryResolver.cs might help.

Have a look for more info here as well:
https://github.com/googlesamples/unity-jar-resolver/blob/master/CHANGELOG.md#version-12118---jun-18-2019
Might be helpful for solving it. BTW, Firebase uses Play Services Resolver 1.2.122 now. I guess it's time to update it for Facebook SDK as well.

This is really serious issue, since we're not able to use official Facebook SDK on Unity 2019.2 together with Firebase now. We're not able to downgrade Unity version as well since it fixes another significant bug. Because of this we're kind of stuck and until this is fixed we're forced to disable Facebook functions in our game.

It is probably the same issue as described in one of comments here: https://github.com/facebook/facebook-sdk-for-unity/issues/281

Most helpful comment

@KylinChang - yes, my issues are fixed for now. This is thanks to fork made by @michael-looply However his changes need to be added to the main repository, otherwise it's only a temporary fix. Thx.

All 25 comments

Hey @andnoonesthere, I had the same issue and spent some time yesterday making a fork that functions w/ 2019.2.0f1 and Firebase. I've tested functionality of both SDKs on Android and iOS and it _looks like_ everything's running as expected. As a warning, the changes in my fork should make the build process backwards-incompatible when building the SDK via the provided scripts with Unity versions prior to 2019.2.0f1.

Note that if you want to build the SDK locally, I updated the repo to require configuration of an environment variable so that Hub-based Unity instances can be used (the .csproj files and shell scripts have hard-coded paths in this repo, making it impossible to build on machines that don't use that exact path to Unity).

In case you're interested in the changes I made, the relevant diff is located here. Package is located here.

Let me know if you have any issues getting up and running.


Additional note related to this particular issue since I'm posting this in the correct repo this time: I removed the resolver script you referenced in your comment and migrated the dependencies into the Dependencies.xml file and updated the resolver version and that fixed the dependency issues I was having; that hopefully will fix them for you too.

Hello Michael,

I've just tested your changes (Android only) and it worked great for me as well. Good job! :)
I also agree that moving dependencies to Dependencies.xml file is an important thing to do as well.

Looking at your changes I think both the fix for the dependencies and solution paths should be included in the SDK by the developers so making our own modifications would be easier in the future.

Thanks again for your work. I guess until they release an official patch I will be using your version :)

After some additional testing I see that while on the device everything is fine, I get this warning in the editor:

FB.Init() has already been called. You only need to call this once and only once.

However I'm 100% sure it gets called only once, I perform additional check for FB.IsInitialized right before. That might be as well not connected with your modification but some bug in recent FB SDK. Let me know if you experience this as well.

Happy to help! I'll try to keep my fork updated as new SDK versions are released until 2019.2.0f1 is officially added.

Regarding your last comment: I'm not seeing that warning in-editor or on device. My suspicion is that you're seeing that becauseFB.Init() doesn't immediately set FB.IsInitialized to true; the SDK has to complete initialization - which isn't guaranteed to be immediate - prior to FB.IsInitialized being set.

The Editor specifically waits for a DLL load delegate to be invoked, which can be seen here.

I recommend keeping track of whether Facebook initialization is in-progress in addition to whether FB.IsInitialized is true or not. A quick way to track this is with a bool _isInitializing; that gets set to true at the time of FB.Init and set to false in the InitDelegate that you pass into the FB.Init method. That way you can abort additional FB.Init calls if one is in-progress in addition to if it is already completed.

OK, so the error was because of the way how I load scenes in play mode. When entering play mode I unload all loaded scenes, then load my custom set of scenes. That's where it got loaded twice, it was already loaded in that basic scene which was unloaded in the beginning. Thanks for the tips anyway :)

Hey @KylinChang. Why this was closed? You already fixed dependencies issues on your side and it will be included in the next version? I haven't seen any information on it.

Hi @andnoonesthere , I thought you have fixed the issues. I reopen the issue and file a task to fix the issue.

@KylinChang - yes, my issues are fixed for now. This is thanks to fork made by @michael-looply However his changes need to be added to the main repository, otherwise it's only a temporary fix. Thx.

Hello @andnoonesthere and @michael-looply. Which version of the Firebase and Play Services Resolver are you using, or more simple question, how do you use your workaround?

We have exactly the same problem and we are trying to use Firebase 6.2.2 with it's play services resolver and the workaround provided by @michael-looply with it's play services resolver (we have tried both of the resolvers and also one that doesn't use gradle) but we were unable to compile the application. I'm not sure if we are doing something wrong or if there is some other plugin that causes the same issue as Facebook (like AdMob, IronSource etc.)

Hi @Wolar - I'm using Firebase 6.2.2 and Resolver 1.2.124.0. I only keep one PlayServicesResolver directory in my project. What compilation error(s) are you seeing? I'm not using any ad mediation SDKs outside of Unity Ads at the moment, so I can't speak for whether or not those are causing more problems.

It's a gradle build error and it's something like :

"com.android.support.support-v4-27.0.2.aar' using Jetifier. Reason. The given artifact contains a string literal with a package reference 'android.support.v4' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx..()"

Facebook SDK is using this library and I think some other library use it as well. I have tried both Jetifier enabled and disabled in PlayServicesResolver but without any effect. We are also using custom gradle template so the PlayServicesResolver doesn't copy the libraries into the Plugins folder but rather adds them to that gradle template but that shouldn't play any role I guess.

@Wolar Do you have that AAR present in your Assets directory? It's possible another SDK isn't using the Resolver and just directly imports the AARs into the project.

Alternatively, does your dependencies block in your mainTemplate.gradle contain two references to the support library? You can't have two explicit versions of the same library listed; if you have one line in your dependencies block like implementation 'com.android.support:support-v4:25.3.1' // Assets/FacebookSDK/Plugins/Editor/Dependencies.xml:7 from the Resolver inside of the

// Android Resolver Dependencies Start
... dependencies
// Android Resolver Dependencies End

section and have another outside of that section like implementation 'com.android.support:support-v4:27.0.2', then you won't be able to build. For example, the ironSource Unity plugin advises manually modifying the mainTemplate.gradle file with a support library reference. You may need to create a Dependencies.xml for ironSource that the Resolver can process instead of modifying your mainTemplate.gradle directly.

@michael-looply I'm pretty sure we have one of the AARs in the plugins folder as well but I think I tried to delete it as I have expected it might cause the problem but I think it didn't help. I will check the other sdks like IronSource, thank you :)

I was able to solve it, it was more issues. What really helped me was to export the project as Android Studio project and debug it there. Basically these libs was also conflicting with AdMob and some leftover library from Fabric which we no longer use. So I upgraded AdMob, deleted that aar, hard changed few versions of libraries in dependency files and it seems to be working with jetifier on as androidx libs. Thanks for the help @michael-looply

@Wolar Glad to hear it's working! Happy to help :)

So @KylinChang when can we expect progress on this one?

@KylinChang ?

Still an issue in February 2020.

  • Unity 2019.3.1f1 (downgrade impossible)
  • newest facebook-unity-sdk-7.18.1
  • enforced usage of androidx and jetifier due to newest AdMob (downgrade impossible)
  • Android resolver still generates mentioned obsolete dependencies in ProjectSettings/AndroidResolverDependencies.xml as well as gradle template. No other mention of obsolete libraries project-wide
  • obsolete libraries disappear after removing Facebook SDK.

Are there any plans to upgrade FacebookSDK dependencies?

Still an issue in March 2020

  • Unity 2019.3.4f1
  • Facebook SDK 7.18.1
  • IronSource SDK 6.15.0.1

Can't build with i think any other sdk on planet. Firebase and IronSource included. This is ridiculous cause it directly apply to ads optimizations, money related thing.

Still an issue in March 2020

  • Unity 2019.3.4f1
  • Facebook SDK 7.18.1
  • IronSource SDK 6.15.0.1

Can't build with i think any other sdk on planet. Firebase and IronSource included. This is ridiculous cause it directly apply to ads optimizations, money related thing.

Looply's solution is no longer enough to work. I investigated this issue. Facebook needs to update their SDK to use AndroidX. But in the mean time, Google provides the External Dependency Manager tool, which is supposed to Jetify the Facebook SDK (and other old libraries) so it will work with AndroidX. Unfortunately, EDM doesn't work with Facebook SDK for some reason. HOWEVER: The Jetifier that comes with Android Studio does work! Just export your project form Unity, open in Android Studio, add these lines to gradle.properties:
android.useAndroidX=true
android.enableJetifier=true

Build and run and everything should go. However: Facebook needs to fix this pronto. (Or Google needs to fix their Jetifier.) However, every company I know, big and small, has released AndroidX versions of their libraries. I'm shocked that Facebook, a huge company, hasn't fixed this yet as it causes developers a lot of pain.

@lexscite

For anyone still stuck on this:
1) You can fix it using Android Studio (as I mentioned above.)
2) There is also a solution from onesignal : https://documentation.onesignal.com/docs/troubleshooting-unity#section-android-x-compatibility that lets you run builds directly from Unity (by adding a settingsTemplate.gradle file.)

It appears the Jetifier/AndroidX support in Unity is somewhat busted.

This bug:
https://github.com/googlesamples/unity-jar-resolver/issues/360 is what I filed with unity-jar-resolver team but we'll see if that's even the right team to fix it.

No need for Android Studio Exports anymore for Jetifier to work within Unity.

Using newest 2019.3 official version (mine is 2019.3.12f1 atm) you can just use Custom Gradle Properties Template on top of the Main Gradle Template, and add:
android.useAndroidX=true
android.enableJetifier=true
to the newly generated gradleTemplate.properties file.

I have been exporting to Android studio to build with all the SDKs my publisher wants and working through the miseries of Android build errors ever since, as a junior dev I think I encountered every issue, but now I can finally build from within Unity. Jetifier wasn't working with Facebook SDK properly except in Android studio, so if you used enough AndroidX migrated plugins it would be hard to build from within Unity without some hacky changes.

I also usually have to change Plugins/Android/AndroidManifest.xml to have this in application tag:
tools:replace="android:appComponentFactory" android:appComponentFactory="leavemealone"
also xmlns:tools="http://schemas.android.com/tools" in manifest tag

Yes, it's a random string as I'm not sure what the purpose of this is but it works and fixes my manifest merger build error, oh well.

I can now build with Ironsource (many adapters with it), Facebook SDK, GameAnalytics, Adjust, Firebase, whatever else. Basically just get latest Unity Jar Resolver, check settings remove auto resolve, remove resolve on build, set patch maintemplate and use jetifier to true, force resolve, add any extra dependencies/repos you want, do the gradleproperties trick, build within Unity, done.

I ran into this issue on Facebook Unity SDK v8.1.1. Do I have to do all the things @mohamad-al-amaary did every time I want to integrate Facebook Unity SDK?

@nguyentrong101094
In the 2019.4 LTS Unity versions I have had no issues, all you need to do is this part from my initial reply:
android.useAndroidX=true
android.enableJetifier=true

Also I think new EDM has an option "Patch gradleTemplate.properties" which should do that for you even. I just enable Custom Main Manifest, Custom Main Gradle Template, Custom Gradle Properties Template every project now at least, and set the two properties, and make sure to resolve with EDM (https://github.com/googlesamples/unity-jar-resolver/blob/master/external-dependency-manager-latest.unitypackage) before build.

@Subtle-Tea I tried that. I still get this error when build

AndroidManifest.xml:38: AAPT: error: unexpected element <queries> found in <manifest>.

I'm using Unity 2019.4.17

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Duke74 picture Duke74  ·  4Comments

michelleran picture michelleran  ·  6Comments

MorganMoon picture MorganMoon  ·  7Comments

michelleran picture michelleran  ·  4Comments

naklow12 picture naklow12  ·  6Comments