How to Modify Code Between Product Flavors

爷,独闯天下 提交于 2019-12-05 19:44:04

You can encode things in the directory structure, e.g.:

src/
  main/                    # Common source and assets
    java/
    res/
    AndroidManifest.xml
  free/                    # Source and assets for the free flavour
    java/
    res/
    AndroidManifest.xml
  paid/                    # Source and assets for the paid flavour
    java/
    res/
    AndroidManifest.xml

The code and assets that are specific to one version or the other go into the specific directories. The manifests are merged. You can use the FLAVOR field generated into BuildConfig.java to base if-else statements on.

The SourceSets and Dependencies section of the Android-Gradle-Plugin user guide explains this in greater detail.

Do I add if-else statements in my main java classes that check to see if it's free or paid?

Yes, you have to add if-else in your activities/fragments to check the flavour:

private void javaCode() {
    if (BuildConfig.FLAVOR.equals("paid")) {
        doIt();
    else {
        showOnlyInPaidAppDialog();
    }
}

Source: BuildConfig

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