Get auth token doesn't work in account manager

1.1k views Asked by At

I have implemented account manager in my android app. I can add accounts and get auth tokens without any problem, and signed-apk also works correctly. The flow was working perfectly untill I added a new flavor to my build.grade :

productFlavors {
    release {
        applicationId "com.faranegar.flight.release"
        versionName "1.0.0"
        buildConfigField 'String', 'BASE_API', '"http://release.mysite.ir/api/"'
        buildConfigField 'String', 'BASE_MOBILE_API', '"https://releaseapp.mysite.ir/api/"'

    }
    demo {
        applicationId "com.faranegar.flight.demo"
        buildConfigField 'String', 'BASE_API', '"http://demo.mysite.ir/api/"'
        buildConfigField 'String', 'BASE_MOBILE_API', '"http://demoapp.mysite.ir/api/"'
        versionName "1.0.0"

    }
}

BUT in signed-apk get auth token doesn't work when flavor is added in gradle. I get auth token as this:

final AccountManager accountManager = AccountManager.get(context);
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        ActivityCompat.requestPermissions((Activity) context,
                new String[]{Manifest.permission.GET_ACCOUNTS},
                Constants.GET_ACCOUNT_PERMISSION);
        return;
    }
    final Account[] accounts = accountManager.getAccountsByType(AccountGeneral.ACCOUNT_TYPE);
    for (final Account account : accounts) {
        if (account.name.equals(Utils.getUserName(context))) {

            final AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, AccountGeneral.ACCOUNT_TYPE, null, (Activity) context, null, null);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        ((Activity)context).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context, "bundle token", Toast.LENGTH_SHORT).show();
                            }
                        });
                        Bundle bnd = future.getResult();
                        final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
                        accountControllerListener.onGetToken(authtoken);
                    } catch (final Exception e) {
                        sendErrorToRetrofit(e);
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

And I get this error when called 'Bundle bnd = future.getResult();'

android.accounts.AccountManager.convertErrorToException(AccountManager.java:2153)
android.accounts.AccountManager.access$500(AccountManager.java:149)

android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1996)     android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)

 android.os.Binder.execTransact(Binder.java:453)

NOTE: When I traced I found out that getAuthToken method in MyAuthenticator never called in signed-apk.

1

There are 1 answers

0
Morteza Rastgoo On

You have defiened the flavor in a wrong way, i think. Take a look at sample code below to see how defining a flavor is different from defining a build type:

Different build types:

You can create and configure build types in the module-level build.gradle file inside the android {} block. When you create a new module, Android Studio automatically creates the debug and release build types for you. Although the debug build type doesn't appear in the build configuration file, Android Studio configures it with debuggable true. This allows you to debug the app on secure Android devices and configures APK signing with a generic debug keystore.

    buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    debug {
        applicationIdSuffix ".debug"
    }

    }

Different flavors:

    productFlavors {
    demo {
        applicationIdSuffix ".demo"
        versionNameSuffix "-demo"
    }
    full {
        applicationIdSuffix ".full"
        versionNameSuffix "-full"
    }
}

After you create and configure your product flavors, click Sync Now in the notification bar. After the sync completes, Gradle automatically creates build variants based on your build types and product flavors, and names them according to . For example, if you created 'demo' and 'full' product flavors, and kept the default 'debug' and 'release' build types, Gradle creates the following build variants:

  • demoDebug

  • demoRelease

  • fullDebug

  • fullRelease

Do this and check if the problem still exists or not.