https://www.youtube.com/watch?v=Gic51O8fZxY
- Create new project in firebase console
- Add your app and register with SHA-1 in setting project
- Download copy
google-services.json
to root folderapp
- Enable Google Sign-in method in authentication
- Add this code your build.gradle files in
depedencies
(project level) to use the plugin
classpath 'com.google.gms:google-services:4.2.0'
Like this
buildscript {
dependencies {
....
// Add this line
classpath 'com.google.gms:google-services:4.2.0'
}
}
apply plugin: 'com.google.gms.google-services'
Like this
apply plugin: 'com.android.application'
....
// add this line
apply plugin: 'com.google.gms.google-services'
implementation 'com.utsman:easygooglelogin:1.0.6'
Define class EasyGoogleLogin
and setup init in onCreate
private EasyGoogleLogin googleLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// define class EasyGoogleLogin
googleLogin = new EasyGoogleLogin(this);
// setup
String token = getString(R.string.default_web_client_id) // generate token google services
googleLogin.initGoogleLogin(token, listener)
// call login in button
Button btnLogin = findViewById(R.id.btn_login)
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// start login
googleLogin.signIn(context);
}
});
}
Override onStart
and set initOnStart
@Override
protected void onStart() {
super.onStart();
// add this line
googleLogin.initOnStart();
}
Last one, override onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// add this line
googleLogin.onActivityResult(this, requestCode, data);
}
Write ui changing in listener. Set listener with LoginResultListener()
. Setup like this
googleLogin.initGoogleLogin(token, new LoginResultListener() {
@Override
public void onLoginSuccess(FirebaseUser user) {
}
@Override
public void onLoginFailed(Exception exception) {
}
@Override
public void onLogoutSuccess(Task<Void> task) {
}
@Override
public void onLogoutError(Exception exception) {
}
});
Or implement your activity with listener
public class MainActivity extends AppCompatActivity implements LoginResultListener
Override onLoginSuccess
, onLoginFailed
, onLogoutSuccess
and onLogoutError
And change your setup on onCreate
like this
googleLogin.initGoogleLogin(token, this) // `this` for LoginResultListener
Logout with
googleLogin.signOut(context);
Revoke with
googleLogin.revokeAccess(context);
For add button sign in with style, download firebase-ui
in depedencies
implementation 'com.firebaseui:firebase-ui-auth:4.3.0'
After sync, add this xml
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And Cast to SignInButton
SignInButton signInButton = findViewById(R.id.sign_button);