9

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

我们为每个客户端创建不同的风格,每个客户端都有DebugAPI Production,所以我试图在 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/\"")
    }
}

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

风味 company1,BuildType 调试

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

风味公司 1,BuildType 发布

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

风味公司2,BuildType调试

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

风味公司 2,BuildType 发布

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

5 回答 5

5

对于我的具体问题,在 URL 之间存在很大差异的情况下,我无法使其与 Flavors 和 BuildTypes 一起使用。

我能够通过使用特定strings.xml于每个构建变体(这是风味和构建类型的每种组合)来定义调试/生产 URL:

这些是执行此操作的文件夹结构:

src/flavour1Debug/res/values/strings.xml 
src/flavour1Release/res/values/strings.xml 

src/flavour2Debug/res/values/strings.xml 
src/flavour2Release/res/values/strings.xml 

EXTRA:这也可用于托管不同的google-services.json文件

于 2019-03-04T09:32:48.867 回答
3

尝试这样的事情:

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

于 2019-01-15T12:19:43.070 回答
1

这是工作示例

// 指定一个风味维度。

flavorDimensions "version"

productFlavors {
dev {
    dimension "version"
    applicationId "com.example.lk"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.dev.com/api/v1/\"")
}

prod {
    dimension "version"
    applicationId "com.example.in"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.prod.com/api/v1/\"")

}
staging {
    dimension "version"
    applicationId "com.example.other"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.staging.com/api/v1/\"")

}}




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

你可以像这样访问它

String baseURL =BuildConfig.BASE_URL_PATH;
于 2019-01-15T10:39:20.927 回答
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你会得到这样的东西:

在此处输入图像描述

于 2019-01-15T10:37:38.347 回答
0

您的主要问题是您没有正确放置您的 buildTypes for flavor,并且在每个公司内部都没有正确的参数,而且我建议您阅读有关 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")


    }
于 2019-01-15T10:56:12.780 回答