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.
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:
Different flavors:
demoDebug
demoRelease
fullDebug
fullRelease
Do this and check if the problem still exists or not.