Reputation: 331
I just started with Firebase and have designed a simple program to test FirebaseUI sign-in. I am getting an exception that the default web client id string wasn't populated.
There was an issue regarding this on Github https://github.com/firebase/FirebaseUI-Android/issues/1381 but it seems abandoned to me. I have updated the gradle dependencies to the latest versions but still had no luck.
Here's the code:
(MainActivity.java)
package uk.ac.le.cityTourPlanner;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.firebase.ui.auth.AuthUI;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private static int RC_SIGN_IN = 1 ;
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseAuth=FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!=null){
Toast.makeText(MainActivity.this, "user signed in!", Toast.LENGTH_LONG).show();
}
else{
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(providers).
setIsSmartLockEnabled(false).build(),RC_SIGN_IN);
}
}
};
}
@Override
protected void onResume() {
super.onResume();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onPause() {
super.onPause();
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
public void signOut(View view) {
AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(MainActivity.this, "user signed out!", Toast.LENGTH_LONG).show();
finish();
}
});
}
}
(MainActivity.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerInParent="true"
android:textSize="25dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:id="@+id/button"
android:text="Sign Out"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textSize="25dp"
android:onClick="signOut"/>
</RelativeLayout>
(AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.ac.le.cityTourPlanner">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And finally
(google-services.json)
{
"project_info": {
"project_number": "921658610309",
"firebase_url": "https://city-tour-planner.firebaseio.com",
"project_id": "city-tour-planner",
"storage_bucket": "city-tour-planner.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:921658610309:android:0a33483e26b227f2",
"android_client_info": {
"package_name": "uk.ac.le.cityTourPlanner"
}
},
"oauth_client": [
{
"client_id": "921658610309-10djmoc7i9k3doou7sq8sg4aog8v78jf.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "uk.ac.le.cityTourPlanner",
"certificate_hash": "fff5b002b82089efa22081bac552cc6b8ccab44c"
}
}
],
"api_key": [
{
"current_key": "AIzaSyD6OWj9Roi1bm40jiD54B3V-h5FsgqaJ-0"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "921658610309-4dhq16mrdgfd45jkrt0vo9hmlr1m4ju7.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
Upvotes: 6
Views: 3345
Reputation: 131
I tried the answer of Jack and it worked, but looking more in detail I found out that I didn't have this plugin:
'com.google.gms.google-services'
in my build.gradle
.
When I added this I got the following error:
Execution failed for task ':app:mergeDebugResources'.
> [string/default_web_client_id] /home/Documents/FireBaseLogIn/android-kotlin-login-start/app/build/generated/res/google-services/debug/values/values.xml [string/default_web_client_id] /home/Documents/FireBaseLogIn/android-kotlin-login-start/app/src/main/res/values/strings.xml: Error: Duplicate resources
Which means that string/default_web_client_id
is declared inside the plugin we added and that is why is required from the app.
I am not sure why adding this value in string.xml
works, but that as mentioned is a workaround.
So just add:
apply plugin: 'com.google.gms.google-services'
at the end of build.gradle(:app)
file.
Upvotes: 0
Reputation: 2005
Please also make sure that google-services.json file has been put in the app directory instead of being at the top level directory.
Upvotes: 0
Reputation: 5614
Refer to this similar question (same error, I answered for my situation there as well).
Basically, just add the default_web_client_id
into either your string.xml file like this:
<string name="default_web_client_id" translatable="false">webClientId.apps.googleusercontent.com</string>
Or add it into the google-services.json
file's Oauth section like this (normally you shouldn't do this but for this case, I explained why in my answer:
"oauth_client": [
...,
{
"client_id": "CLIENT_ID (ends with apps.googleusercontent.com)",
"client_type": 3
}
]
You can find the default_web_client_id
in your GCP console like this
go to API & Services > Credentials and under OAuth 2.0 client IDs you will find a ID with the name Web Client
Upvotes: 9