Android Gradle。如何将Flavors与buildTypes结合使用

时间:2019-01-15 10:27:17

标签: android android-gradle android-productflavors android-build-type

我正在开发一个白色品牌的应用程序。

我们为每个客户端创建了不同的样式,并且每个客户端都具有DebugProduction API,因此我尝试在Gradle中进行设置。

我应该怎么做?

这是我尝试过的:

buildTypes {
    debug {
        // some configurations
    }
    release {
        // some configurations
    }
}

flavorDimensions "client"
productFlavors {
    company1{
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
    }

    company2 {
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
    }
}

编辑: 我希望能够为每个Flavor和Buildtype定义一个不同的BASE_URL

Flavor company1,BuildType调试

https://app.company1.com/devApi/

Flavor company1,BuildType发布

https://app.company1.com/prodApi/

Flavor company2,BuildType调试

https://dev.company2.com/api/

Flavor company2,BuildType发布

https://prod.company2.com/api/

5 个答案:

答案 0 :(得分:1)

尝试这样的事情:

buildTypes {
    debug {
        buildConfigField("String", "BASE_URL_PATH", "\"devApi/\"")
    }
    release {
        buildConfigField("String", "BASE_URL_PATH", "\"prodApi/\"")
    }
}

flavorDimensions "client"
productFlavors {
    company1{
        dimension "client"
        buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company1/\"")
    }

    company2 {
        dimension "client"
        buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company2/\"")
    }
}

并像这样使用它:

  

字符串BASE_URL = BuildConfig.BASE_URL_DOMAIN +   BuildConfig.BASE_URL_PATH

答案 1 :(得分:0)

您可以使用口味为应用程序添加基本配置,范围从app urlAPI keysmaster password等。

flavorDimensions "Mobile"
     productFlavors {
        Production {
            dimension "Mobile"   // dimension can be mobile, kiosks, tv, miniKiosks etc

            resValue "string", "API_KEY", "Just to give the idea"
            resValue "string", "SERVICE_IP", "Your service IP"
            resValue "string", "SERVICE_BASE_URL", ""
            resValue "string", "APK_BASE_URL", "base url"
            resValue "string", "MASTER_PASSWORD", ""

        }
        Demo {
            dimension "Mobile"

            resValue "string", "API_KEY", "Just to give the idea"
            resValue "string", "SERVICE_IP", "Your service IP"
            resValue "string", "SERVICE_BASE_URL", "services/v1/"
            resValue "string", "APK_BASE_URL", "base url"
            resValue "string", "MASTER_PASSWORD", ""
        }

    Local {
            dimension "Mobile"

            resValue "string", "API_KEY", ""
//            resValue "string", "app_name", ""
            resValue "string", "SERVICE_IP", ""
//            resValue "string", "SERVICE_IP", ""
            resValue "string", "SERVICE_BASE_URL", ""
            resValue "string", "APK_BASE_URL", ""
            resValue "string", "MASTER_PASSWORD", "a"
        }
    }

现在,如果您查看您的 build varients ,您将得到类似以下内容的信息:

enter image description here

答案 2 :(得分:0)

这是工作示例

//指定一种风味尺寸。

flavorDimensions "version"

  productFlavors {
    srilanka {
      dimension "version"
      applicationId "com.example.lk"
      versionCode 1
      versionName "1.0.1"
      buildConfigField("String", "COUNTRY", "\"LK\"")
    }

india {
  dimension "version"
  applicationId "com.example.in"
  versionCode 1
  versionName "1.0.1"
  buildConfigField("String", "COUNTRY", "\"IN\"")

}
other {
  dimension "version"
  applicationId "com.example.other"
  versionCode 1
  versionName "1.0.1"
  buildConfigField("String", "COUNTRY", "\"OTHER\"")

}
}


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

您可以这样访问它

String country =BuildConfig.COUNTRY;

答案 3 :(得分:0)

您的主要问题是您没有正确地将口味的buildType放置在每个公司内部,也没有正确的参数,而且我建议您阅读有关Gradle设置的更多信息(developer.android)。

    buildTypes {
        debug {
            // some configurations
        }
        release {
            // some configurations
        }
    }

    flavorDimensions "version", "brand"
    productFlavors {
        dev {
            versionName += "dev"
            dimension "version"

            buildConfigField "String", "BASE_API_URL", "\...\""
        }

        prod {
            dimension "version"

            buildConfigField "String", "BASE_API_URL", "\...\""
        }
        company1{
            dimension "brand"
            versionName "1.0.0"
            buildConfigField("int", "CLONE_ID", "1")
            **here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "true")
        }

        company2 {
            dimension "brand" 
            versionName "1.0.0"
            buildConfigField("int", "CLONE_ID", "2")
            **here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "false")


    }

答案 4 :(得分:0)

对于我的特定问题,URL之间存在很大的不同,我无法使其与Flavors和BuildTypes一起使用。

我能够通过对每种口味/构建类型使用特定的strings.xml来定义调试/生产URL:

这些是这样做的文件夹结构:

src/flavour1/debug/res/values/strings.xml 
src/flavour1/res/values/strings.xml 

src/flavour2/debug/res/values/strings.xml 
src/flavour2/res/values/strings.xml 

额外: 这也可以用来托管不同的google-services.json文件