Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetifier fails with Facebook SDK 7.17.2 #360

Closed
doom-goober opened this issue Apr 16, 2020 · 15 comments
Closed

Jetifier fails with Facebook SDK 7.17.2 #360

doom-goober opened this issue Apr 16, 2020 · 15 comments
Assignees

Comments

@doom-goober
Copy link

doom-goober commented Apr 16, 2020

Please fill in the following fields:

Unity editor version: 2019.3.0f6
External Dependency Manager version: 1.2.135.0
Source you installed EDM (from .unitypackage or Unity Package Manager): .unitypackage
Features in External Dependency Manager in use (Android Resolver, iOS Resolver, VersionHandler, etc.): Jetifier
Plugins SDK in use (Firebase, Admob, Facebook, etc.): Firebase, Facebook
Platform you are using the Unity editor on (Mac, Windows, or Linux): Windows
Platform you are targeting (iOS, Android, and/or desktop): Android
Scripting Runtime (Mono, and/or IL2CPP): Mono

Please describe the issue here:

  1. Install Firebase Unity SDK 6.13 (We have Auth, Functions, and Database)
  2. Install Facebook SDK for Unity 7.17.2
  3. Turn Jetifier on.
  4. Force Resolve.
  5. Build and Run onto Android Device.
  6. The build will immediate exception with a series of duplicate class exceptions between AndroidX and android.support libraries:
    java.lang.RuntimeException: Duplicate class android.support.customtabs.ICustomTabsCallback found in modules classes.jar (androidx.browser:browser:1.0.0) and classes.jar (com.android.support:customtabs:28.0.0)

Other Notes: It appears the Unity Dependency Manager is failing to correctly Jetify the Facebook SDK. However, if you export the project and let Android Studio Jetify the project, everything works fine.

Please answer the following, if applicable:

What's the issue repro rate? (eg 100%, 1/5 etc)
100%

@doom-goober doom-goober added the new to be triaged label Apr 16, 2020
@Russel1
Copy link

Russel1 commented Apr 16, 2020

I have the same problem.

@patm1987
Copy link
Collaborator

Thanks for reaching out, can y'all do me a favor and verify that the issue occurs in in one of the Unity quickstarts (say the Auth one).

Can you also verify whether you're using the gradle template. If you use the gradle resolution, you'll likely have the file Assets/Plugins/Android/mainTemplate.gradle and you'll see it in your build settings (this varies with Unity version):
image

You'll also need to have patching enabled:
image

In this setup, you'll want to look at the mainTemplate.gradle file after resolving resources and look for duplicates here:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
// Android Resolver Dependencies Start
    implementation 'com.google.firebase:firebase-analytics:17.2.2' // Assets/Firebase/Editor/AppDependencies.xml:15
    implementation 'com.google.firebase:firebase-analytics-unity:6.13.0' // Assets/Firebase/Editor/AnalyticsDependencies.xml:18
    implementation 'com.google.firebase:firebase-app-unity:6.13.0' // Assets/Firebase/Editor/AppDependencies.xml:20
    implementation 'com.google.firebase:firebase-common:19.3.0' // Assets/Firebase/Editor/AppDependencies.xml:13
// Android Resolver Dependencies End
**DEPS**}

and you can prune dependencies here as a workaround. It would be helpful to know if you see the erroneous entries here.

If you're not using this process (you see a bunch of jar files in Assets/Plugins/Android) let us know as well.

Thanks for your help!
--Patrick

@patm1987 patm1987 added needs-info Need information for the developer and removed new to be triaged labels Apr 17, 2020
@chkuang-g
Copy link
Collaborator

chkuang-g commented Apr 17, 2020

@doom-goober
I think this is an issue in Facebook SDK.
facebook/facebook-sdk-for-unity#303
That is, they really need to upgrade their SDK to be compatible with AndroidX.

I downloaded Facebook SDK 7.17.2 and I noticed that there are some loose .aar under Assets/FacebookSDK/Plugins/Android/libs/facebook-android-wrapper-7.17.2.aar. I suspect this depends on legacy Android support library and cause the conflict. Since Facebook did not reference it in Dependencies.xml , Android Resolver cannot manage and patch it with Jetifier for you.

AFAIK, newer Unity versions (not all) can patch those loose .aar with Jetifier if you build with gradle. Please try what @patm1987 mentioned: turn on Custom Gradle Template, enable Jetifier and Patch mainTemplate.gradle in Android Resolver settings and resolve again.

Let us know,
Shawn

@gromilQaaaa
Copy link

https://documentation.onesignal.com/docs/troubleshooting-unity#section-android-x-compatibility

Didn't try yet, but found possible fix on onesignal help section
image

@Russel1
Copy link

Russel1 commented Apr 22, 2020

@gromilQaaaa Yeah thanks a lot, the OneSignal solution fixed the problem. :)

@doom-goober
Copy link
Author

@chkuang-g @patm1987

  • With all the suggested settings, using "Build and Run" from Unity, Firebase Auth sample works fine.
  • With all the suggested settings, using "Build and Run" from Unity, my app with FacebookSDK and Firebase, fails with "duplicate classes".
  • From exported project to Android Studio, my app with Facebook and Firebase runs on device.
  • With all the suggested settings, using "Build and Run" from unity, my app with FacebookSDK and Firebase runs on device if I use @gromilQaaaa suggestion of adding settingsTemplate.gradle.

