0

In my project I have two flavors (free and paid). The paid one has some activities that extend the ones from the main package and have additionally a licensing implementation. Im now trying to setup my manifest that way, that it always opens that activity that relates to the current flavor. How to do that?

I do it like this at the moment.

if(isFree)
  startActivity(new Intent(this, ActivityA.class));
else
  startActivity(new Intent(this, com.project.app.paid.ActivityA.class))

The problem is that the free version complains that it does not know package "com.project.app.paid.". Thats good because proguard removes it.

XxGoliathusxX
  • 922
  • 13
  • 34
  • How is this different than [your question here](http://stackoverflow.com/questions/40858582/android-flavor-intent-activity)? – Mike M. Dec 04 '16 at 20:43
  • One possibility would be to add an `` for some private action to both of the activities, then use `android:exported="false"` to prevent third-party apps from starting these activities. Then, use an `Intent` using that action string. – CommonsWare Dec 04 '16 at 20:44
  • @Mike M. That did not work because the activities never get called explicitly so they get removed by proguard and I cant set bounties to push the question up. – XxGoliathusxX Dec 04 '16 at 20:45
  • @CommonsWare Sounds good. Can you give an example for implementation? – XxGoliathusxX Dec 04 '16 at 20:46
  • Ah, gotcha. Could you not set a rule to keep that class, though? – Mike M. Dec 04 '16 at 20:49
  • @MikeM. I thought about to call an empty method for every activity that is affected by this but thats in my opinion a "bad" solution. So I just asking for the "real" solution. – XxGoliathusxX Dec 04 '16 at 20:52
  • @MikeM. Im not into proguard and Im sure theres a better solution – XxGoliathusxX Dec 04 '16 at 20:53

1 Answers1

1

I know this is old post, but since it didn't get answered, let me point you in the way late direction in case someone else comes across this post.

You can do a lot with flavors, but what you are trying to do is far simpler than anyone has answered.

First you have a build variant to select your flavor for debugging and running. So use this, otherwise all your debugging will use default main release.

Secondly, you don't have to get package name, just use a build config flag or check flavor. I.E.

    android {
    signingConfigs {
        releaseA35Demo {
            storeFile file("$projectDir/../yaskeystore.jks")
            storePassword System.getenv('YOUR_APP_STUDIO_STORE_PASSWORD')
            keyAlias System.getenv('YOUR_APP_STUDIO_KEY_ALIAS')
            keyPassword System.getenv('YOUR_APP_STUDIO_KEY_PASSWORD')
        }
    }

    flavorDimensions 'default'

    productFlavors {
        a35Demo {
            dimension 'default'
            applicationId "com.appstudio35.yourappstudio"
            buildConfigField "String", "SERVER_URL", '"http://fakeNumbers.compute-1.amazonaws.com:3006"'
            buildConfigField "int", "BUSINESS_ID", "1"
            versionCode 1
            versionName "0.01.01-b1"
            minSdkVersion 21
        }
        a35DemoDev {
            dimension 'default'
            applicationId "com.appstudio35.yourappstudio.dev"
            buildConfigField "String", "SERVER_URL", '"http://fakeNumbers2.compute-1.amazonaws.com:3006"'
            buildConfigField "int", "BUSINESS_ID", "2"
            versionCode 1
            versionName "0.01.01-b1"
            minSdkVersion 21
        }
    }

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

            productFlavors.a35Demo.signingConfig signingConfigs.releaseA35Demo
            productFlavors.a35DemoDev.signingConfig signingConfigs.releaseA35Demo
        }
    }
}

Then simply reference it in code like:

BuildConfig.BUSINESS_ID 

Wherever you need it. Just make sure you don't accidentally use the BuildConfig of a library project when it auto imports the BuildConfig.

Next way is if you want to check your flavor you can simply do BuildConfig.FLAVOR to see which one you are on. However, keep in mind there are some compiler warnings about using it because you are checking against a flavor and the BuildConfig assumes it will ALWAYS be whatever you are currently in for the Build Variant dropdown, Which is not true, you can ignore this always true or always false warning, I assure you it works.

Lastly your package is driven by the build variant that you are debugging I'll add an image so you can see where to change that.

enter image description here

Hope that helps.

But the last thing I will say is that your MainActivity will be automatically handled you do not have to manage the Package for the MainActivity just launch it. It will use the respective flavor for the one you are in. Typically when you move a file to a flavor, you make your release flavor and your variants so that it will use the respective MainActivity. What you are doing is a bit hacky, but I guess it works. However, would recommend letting the flavor tool do it's job and launch it for you.

Just remove MainActivity from Main and make sure it is in your two flavors paid and free and you are good to go. For example, notice I do not have an Application folder or file in my main:

enter image description here

Now see the manifest points to it still.

enter image description here

So if you let the flavor do its job you will be fine. if you try to leave it in your main you will have to deal with the conflicts and warnings.

Sam
  • 5,342
  • 1
  • 23
  • 39