在Android模拟器上,Google登录无法使用,但在设备上可以正常工作。

3

最近我一直使用Genymotion用户,并成功登录了Google Android。但是当我尝试更新到Android模拟器folosec studio后,发现无法登录。我回到Genymotion,但在模拟器中无法登录。

实际设备可以正常工作。我已经按照正确的SHA1和JSON等步骤做了一切。可能是什么问题呢?

错误代码:

    Client not ready yet..Waiting for process to come online
Connected to process 12535 on device unknown-google_nexus_10___5_1_0___api_22___2560x1600_1-192.168.56.101:5555
I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
D/SignInActivity: handleSignInResult:false
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xf3eba9a0, error=EGL_SUCCESS
D/SignInActivity: handleSignInResult:false
W/InputMethodManager: Ignoring onBind: cur seq=96, given seq=95

构建:

    apply plugin: 'com.android.application'

android {
    signingConfigs {
        config1 {
            keyAlias 'xxxxx'
            keyPassword 'yyyy'
            storeFile file('G:/OneDrive/AndroidStudioProjects/vrt.jks')
            storePassword 'yyyy'
        }
    }
    compileSdkVersion 23
    buildToolsVersion '24.0.0 rc4'
    defaultConfig {
        applicationId 'xx.yyy.zzzz'
        minSdkVersion 18
        targetSdkVersion 23
        versionName '2.24.00.Sonia'
        versionCode 24

        // Enabling multidex support.
        multiDexEnabled true

        ndk {
            abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    dexOptions {
        incremental true
    }
    productFlavors {
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

}
allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'

    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    androidTestCompile 'com.android.support:support-annotations:23.4.0'



    compile project(':library')
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:mediarouter-v7:23.4.0'
    compile 'com.google.android.gms:play-services-cast:9.0.1'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.google.android.gms:play-services-appindexing:9.0.1'
    compile 'com.google.android.gms:play-services-analytics:9.0.1'
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.android.support:support-annotations:23.4.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    compile 'com.getbase:floatingactionbutton:1.10.1'
    compile 'com.google.android.libraries.cast.companionlibrary:ccl:2.8.3'
    compile 'me.zhanghai.android.materialprogressbar:library:1.1.6'
    compile 'com.google.android.gms:play-services-ads:9.0.1'
    compile 'com.google.android.gms:play-services-auth:9.0.1'
    compile 'com.google.android.gms:play-services-gcm:9.0.1'

}

代码登录:

    public class Login extends AppCompatActivity implements
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

    private static final String TAG = "SignInActivity";
    private static final int RC_SIGN_IN = 9001;

    private GoogleApiClient mGoogleApiClient;
    private TextView mStatusTextView;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Views
        mStatusTextView = (TextView) findViewById(R.id.status);

        // Button listeners
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.sign_out_button).setOnClickListener(this);
        findViewById(R.id.disconnect_button).setOnClickListener(this);

        // [START configure_signin]
        // Configure sign-in to request the user's ID, email address, and basic
        // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        // [END configure_signin]

        // [START build_client]
        // Build a GoogleApiClient with access to the Google Sign-In API and the
        // options specified by gso.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
        // [END build_client]

        // [START customize_button]
        // Customize sign-in button. The sign-in button can be displayed in
        // multiple sizes and color schemes. It can also be contextually
        // rendered based on the requested scopes. For example. a red button may
        // be displayed when Google+ scopes are requested, but a white button
        // may be displayed when only basic profile is requested. Try adding the
        // Scopes.PLUS_LOGIN scope to the GoogleSignInOptions to see the
        // difference.
        SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
        signInButton.setSize(SignInButton.SIZE_STANDARD);
        signInButton.setScopes(gso.getScopeArray());
        // [END customize_button]
    }

    @Override
    public void onStart() {
        super.onStart();

        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        if (opr.isDone()) {
            // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
            // and the GoogleSignInResult will be available instantly.
            Log.d(TAG, "Got cached sign-in");
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            // If the user has not previously signed in on this device or the sign-in has expired,
            // this asynchronous branch will attempt to sign in the user silently.  Cross-device
            // single sign-on will occur in this branch.
            showProgressDialog();
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    signIn();
                    hideProgressDialog();
                    handleSignInResult(googleSignInResult);
                }
            });
        }
    }

    // [START onActivityResult]
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }
// [END onActivityResult]

    // [START handleSignInResult]
    private void handleSignInResult(GoogleSignInResult result) {
        Log.d(TAG, "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();
            mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()+" Your Token is: "+acct.getId()));



            Intent intent = new Intent(this, Main2Activity.class);
            Bundle extras = new Bundle();
            extras.putString("poza_url", String.valueOf(acct.getPhotoUrl()));
            extras.putString("nume_utilizator", String.valueOf(acct.getDisplayName()));
            extras.putString("token", String.valueOf(acct.getId()));
            extras.putString("email", String.valueOf(acct.getEmail()));
            intent.putExtras(extras);
            startActivity(intent);



finish();







            updateUI(true);
        } else {
            // Signed out, show unauthenticated UI.
            updateUI(false);
        }
    }
// [END handleSignInResult]

    // [START signIn]
    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
// [END signIn]

    // [START signOut]
    private void signOut() {
        Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        // [START_EXCLUDE]
                        updateUI(false);
                        // [END_EXCLUDE]
                    }
                });
    }
// [END signOut]

    // [START revokeAccess]
    private void revokeAccess() {
        Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        // [START_EXCLUDE]
                        updateUI(false);
                        // [END_EXCLUDE]
                    }
                });
    }
// [END revokeAccess]

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // An unresolvable error has occurred and Google APIs (including Sign-In) will not
        // be available.
        Log.d(TAG, "onConnectionFailed:" + connectionResult);
    }

    private void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage(getString(R.string.loading));
            mProgressDialog.setIndeterminate(true);
        }

        mProgressDialog.show();
    }

    private void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.hide();
        }
    }

    private void updateUI(boolean signedIn) {
        if (signedIn) {
            findViewById(R.id.sign_in_button).setVisibility(View.GONE);
            findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
        } else {
            mStatusTextView.setText(R.string.signed_out);

            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
            findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
            case R.id.sign_out_button:
                signOut();
                break;
            case R.id.disconnect_button:
                revokeAccess();
                break;
        }
    }
}

请求权限时,我在模拟器中接受了但没有得到批准。请帮忙解决。谢谢。


你在模拟器上安装了GApps吗?https://gist.github.com/wbroek/9321145 - Alex
是的,我已经添加了Gapps。 - Diaconu Liviu
你必须确保模拟器中存在Google Play服务。 - jaibatrik
我安装了gapps,我已经登录了Play商店,并更新了Google服务。 - Diaconu Liviu
1个回答

2
现在我已经将这个添加到build.gradle中,并且从模拟器中运行正常。
signingConfig signingConfigs.config1

配置1是

signingConfigs {
    config1 {
        keyAlias 'xxxxx'
        keyPassword 'yyyy'
        storeFile file('G:/OneDrive/AndroidStudioProjects/vrt.jks')
        storePassword 'yyyy'
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接