My Android app launches the in-app purchasing interface from a fragment, like this:
Bundle responseBundle = this.billingService.getBuyIntent(3, getPackageName(), id, "inapp", StringUtils.randomString(32));
PendingIntent pendingIntent = responseBundle.getParcelable("BUY_INTENT");
fragment.getActivity().startIntentSenderForResult(pendingIntent.getIntentSender(), R.id.buyUpgradeResponse, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
This was working fine until I increased my targetSdkVersion
from 23 to 25 to add some Android 7 features. Now the startIntentSenderForResult
line throws this exception:
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
This means the request code (2nd argument of that method) has a limit of 65,535. But the R.id.buyUpgradeResponse
, which is randomly generated from the ids.xml file, has a value of something like 20,000,000. Is there a way limit the range Android uses for randomly-generated resource IDs?
Another SO answer says this error only occurs when using android.support.v4.app.FragmentActivity
. My activities all use a base activity that extends AppCompatActivity
, which extends android.support.v7.app.AppCompatActivity
, which extends android.support.v4.app.FragmentActivity
. So another approach would be removing my reliance on the support.v4 class. But I'm afraid to make a global change that could have unintended consequences in any part of my app. (My minSdkVersion is 15, if that matters.)
I could also just replace R.id.buyUpgradeResponse with a hard-coded ID, but I'm afraid there are other places in the app that have the same problem, and that wouldn't solve all instances of the problem.
Update
I just confirmed that the change to my build settings is what triggered the problem. The problem did not occur with my previous settings:
compileSdkVersion 23
buildToolsVersion '25.0.2'
minSdkVersion 15
targetSdkVersion 23
compile "com.android.support:appcompat-v7:23.0.0"
The problem started when I updated these settings:
compileSdkVersion 25
buildToolsVersion '25.0.2'
minSdkVersion 15
targetSdkVersion 25
compile "com.android.support:appcompat-v7:25.3.1"
Update 2
I just ran into this again while implementing the Storage Access Framework. startActivityForResult
generated the invalid ID exception because the randomly-generated ID was 20 million something. Other functions in my app, like opening a fragment in a popup, use these IDs without complaint, but the SAF intents won't. I ended up hard-coding more IDs for the SAF intents. I think the best solution would be to restrict the range of the randomly-generated IDs to a range that all frameworks support, but I don't see a way to do that.