XxGoliathusxX
XxGoliathusxX

Reputation: 982

Android Flavor Intent Activity

How to create an intent to an activity depending on the flavor?

Hierarchy:

main
-ActivityA

flavor(free)
-uses main/ActivityA

flavor(paid)
-uses own paid/ActivityA

So how to create a Intent depending on the current flavor?

Upvotes: 2

Views: 1596

Answers (3)

Sam
Sam

Reputation: 5392

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.

Upvotes: 0

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10881

You can get Activity class by it's name. This way you won't get compile errors:

Intent i = new Intent(this, Class.forName("com.xxx.packagename.paid.ActivityA"))

Upvotes: 6

Logic
Logic

Reputation: 2258

create build flavors as

productFlavors {
    paid {
          ...
    }
    free {
          ...
    }
}

    sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                aidl.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
            }

            free {
                java.srcDirs = ['free']
            }
            paid{
                java.srcDirs = ['paid']
            }
    }

and then create source folders as

+ src
+ main // this is your common code
    + java 
    + res
+ free
    + java
    + res
+ paid
    + java
    + res

so, when you build with specific flavor, the Activity from specific folder gets picked. So, you can place paid Activity in paid folder and free Activity in free folder.

Upvotes: -1

Related Questions