firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
827 stars 428 forks source link

Failed to read firebase options #677

Closed Arcalise08 closed 3 years ago

Arcalise08 commented 4 years ago

Please fill in the following fields:

Unity editor version: 2020.1.0b8 Firebase Unity SDK version: 6.14 Source you installed the SDK (.unitypackage or Unity Package Manager): .unitypackage Firebase plugins in use (Auth, Database, etc.): Auth and analytics Additional SDKs you are using (Facebook, AdMob, etc.): none fresh project 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:

"Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly." Error on deploy. google-services.json is in root assets in unity. Reproduced in fresh unity project.

Please answer the following, if applicable:

Have you been able to reproduce this issue with just the Firebase Unity quickstarts (this GitHub project)? Havent tried

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

Arcalise08 commented 4 years ago

Just for anyone else having this issue, The workaround is directly adding the sourceset to the mainTemplate.gradle. You do this by going to project settings//publishing settings//build checkmark custom main gradle template it will give you the location to the file then add this code to the file.

sourceSets { main { res.srcDirs += '<full path from your root directory to google-services.xml>' } } and add this array to noCompress under aaptOptions noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'google-services.json']

all together it should look like this as an example

 lintOptions {
        abortOnError false
    }
 sourceSets { main { res.srcDirs += 'C:\\Users\\USERNAME\\Documents\\Unity Projects\\GAMENAME\\Assets\\Plugins\\Android\\Firebase\\res\\values\\google-services.xml' } }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'google-services.json']
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

Even though a workaround exist no mention of this issue is in any of the documentation for unity that ive seen. So im fairly positive its a bug. I'm not sure which versions this started in, I know for sure unity 2021.1.0b8, 2021.1.0b7, & 2021.1.0b6 are affected.

MarconiusDMM commented 4 years ago

Is there a way to put a local path into sourceSets? Putting a global path doesn't work for version control between multiple users.

Arcalise08 commented 4 years ago

I'm not at my PC to test this but try


    sourceSets {
        main { 
            def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
            res.srcDirs += (unityProjectPath +  '/Assets/Plugins/Android/Firebase/res/values/google-services.xml') 
        } 
    }

Lemme know if it works for you. I'm not a big gradle user. I dont mess with it much so theres probably better ways to do this.

MarconiusDMM commented 4 years ago

@Arcalise08 That worked great, thanks! For me at least, the noCompress part was actually not necessary to include, works without it.

chkuang-g commented 4 years ago

Hi folks,

I did noticed that FirebaseApp cannot be created in an Android build created from Unity 2020.1.0b10. I suspect there are some breaking change from Unity 2020 that stop packing google-services.xml from Assets/Plugins/Android/Firebase/res/values folder to the build.

We will look into. At the meantime, @Arcalise08 's suggestion to utilize mainTemplate.gradle looks good.

Shawn

Thaina commented 4 years ago

@Arcalise08 Thanks for your solution, however I want to add that we need to add every file in /res/ folder, if there is any

