Varun Narisetty
Varun Narisetty

Reputation: 155

Android GoogleApiClient.Builder().enableAutoManage causes IllegalArgumentException: Can only use lower 16 bits for requestCode

I am getting an IllegalArgumentException: when trying to initialize GoogleApiClient as mentioned in this link here.

below is the code I have used to initialize the ApiClient and followed by the error log

mApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(Fitness.HISTORY_API)
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
                .addConnectionCallbacks(
                        new GoogleApiClient.ConnectionCallbacks() {
                            @Override
                            public void onConnected(Bundle bundle) {
                                Log.i(TAG, "Connected!!!");
                                
                                // Now you can make calls to the Fitness APIs.
                            }

                            @Override
                            public void onConnectionSuspended(int i) {
                                // If your connection to the sensor gets lost at some point,
                                // you'll be able to determine the reason and react to it here.
                                if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                    Log.i(TAG, "Connection lost.  Cause: Network Lost.");
                                } else if (i
                                        == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                    Log.i(TAG,
                                            "Connection lost.  Reason: Service Disconnected");
                                }
                            }
                        }
                )
                .enableAutoManage(getActivity(), 1, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                        Log.i(TAG, "Google Play services connection failed. Cause: " +
                                result.toString());
                        
                    }
                })
                .build();

error log

Process: fitbark.com.android, PID: 3299
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
        at android.support.v4.app.BaseFragmentActivityEclair.checkForValidRequestCode(BaseFragmentActivityEclair.java:64)
        at android.support.v4.app.BaseFragmentActivityEclair.startIntentSenderForResult(BaseFragmentActivityEclair.java:45)
        at android.support.v4.app.FragmentActivity.startIntentSenderForResult(FragmentActivity.java:75)
        at com.google.android.gms.common.ConnectionResult.startResolutionForResult(Unknown Source)
        at com.google.android.gms.common.api.internal.zzw$zzb.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

I am not getting any error when I comment the following method

enableAutoManage()

I understand that the error is due to somewhere the App is calling startActivityWithResult() with requestCode greater than 16bit but what I don't understand is where exactly is it happening and how to fix it?

Upvotes: 1

Views: 719

Answers (1)

MinosL
MinosL

Reputation: 148

This error does occur if you

  1. restrict the permissions of the google play services-app and
  2. target an old play services version and a newer support library version at the same time.

Which version of google play services and which android support library are you using?

I've had the same problem, got it solved by this google+ post:

While developing an app which uses google fit feature, we ran into one problem. When we updated the support libraries to 24.0.0-beta1 version it is throwing an exception for requestCode when building client for google fit. IllegalArgumentException: Can only use lower 16 bits for requestCode

...

found the cause, updating play services to 9.0.1 fixed it.

So, either an update of your play services libraries or a downgrade of the support libraries might be of help.

If you perform an update/downgrade, and still don't grant full rights to the play services-app, you will see a permission request-dialog caused by the enableAutoManage()-method.

Upvotes: 2

Related Questions