Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.content.pm.PackageManager$NameNotFoundException: when using 2 product flavors?

Tags:

java

android

I have 2 product flavor. Let's say the example like this:

productFlavors {
    free {
      applicationId 'com.free.android'

    }
    premium {
      applicationId 'com.premium.android'

    }

My problem is when i use

Intent resultIntent = new Intent(this, ExpiryListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ExpiryListActivity.class);
stackBuilder.addNextIntent(resultIntent);

The problem happened when stackBuilder.addParentStack(ExpiryListActivity.class) The first app which used the productFlavor free doesn't cause android.content.pm.PackageManager$NameNotFoundException error. But the second app which used the productFlavor premium it causes android.content.pm.PackageManager$NameNotFoundException. Then i read the docs that stackBuilder.addParentStack(<Class>) Add the activity parent chain as specified by manifest . How to solve this problem?

like image 786
Andri Wu Avatar asked May 19 '16 03:05

Andri Wu


1 Answers

TL;DR change your metadata as

<meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="originalPackageName.ui.MainActivity" />

The basic problem is that gradle is expanding the package name for the parent activity class that you wrote in the metadata.

In case of your free product flavor it tries to find a class at location com.free.android.ui.MainActivity

And in case of your paid flavor it tries to find a class at location com.premium.android.ui.MainActivity

But gradle actually does not restructure the packages when you mention different applicationId's for your product flavors and the class is still located at originalPackageName.ui.MainActivity and hence the NameNotFoundException

where originalPackageName is a placeholder for the package name you started your project with.

like image 142
Aashrai Ravooru Avatar answered Sep 27 '22 21:09

Aashrai Ravooru