Currently now I need to manually add file by file like this


    sourceSets {
        main { 
            res.srcDirs += ($/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") +  '/Assets/Plugins/Android/Firebase/res/values/google-services.xml') 
            res.srcDirs += ($/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") +  '/Assets/Plugins/Android/Firebase/res/values/crashlytics_build_id.xml') 
            res.srcDirs += ($/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") +  '/Assets/Plugins/Android/Firebase/res/values/crashlytics_unity_version.xml') 
        } 
    }

Are there anyway we could specified whole folder, or wildcard, in the sourceSets setting?

chkuang-g commented 4 years ago

Theoretically you should be able to do

    sourceSets {
        main { 
            res.srcDirs += ($/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") +  '/Assets/Plugins/Android/Firebase/res') 
        } 
    }

However, seems like Unity's gradle task "':unityLibrary:packageReleaseResources" would attempt to pack the meta files generated by Unity as well and results in an error like "google-services.xml.meta: Error: The file name must end with .xml"

Before Unity offers a better way to exclude meta file in gradle build, you would need to add each .xml files.

Thaina commented 4 years ago

@chkuang-g I am not know about gradle language at all but is this possible?


    sourceSets {
        main { 
            res.srcDirs += ($/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") +  '/Assets/Plugins/Android/Firebase/res/**')  
            res.exclude "**/*.meta"
        }
    }
Arcalise08 commented 4 years ago

@chkuang-g I am not know about gradle language at all but is this possible?


    sourceSets {
        main { 
            res.srcDirs += ($/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") +  '/Assets/Plugins/Android/Firebase/res/**')  
            res.exclude "**/*.meta"
        }
    }

not at my PC but I believe you can add the .meta filetype to the ignoreassetspattern

aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'google-services.json']
        ignoreAssetsPattern = "!.svn:!.meta:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

It may not work if anything in there relies on meta data. Trial and error is your best friend lol.

I'll mess around with it in the morning and see what I come up with.

chrisraz commented 4 years ago

I'm still having this issue... Using latest beta Unity 2020.1.0b12 I have the custom grade set with sourceSets { main { def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") res.srcDirs += (unityProjectPath + '/Assets/Plugins/Android/Firebase/res/values/google-services.xml') } }

And only firebase anylitics package in my project , have the google-services.json in my assets folder and regardless of what I have tried over the past week I keep getting.

Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly

Any help would be appreciated

chkuang-g commented 4 years ago

@chrisraz

I documented this known issue and resolution here.

https://firebase.google.com/docs/unity/setup#missing_firebase_android_config_file_in_unity_2020

Could you make sure you follow all these steps? I was able to fix the problem using our Analytics quickstart

It is really odd that Unity no longer supports res/values/ in 2020. Before we can provide any workaround in the SDK, please follow the instructions on that page.

chrisraz commented 4 years ago

@chkuang-g Thank you for the Reply! I just got back and have tried everything. have really followed the instructions and still no luck :( It seems to be an issue of reading or applying the google-services.json as per the logcat below.

for a bit of background. i'm using Unity2020.1.0b12 I have Added my dev SHA1 in firebase. I Have added the google-services.json to the assets folder

Im using: Firebase 6.15.0 only using FirebaseAnalytics.unitypackage

Have Forced resolved multiple times.

Other google services i have in the project Google Play Games Plugin 0.10.09 Admob Plugin

I have on app start: FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => { FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); });

I cannot log any events, app freezes when i do...

Log cat shows the following on app start 2020-06-16 11:19:45.428 9681-9681/com.razmobi.konggo W/FirebaseApp: Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project. 2020-06-16 11:19:45.428 9681-9681/com.razmobi.konggo I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 2020-06-16 11:19:45.482 9681-9703/com.razmobi.konggo V/FA: Deferring to Google Analytics for Firebase for event data collection. https://goo.gl/J1sWQy 2020-06-16 11:19:45.585 9681-9711/com.razmobi.konggo I/FA: To enable faster debug mode event logging run: adb shell setprop debug.firebase.analytics.app com.razmobi.konggo 2020-06-16 11:19:45.590 9681-9711/com.razmobi.konggo E/FA: Missing google_app_id. Firebase Analytics disabled. See https://goo.gl/NAOOOI 2020-06-16 11:19:58.982 9681-9849/com.razmobi.konggo E/firebase: Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly.

Attached is my mainTemplate.gradle (renamed to .txt to upload here) mainTemplate.gradle.txt

Arcalise08 commented 4 years ago

@chrisraz You need to remove the top line of your gradle

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

you also probably want to move the sourcesets into the android path instead of declaring android again.

Stick it right under lintOptions is where i put it.

    lintOptions {
        abortOnError false
    }
    sourceSets {
        main {
            def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
            res.srcDirs += (unityProjectPath +  '/Assets/Plugins/Android/Firebase/res/values/google-services.xml')
        }
    }
    aaptOptions {
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }
chrisraz commented 4 years ago

@Arcalise08 Thanks for reply...

Yes i missed that. i had it that way before, but removed everything firebase and tried re-adding everything and missed the top comment.

i just updated the mainTemplate to see attached (again added.txt to upload here) as you specified, ran the build on device... and still cant get it working. it has been driving me mad for the past 2 weeks :/

Logcat: 2020-06-16 12:05:41.152 19943-20501/? E/FA: Missing google_app_id. Firebase Analytics disabled. Seehttps://goo.gl/NAOOOI 2020-06-16 12:05:45.458 20552-20552/? W/FirebaseApp: Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project. 2020-06-16 12:05:45.458 20552-20552/? I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 2020-06-16 12:05:45.475 20552-20572/? V/FA: Deferring to Google Analytics for Firebase for event data collection. https://goo.gl/J1sWQy`

new mainTemplate.gradle mainTemplate.gradle.txt

Arcalise08 commented 4 years ago

@chrisraz Did you verify that there is a google.services.xml at /Assets/Plugins/Android/Firebase/res/values/

Arcalise08 commented 4 years ago

@chrisraz Actually wait, thats a different error. No longer missing google.services. that means this issue is fixed. you got other stuff going on.

chrisraz commented 4 years ago

@Arcalise08 do you mean before building in Unity? if so no i DON'T have google.services.xml at /Assets/Plugins/Android/Firebase/res/values/

Arcalise08 commented 4 years ago

@Arcalise08 do you mean before building in Unity? if so no i DON'T have google.services.xml at /Assets/Plugins/Android/Firebase/res/values/

My understanding is that google-services transforms google.services.JSON to google.services.xml which is then packed by unity. the error ive specified is an error an issue with the way unity has started packing files in the newer version. Your talking about a different issue. I would start a fresh project and carefully follow the steps outlined on firebase for unity if you can replicate the issue then start a new issue. I can tell from your gradle your missing com.google.gms:google-services:3.0.0 though. so some step might have been missed?

Not super sure but if you do start fresh make sure you follow the workaround here so you dont run into the same issue with it not being bundled correctly.

EDIT: That is to say, if your missing google-services.xml there is an issue! That should be there!

chrisraz commented 4 years ago

THANK YOU @Arcalise08 i just created a fresh project, installed firebase, and its working! i initially had a project with unity2020.2.09alpha and downgraded to the 2020.1.012beta version to try get a hold of this firebase issue... and maybe thats where things got messed up.

Now the part of exporting and reimporting all the files as a package from the old project.... but thank you again its working :)

russ-smith commented 4 years ago

Bumping this thread as this issue seems to have regressed in 2020.1.0f1

Have built release APKs using this workaround in 2020.1.0b12 , and it works just fine. Pointing Unity Cloud Build at the same commit and upgrading to 2020.1.0f1 causes the build to fail at the gradle stage after exporting the Unity project. It also fails on my local machine.

Easy repro:

Partial error log:

27: [Unity] ERROR: FAILURE: Build failed with an exception.
28: [Unity] ERROR: 
29: [Unity] ERROR: * What went wrong:
30: [Unity] ERROR: Could not determine the dependencies of task ':launcher:lintVitalRelease'.
31: [Unity] ERROR: > This project uses AndroidX dependencies, but the 'android.useAndroidX' property is not enabled. Set this property to true in the gradle.properties file and retry.
32: [Unity] ERROR:   The following AndroidX dependencies are detected: androidx.versionedparcelable:versionedparcelable:1.0.0, androidx.fragment:fragment:1.0.0, androidx.slidingpanelayout:slidingpanelayout:1.0.0, androidx.core:core:1.0.0, androidx.customview:customview:1.0.0, androidx.swiperefreshlayout:swiperefreshlayout:1.0.0, androidx.interpolator:interpolator:1.0.0, androidx.loader:loader:1.0.0, androidx.drawerlayout:drawerlayout:1.0.0, androidx.viewpager:viewpager:1.0.0, androidx.collection:collection:1.0.0, androidx.localbroadcastmanager:localbroadcastmanager:1.0.0, androidx.lifecycle:lifecycle-common:2.0.0, androidx.arch.core:core-common:2.0.0, androidx.annotation:annotation:1.1.0, androidx.lifecycle:lifecycle-livedata:2.0.0, androidx.legacy:legacy-support-core-ui:1.0.0, androidx.lifecycle:lifecycle-viewmodel:2.0.0, androidx.lifecycle:lifecycle-livedata-core:2.0.0, androidx.arch.core:core-runtime:2.0.0, androidx.legacy:legacy-support-core-utils:1.0.0, androidx.documentfile:documentfile:1.0.0, androidx.cursoradapter:cursoradapter:1.0.0, androidx.lifecycle:lifecycle-runtime:2.0.0, androidx.coordinatorlayout:coordinatorlayout:1.0.0, androidx.asynclayoutinflater:asynclayoutinflater:1.0.0, androidx.print:print:1.0.0

This is a strange error since the resolver adds these lines to mainTemplate.gradle

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

so perhaps the gradle templates are being parsed / merged differently in the latest Unity? I had a go at debugging but my gradle skills are non-existent...

chkuang-g commented 4 years ago

Hi folks,

I think Unity definitely changed the generated gradle project. There might be some thing EDM4U need to adapt for the new gradle project structure. while still supporting Unity 2019 and below.

I'll have the team to really look into this and sort out all the change made in Unity 2020.

@russ-smith Seems like android.useAndroidX and android.enableJetifier no longer live under project.
These lines are injected by EDM4U here. https://github.com/googlesamples/unity-jar-resolver/blob/master/source/AndroidResolver/src/GradleTemplateResolver.cs#L321 And there is a workaround documented here https://github.com/googlesamples/unity-jar-resolver/issues/360#issuecomment-644461859

There are a couple of options for the workaround

  1. Made a custom EDM4U build with that block of code removed. We cannot ship this because Unity 2019 and below still depends on it.
  2. Disable Android Resolver Auto Resolution. After doing a Force Resolve, remove those lines from mainTemplate.gradle

Sorry for the inconvenience.

Shawn

exzizt commented 4 years ago

Is there likely to be a patch for this soon? Just want to know before I start looking into implementing the workaround.

Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly.
chkuang-g commented 4 years ago

@exzizt

The team is currently looking into the issue with google-services.json and Jetifier in Unity 2020. Will keep you updated.

exzizt commented 4 years ago

@exzizt

The team is currently looking into the issue with google-services.json and Jetifier in Unity 2020. Will keep you updated.

That is awesome to hear! Thanks for keeping me posted.

chkuang-g commented 4 years ago

Hi folks, some updates.

We found this breaking change in Unity 2020.

Android: Android Library Project plugins now require to be directories with .androidlib "extension"; can be place anywhere inside Unity project.

So a quick workaround for this issue is to rename Assets/Plugins/Android/Firebase directory to Assets/Plugins/Android/Firebase.androidlib.

Another issue I discovered is that android:minSdkVersion attribute is no longer supported in AndroidManifest.xml so please remove that in Assets/Plugins/Android/Firebase/AndroidManifest.xml.

Both fixes will be included in the next release.

Also we are investigating the most generic way to fix the issue with Jetifier in Unity 2020 (https://github.com/googlesamples/unity-jar-resolver/issues/360#issuecomment-644461859) and will likely to be fixed from EDM4U side.

Shawn

keskin commented 4 years ago

Hi @chkuang-g renaming Assets/Plugins/Android/Firebase directory to Assets/Plugins/Android/Firebase.androidlib is fixed the issue. Thank you.

shefyg commented 4 years ago

Hi, When I download the google-services file from firebase its a JSON file. But when I try to build for android, I get this error

google-services.json: Resource and asset merger: The file name must end with .xml

I do have an XML file on D:\Dropbox\MyProjects2020\Learning\Unity\tryProjs\TryGoogleAnalyticsv001\Assets\Plugins\Android\Firebase.androidlib\res\values

How to handle this issue?

Shefy

chkuang-g commented 4 years ago

@shefyg

Did you put google-services.json under Assets\Plugins\Android\Firebase.androidlib\res\values?

If so, just move it out of of Firebase.androidlib folder.

cgascons commented 4 years ago

Hi all, one quick question, after renaming the folder to have the extension ".androidlib", Unity automatically creates another one without it (as if it was missing) and I keep getting errors. Any ideas on what should we do to avoid this from happening? Using Unity v2020.1.3f1 and Firebase v6.15.2

chkuang-g commented 4 years ago

@cgascons

Yes, Firebase 6.15.2 will keep creating Assets\Plugins\Android\Firebase but that should not affect the build. What error did you get?

BTW, here is a blog post regarding all Android build issues in Unity 2020. Worth checking if you have done all the workarounds. https://firebase.googleblog.com/2020/08/firebase-compatibility-with-unity-20201.html

Several notes:

shefyg commented 4 years ago

Just for anyone else having this issue, The workaround is directly adding the sourceset to the mainTemplate.gradle. You do this by going to project settings//publishing settings//build checkmark custom main gradle template it will give you the location to the file then add this code to the file.

sourceSets { main { res.srcDirs += '<full path from your root directory to google-services.xml>' } } and add this array to noCompress under aaptOptions noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'google-services.json']

all together it should look like this as an example

 lintOptions {
        abortOnError false
    }
 sourceSets { main { res.srcDirs += 'C:\\Users\\USERNAME\\Documents\\Unity Projects\\GAMENAME\\Assets\\Plugins\\Android\\Firebase\\res\\values\\google-services.xml' } }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'google-services.json']
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

Even though a workaround exist no mention of this issue is in any of the documentation for unity that ive seen. So im fairly positive its a bug. I'm not sure which versions this started in, I know for sure unity 2021.1.0b8, 2021.1.0b7, & 2021.1.0b6 are affected.

Thanks @Arcalise08 , It was very helpful.

shefyg commented 4 years ago

@shefyg

Did you put google-services.json under Assets\Plugins\Android\Firebase.androidlib\res\values?

If so, just move it out of of Firebase.androidlib folder.

I used this link to turn it into XML: https://dandar3.github.io/android/google-services-json-to-xml.html

cgascons commented 4 years ago

What error did you get?

First of all thank you for your quick reply, the error I was getting when building was that the configuration files that were supposed to be inside the Assets\Plugins\Android\Firebase folder were missing, after manually creating again the folder (this time with the .androidlib extension), I was able to create the build wihout problems.

Also, regarding your notes, I was indeed to also get rid of the gradle template, so far so good.

Looking forward the new version.

CoderNerd commented 4 years ago

This fixes all issues. In mainTemplate add this to bottom of the file.

android { sourceSets { main { def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") res.srcDirs += (unityProjectPath + '/Assets/Plugins/Android/Firebase/res/values/google-services.xml') } }

Then in project settings use Custom Gradle Template

chkuang-g commented 4 years ago

Hi folks,

Here is a blog post regarding to all the issues in Unity 2020. https://firebase.googleblog.com/2020/08/firebase-compatibility-with-unity-20201.html

All of these should be fixed in the next SDK release.

chkuang-g commented 4 years ago

Hi folks,

The issue related to Jetifier and AndroidX is released from EDM4U side.

You can install 1.2.159 unitypackage. UPM version available in around 30min

And still brewing the next Firebase release. Will keep you all updated.

soorya-ha commented 4 years ago

This helped me to fix this issue. https://firebase.google.com/docs/unity/setup#missing_firebase_android_config_file_in_unity_2020

chkuang-g commented 3 years ago

Hi folks,

Good news. 6.16.0 is release and it should resolve ALL of the following issues:

That is, you should no longer need any workaround described in this blog post or this doc after you upgrade to 6.16.0.

I'll close this issue for now. If the issue remains, please report a new bug using bug template