41

I'm writing my first android app, and I'm just getting started with product flavors. I have an ad-supported app in beta, and I'm writing a paid version with no ads. I can compile both flavors, I think. When I open the gradle window, I see targets like "compile AdsDebugSources" and "compile PremiumDebugSources."

Now, if I double-click on either of those, the build runs to completion without error, but I can't figure out how to run the results. If I click on the green "Run" arrow at the top of the screen, I can never run the premium app.

There's only one entry, "app" that results in a an apk being installed and run on the attached device, and it's the AdsDebug version. I guess I need to add a new configuration, but I can't find any documentation that even mentions the word "flavor."

I've tried adding a configuration, but I don't understand what the questions mean. I looked at the settings for the default app, but they don't seem to mean much. How do I tell it that I want the premium version of my app?

Or does my problem have nothing to do with configurations? I've noticed that when I look at the Build/Edit Flavors the two flavors are listed, but none of the data fields are filled in. I would have thought that these would be copied from the manifest. Have I neglected something?

All I did to set up the flavors was to add this code to the app level build.gradle file:

flavorDimensions "dummy"

productFlavors {
    ads {
        dimension "dummy"
        applicationId 'com.example.myApp.ads'
    }

    premium {
        dimension "dummy"
        applicationId 'com.example.myApp.premium'
    }

}

What else do I need to do?

saulspatz
  • 5,011
  • 5
  • 36
  • 47
  • Have a look at my article about build variants. It have detailed discussion about build variants. [How to create Android Product Flavors and Build Variants](https://therightsw.com/android-product-flavors/) – rana_sadam Aug 18 '17 at 05:14
  • This is a good article. It would be even better if it explained something about the manifests for the various flavors. I found the documentation for that rather opaque. – saulspatz Aug 18 '17 at 14:41
  • I will add a section for different manifest in my article soon. – rana_sadam Aug 19 '17 at 05:00

2 Answers2

87

Open the Build Variants tool in Android Studio. By default, this is docked on the left.

It will show a list of modules in your project, with a drop-down for each indicating the build variant that will be used by the Run button.

Android Studio Build Variants tool

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    It shows nothing. I see the "module" and "Build Variant" titles, but there are no modules listed. – saulspatz Aug 17 '17 at 23:18
  • 2
    @saulspatz: That's... bizarre. You can try Tools > Android > Sync Project with Gradle Files from the main menu, and see if that now causes things to show up in the Build Variants tool. You might also consider creating a scrap project in Android Studio and looking at the Build Variants tool there, where you should see one entry for `app` with two build variants (`debug` and `release`). – CommonsWare Aug 17 '17 at 23:27
  • 1
    That did it. I have all 4 variants. Thanks a million. – saulspatz Aug 17 '17 at 23:33
  • I don't see anything called "build variant' under either 'tools' or 'build'. Silly of me to look in those places, I know. – johnrubythecat Aug 28 '18 at 22:58
  • 2
    @johnrubythecat It's now in File > Synch Project with Gradle Files – Kind Contributor Jan 17 '19 at 22:50
  • Thank you for Saving my time. – Ajinkya Kalaskar Oct 04 '21 at 11:23
2

SOLUTION

  1. If you want to create different type product flavours (like different urls: development url, Quality url and production url of our application)

so you want to follow this code is working properly.

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.premsinghdaksha"
        minSdkVersion 17
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


    flavorDimensions "client"
    productFlavors {
// use for production debug and release build
        production {
            dimension "client"
            buildConfigField("String", "BASE_URL", "YourURL")
            //if you want to use string anywhere of app show you define
            // buildConfigField and get value where you want to use.
            buildConfigField("String", "Shop_", "\"Shop1\"")
            // production app name

            //if you want change application name  and app icon so you define
            // resValue, manifestPlaceholders and get these value in Manifests file.
            resValue "string", "app_name", "Your production name"
            //production  app icon
            manifestPlaceholders = [
                    appIcon     : "@mipmap/app_icon",
                    appIconRound: "@mipmap/app_icon_round"
            ]
            signingConfig signingConfigs.release

        }
        // use for quality debug and release build
        quality {
            dimension "client"
            buildConfigField("String", "BASE_URL", "YourQualityurl")
            buildConfigField("String", "Shop_", "\"Shop2\"")
            //use for quality app name
            resValue "string", "app_name", "quality App name"
            // use for quality app icon
            manifestPlaceholders = [
                    appIcon     : "@mipmap/quality_app_icon",
                    appIconRound: "@mipmap/quality_app_icon_round",
            ]
        }
// use for development debug and release build
        development {
            dimension "client"
            buildConfigField("String", "BASE_URL", "development url")
            buildConfigField("String", "Shop_", "\"Shop3\"")
            //use for dev app name
            resValue "string", "app_name", "developer app name"
            //use for dev app icon
            manifestPlaceholders = [
                    appIcon     : "@mipmap/developer_icon",
                    appIconRound: "@mipmap/developer_icon_round"
            ]
        }

    }
}
  1. If you want to create signingApp for live application, So you have follow this code in build.gradle(:app) in android{}.

    signingConfigs {
            // use for signed apk
            release {
                storeFile file("../premsingh.jks")
                storePassword "app@app"
                keyAlias "key0"
                keyPassword "app@app"
                v1SigningEnabled true
                v2SigningEnabled true
            }
        }

Result will be showing on build Variants and click drop down Button use click of variants.

enter image description here