Android - How To Get Flavor's ApplicationId

点点圈 提交于 2019-11-30 08:36:43

I would use build configuration variables in your product flavors. Something along the lines of:

productFlavors {
    free {
        applicationId "com.example.free"
        resValue "string", "app_name", "Free App"
        versionName "1.0-free"
        buildConfigField "boolean", "PAID_VERSION", "false"
    }
    paid {
        applicationId "com.example.paid"
        resValue "string", "app_name", "Paid App"
        versionName "1.0-paid"
        buildConfigField "boolean", "PAID_VERSION", "true"
    }
}

Then after a build you can use:

if (BuildConfig.PAID_VERSION) {
    // do paid version only stuff
}

You may have to do a sync/build on gradle after you add the attribute before you can compile and import the BuildConfig class that Gradle generates on your behalf.

I found best solution to get all values like APPLICATION_ID, BUILD_TYPE, FLAVOR, VERSION_CODE and VERSION_NAME.

Just write : Log.d("Application Id : ",BuildConfig.APPLICATION_ID); in your code. It will provide APPLICATION_ID of your flavor.

BuildConfig.java

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "";
}

For more details you can refer this link : http://blog.brainattica.com/how-to-work-with-flavours-on-android/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!