Skip to main content

Add WeChat Login on Android

This guide shows the Android integration points for WeChat OpenSDK. Complete the credential handoff first, then pin the SDK release selected for your application.

1. Add and configure OpenSDK​

Use the provider's Android package and installation instructions for your pinned release. Configure the assigned WeChat AppID, package identity, SDK-required manifest entries, and callback activity. On Android versions with package-visibility restrictions, include the WeChat package in the visibility configuration required by your SDK.

Initialize the SDK with the AppID supplied by NextAppMarket:

import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

IWXAPI wechat = WXAPIFactory.createWXAPI(context, wechatAppId, true);
wechat.registerApp(wechatAppId);

context is your Android context and wechatAppId is your assigned configuration value. Keep initialization accessible to your login screen and callback activity.

2. Request authorization​

Start a login attempt on your own backend and receive its random state. Bind that attempt to the initiating client session and an expiry. Use the returned value in the SDK request:

import com.tencent.mm.opensdk.modelmsg.SendAuth;

SendAuth.Req request = new SendAuth.Req();
request.scope = "snsapi_userinfo";
request.state = stateFromYourBackend;
boolean dispatched = wechat.sendReq(request);

If dispatch fails, leave the customer on your login screen and offer a useful retry or another sign-in method. A successful dispatch means that the request was handed to WeChat; your customer is not signed in yet.

3. Receive the callback​

Connect your wxapi.WXEntryActivity to IWXAPIEventHandler. Forward both its initial intent and any replacement intent to wechat.handleIntent(intent, handler). Use the callback setup required by the pinned SDK, including the complete activity package name.

In onResp, first check that the response is a SendAuth.Resp, then inspect errCode. On success, forward code and state to your backend over HTTPS. On cancellation or refusal, restore the normal login UI. Do not exchange the code inside the activity.

Persist the login attempt on the backend so an activity recreation does not lose its identity. Reject a callback that does not belong to the current attempt. See Server-side authentication for completion and account linking.

Verify the signed build​

Test the build signed for distribution on a device with WeChat. Confirm successful login, cancellation, WeChat unavailable, and callback return after your app has left the foreground. If a debug build works but the release does not, compare the installed package and signing configuration with the handoff record.

References: Android access guide, Tencent OpenSDK request fields, and response fields. The linked API reference is versioned; use your installed release's interfaces when integrating.