include ':react-native-google-signin', ':app'
project(':react-native-google-signin').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/google-signin/android')
5.MainApplication.java
React Native 0.6 以後因為 Auto linking 不用此步驟
@OverrideprotectedList<ReactPackage>getPackages() { @SuppressWarnings("UnnecessaryLocalVariable")List<ReactPackage> packages =newPackageList(this).getPackages();// Packages that cannot be autolinked yet can be added manually here, for example:// 加上下面這行packages.add(newRNGoogleSigninPackage());return packages; }
範例
import {GoogleSignin, statusCodes} from'@react-native-community/google-signin';GoogleSignin.configure();consthandleGoogleSignIn=async () => {try {awaitGoogleSignin.hasPlayServices();constuserInfo=awaitGoogleSignin.signIn();console.log(userInfo) } catch (error) {console.log(error)if (error.code ===statusCodes.SIGN_IN_CANCELLED) {// user cancelled the login flow } elseif (error.code ===statusCodes.IN_PROGRESS) {// operation (e.g. sign in) is in progress already } elseif (error.code ===statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {// play services not available or outdated } else {// some other error happened } } };