This leads me to conclude that problem is the Jetifier is failing with a combo of Facebook and Unity. I opened this bug because I thought that the Jetifier in Unity was owned by the Unity Jar Resolver team. I apologize if I opened the bug in the wrong place. Who owns the Jetifier Process in Unity? Is that a Unity thing or a Google thing? Should I close this bug and file it somewhere else?

Thanks! (Sorry for delay in response. My team is approaching a ship date.)

@hlongvu
Copy link

hlongvu commented Apr 23, 2020

I follow OneSignal solution to fix then can build successfully. But the apk size increase significantly. Is this due to androidx library size or something else?
Does anyone face the same issue?

@Russel1
Copy link

Russel1 commented Apr 23, 2020

I agree my apk size has also increased by 8.8 MB.

@doom-goober
Copy link
Author

doom-goober commented May 3, 2020

@chkuang-g @patm1987 Do you still need more info? This appears to be an interplay between Unity, how the resolver runs the Jetifier/AndroidX/Gradle, and the Facebook SDK.

The workaround everyone has settled on (see the two comments above this one) is to enable the jetifier and androidX using a gradle script rather than through the resolver as per the instructions here: https://documentation.onesignal.com/docs/troubleshooting-unity#unity-20193-or-newer OneSignal explains what the issue is with this comment:

// This file contents should be placed at Assets/Plugins/Android/settingsTemplate.gradle
// This works around Unity's 2019.3 bug where their root build.gradle is placing buildscript under allprojects
// On it's own it doesn't create issues however doing so means including a buildscript block in any sub projects
// such as "unityLibrary" which is generated from the template Assets/Plugins/Android/mainTemplate.gradle does not work.
// It results in a build error of "Configuration with name 'compileClasspath' not found." on a lint task.
// Normally adding "lintOptions { abortOnError false }" bypasses any lint task errors however
// either due to a bug with the Android Gradle plugin or an order of operations this does seem to be applying in this case.
// Until Unity fixes their root build.gradle we will need to keep using this file to enable any additional Gradle plugins.

Please let me know if you need anything else and if I should be filing this bug somewhere more appropriate. Does Unity own this or does the Jar Resolver team own this?

@mohamad-al-amaary
Copy link

mohamad-al-amaary commented May 4, 2020

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.

@Russel1
Copy link

Russel1 commented May 4, 2020

@mohamad-al-amaary @doom-goober Has your APK size also increased significantly with the latest Unity?

@mohamad-al-amaary
Copy link

mohamad-al-amaary commented May 4, 2020

I did not notice a difference @Russel1 - then again I did not test it fully compared to the OneSignal solution, though my solution seems more organic and my last build was 5mb bigger actually.

@ldrly-roger
Copy link

I cannot thank you enough @mohamad-al-amaary, your advice works for me and has unblocked our progress! 👏 👏 👏

@chkuang-g
Copy link
Collaborator

chkuang-g commented Jun 16, 2020

@mohamad-al-amaary

Thank you for offering the workaround for this issue.

To summarize, Unity 2019.3+ changed its gradle build so that enabling Jetifier in mainTemplate.gradle such as

([rootProject] + (rootProject.subprojects as List)).each {
    ext {
        it.setProperty("android.useAndroidX", true)
        it.setProperty("android.enableJetifier", true)
    }
}

Instead, OneSignal recommended to create Assets/Plugins/Android/settingsTemplate.gradle and add the following block to it, in order to enable Jetifier:

static void enableJetifier(Project project) {
    project.ext['android.useAndroidX'] = true
    project.ext['android.enableJetifier'] = true
}

gradle.rootProject {
    it.afterEvaluate {
        it.allprojects {
            // Since Unity 2019.3 enabling Jetifier via mainTemplate.gradle is no longer working
            // Enabling it for all gradle projects here.
            enableJetifier(it)

            addBuildscript(it)
            applyPlugins(it)
        }
    }
}

We can investigate how to automate this process in EDM4U, or alternative way to solve this. We also welcome any pull request to solve this issue!

@chkuang-g chkuang-g added type: feature request and removed needs-attention Need Googler's attention labels Jun 16, 2020
@mohamad-al-amaary
Copy link

mohamad-al-amaary commented Jun 18, 2020

@chkuang-g Easiest way to build now as I mentioned in my previous answer with latest 2019.3 is enabling and using gradleTemplate.properties and adding in
android.useAndroidX=true android.enableJetifier=true
since those properties in mainTemplate just don't work.

So maybe automation would be making the "Use Jetifier" setting of EDM4U enable custom gradle properties in Unity and automatically add those in, but this feature is only available in 2019.3.9+ I think (definitely not sure but something like that) - for previous versions of 2019.3 I guess the OneSignal solution is the only way other than the dreadful export to Android Studio option (it's not so bad but hella time consuming if you wanna make a lot of builds and work within a team with testers and marketing builds etc.)

@googlesamples googlesamples locked and limited conversation to collaborators Oct 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants