Android Gradle中每种风格的代码路径 [英] Code Path per Flavor in Android Gradle

查看:101
本文介绍了Android Gradle中每种风格的代码路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个buildTypes(debug,release)和2个productFlavors(product1,product2)。我想为每个buildType和productFlavors定义一个buildConfigField。 buildConfigField是从服务器下载数据的应用程序的url,它随每个productFlavor和buildTypes而变化。



现在,我有:

  buildTypes {
debug {
debuggable true
}
release {
debuggable false


productFlavors {
product1 {
buildConfigField STRING,URL_BASE,'https://api1.release.com'

}
product2 {
buildConfigField STRING,URL_BASE,'https://api2.release.com'

}
}

但是我想要这样的东西:

  buildTypes {
debug {
debuggable true
}
release {
debuggable false
}
}
productFlavors {
product1 {
debug {
buildConfigField STRING,URL_BASE,'https://api1.debug.com'
}
发布{
buildConfigField STRING,URL_BASE,'https://api1.release.com'
}
product2 {
debug {
buildConfigField STRING, URL_BASE,https://api2.debug.com'
}
版本{
buildConfigField STRING,URL_BASE,'https://api2.release.com '
}
}
}

我该如何实现这是什么?

更新:



每个URL_BASE都有不同的模式,所以我不能对URL进行分组。一个可行的解决方案是在不同的构建类型中添加2种味道的网址,并在味道中选择正确的一种。

  buildTypes {
debug {
debuggable true
buildConfigField STRING,API_VARIANT_PRODUCT1,'api1.deb.com''
buildConfigField STRING,API_VARIANT_PRODUCT2,'api2.debug.com '

}
release {
debuggable false
buildConfigField STRING,API_VARIANT_PRODUCT1,''api1.release.com''
buildConfigField STRING,API_VARIANT_PRODUCT2,' api2.release.com'
}
}

productFlavors {
product1 {
buildConfigField STRING,URL_BASE,'https:// + API_VARIANT_PRODUCT1 +/ v1'
}
product2 {
buildConfigField STRING,URL_BASE,'https://+ API_VARIANT_PRODUCT2 +/ v1'
}


$ / code>

更新2



如果你需要d在gradle中添加资源,就像'KEY_MAP'一样,解决方案位于此页面

解决方案

@Beni,你可以使用类似的东西

  buildTypes {
debug {
debuggable true
uildConfigField(String,API_VARIANT,'debugvariant')
}
release {
debuggable false
buildConfigField(String,API_VARIANT,'releasevariant')
}
}
productFlavors {
product1 {
buildConfigField(String,URL_BASE,'https:// api1。+ API_VARIANT +.com')
}
product2 {
buildConfigField(String ,URL_BASE,https:// api2。+ API_VARIANT +.com')
}
}

您提议做的问题是 buildConfigFiel的最后一个定义每个 buildType 的d STRING,URL_BASE值将用于所有产品口味。因此,在两个发行版本中,最终结果都会是https://api2.release.com



使用上面的代码可以在每个变体的 BuildConfig 文件中得到类似这样的结果

  //来自构建类型的字段:debug 
public static final String API_VARIANT = [debugvariant|releasevariant];
//来自产品风格的字段:[product1 | product2]
public static final String URL_BASE =https:// [api1 | api2]。 + API_VARIANT +.com;

希望这有助于您。


I have 2 buildTypes (debug, release) and 2 productFlavors (product1, product2). I want to define a buildConfigField for each buildType and productFlavors. The buildConfigField is the url the app to download data from the server, and it changes for each productFlavor and buildTypes.

Now, I have:

buildTypes {
    debug {
        debuggable true
    }
    release {
        debuggable false
    }
}
productFlavors {
    product1 {
        buildConfigField STRING, "URL_BASE",  '"https://api1.release.com"'

    }
    product2 {
        buildConfigField STRING, "URL_BASE", '"https://api2.release.com"'

    }
}

But I want something like this:

buildTypes {
    debug {
        debuggable true
    }
    release {
        debuggable false
    }
}
productFlavors {
    product1 {
        debug {
            buildConfigField STRING, "URL_BASE",  '"https://api1.debug.com"'
        }
        release {
            buildConfigField STRING, "URL_BASE",  '"https://api1.release.com"'
    }
    product2 {
        debug {
            buildConfigField STRING, "URL_BASE", '"https://api2.debug.com"'
            }
        release {
            buildConfigField STRING, "URL_BASE", '"https://api2.release.com"'
        }
    }
}

How I can achieve this?

Update:

Each URL_BASE has a different pattern so I can't group the URLs. A posible solution is to add the url base of the 2 flavor in the different build types and select the right one in the flavor.

buildTypes {
    debug {
        debuggable true
        buildConfigField STRING, API_VARIANT_PRODUCT1, '"api1.deb.com"'
        buildConfigField STRING, API_VARIANT_PRODUCT2, '"api2.debug.com"'

    }
    release {
        debuggable false
        buildConfigField STRING, API_VARIANT_PRODUCT1, '"api1.release.com"'
        buildConfigField STRING, API_VARIANT_PRODUCT2, '"api2.release.com"'
    }
}

productFlavors {
    product1 {
        buildConfigField STRING, URL_BASE, '"https://" + API_VARIANT_PRODUCT1 + "/v1"'
    }
    product2 {
        buildConfigField STRING, URL_BASE, '"https://" + API_VARIANT_PRODUCT2 + "/v1"'
    }
  }
}

UPDATE 2

If you need to add resources in gradle, like a 'KEY_MAP' the solution is in this page.

解决方案

@Beni, you can use something like this

buildTypes {
    debug {
        debuggable true
        buildConfigField("String", "API_VARIANT", '"debugvariant"')
    }
    release {
        debuggable false
        buildConfigField("String", "API_VARIANT", '"releasevariant"')
    }
}
productFlavors {
    product1 {
        buildConfigField("String", "URL_BASE", '"https://api1." + API_VARIANT + ".com"')
    }
    product2 {
        buildConfigField("String", "URL_BASE", '"https://api2." + API_VARIANT + ".com"')
    }
}

The problem with what you were proposing to do is that the last definition of the the buildConfigField STRING, "URL_BASE" values for each buildType will get used in all product flavors. So what you would end up with would be something like "https://api2.release.com" in both release versions.

Using the above you will end up with something like this in your BuildConfig files for each variant

// Fields from build type: debug
public static final String API_VARIANT = ["debugvariant"|"releasevariant"];
// Fields from product flavor: [product1|product2]
public static final String URL_BASE = "https://[api1|api2]." + API_VARIANT + ".com";

Hope this helps.

这篇关于Android Gradle中每种风格的代码路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