有和没有广告的多种构建风格(新 Google 开发者政策)
Multiple Build Flavors with and without Ads (new Google Developer Policy)
我发布了一个具有 2 种构建风格的应用程序:一个 "normal" 版本包含广告和一个无广告版本。
在 Google Play Developer Console 中,如果您的应用使用广告,您现在必须对其进行标记。这对于普通版本是可以的,但无广告版本使用与专业版相同的依赖项(尤其是 google 播放服务)。所以当我将此版本设置为无广告时收到警告,因为发现广告库。
是否可以根据 gradle 构建风格更改依赖项?
build.gradle:
android {
(...)
productFlavors {
lite {
signingConfig signingConfigs.Release
versionCode 14
versionName '1.1.5'
buildConfigField "boolean", "IS_PRO", "false"
}
pro {
applicationId 'com.example.exampleadfree'
signingConfig signingConfigs.Release
targetSdkVersion 21
versionCode 14
versionName '1.1.5'
buildConfigField "boolean", "IS_PRO", "true"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.0.2'
compile 'com.google.android.gms:play-services:6.1.+'
compile project(':libraries:SharedItems')
compile 'com.android.support:recyclerview-v7:21.0.2'
}
When a project declares Product Flavors, these extends the main configuration.
来自 here。因此,Product Flavors 有效地为您声明的每种口味添加了新配置。在 gradle 中,可以添加特定于配置的依赖项。例如,
dependencies {
<configname> <dependency>
}
如果您想列出您的项目添加的所有配置:
configurations.findAll().each{println "$it.name"}
在您的项目中,您会看到与您的产品风格同名的配置。正如@cwbowron 评论的那样,为 flavor lite
:
添加编译时依赖
dependencies {
liteCompile <dependency>
}
你可以改变
compile 'com.google.android.gms:play-services:6.1.+'
到
liteCompile 'com.google.android.gms:play-services:6.1.+'
这将仅包含精简版的播放服务库。
但您还没有完成,因为现在在您的应用程序中创建 AdView 和来自播放服务库的相关 classes 的代码在您创建专业版时将不会编译。
我在类似情况下(使用计费库)的解决方案是将所有引用排除库和相关 classes 的代码移动到一个源文件中,该文件也仅使用精简版构建, 然后为不引用该库的专业版提供虚拟实现。
例如,创建两个同名 java class 的 flavor-specific src 目录:
src/lite/java/com/example/myapp/util/AdUtil.java
src/pro/java/com/example/myapp/util/AdUtil.java
在精简版 AdUtil 中,您可以调用 google 播放服务并获取 AdView return:
View getAdView(...)
{
View adView = new AdView(...);
adView.setAdSize(...);
adView.setAdUnitId(...);
...
return adView;
}
并且在 class 的专业版中,您可以只放置一个不引用播放服务库的虚拟实现:
View getAdView(...)
{
return null;
}
然后在您的主应用程序代码中,当您调用 AdUtil.getAdView() 时,您将获得一个可以放置在屏幕上的精简版视图。在专业版中,您将得到一个空值,因此您可以跳过添加视图(但您可能已经在尝试首先创建 adview 之前检查您是专业版还是精简版)。
在 Google Play 支持聊天中,我在 Google Play 控制台中被告知 "No",尽管已检测到。所以包含 Google 库应该没有问题。
另一方面,道格的回答很优雅。
此致,
我发布了一个具有 2 种构建风格的应用程序:一个 "normal" 版本包含广告和一个无广告版本。
在 Google Play Developer Console 中,如果您的应用使用广告,您现在必须对其进行标记。这对于普通版本是可以的,但无广告版本使用与专业版相同的依赖项(尤其是 google 播放服务)。所以当我将此版本设置为无广告时收到警告,因为发现广告库。
是否可以根据 gradle 构建风格更改依赖项?
build.gradle:
android {
(...)
productFlavors {
lite {
signingConfig signingConfigs.Release
versionCode 14
versionName '1.1.5'
buildConfigField "boolean", "IS_PRO", "false"
}
pro {
applicationId 'com.example.exampleadfree'
signingConfig signingConfigs.Release
targetSdkVersion 21
versionCode 14
versionName '1.1.5'
buildConfigField "boolean", "IS_PRO", "true"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.0.2'
compile 'com.google.android.gms:play-services:6.1.+'
compile project(':libraries:SharedItems')
compile 'com.android.support:recyclerview-v7:21.0.2'
}
When a project declares Product Flavors, these extends the main configuration.
来自 here。因此,Product Flavors 有效地为您声明的每种口味添加了新配置。在 gradle 中,可以添加特定于配置的依赖项。例如,
dependencies {
<configname> <dependency>
}
如果您想列出您的项目添加的所有配置:
configurations.findAll().each{println "$it.name"}
在您的项目中,您会看到与您的产品风格同名的配置。正如@cwbowron 评论的那样,为 flavor lite
:
dependencies {
liteCompile <dependency>
}
你可以改变
compile 'com.google.android.gms:play-services:6.1.+'
到
liteCompile 'com.google.android.gms:play-services:6.1.+'
这将仅包含精简版的播放服务库。
但您还没有完成,因为现在在您的应用程序中创建 AdView 和来自播放服务库的相关 classes 的代码在您创建专业版时将不会编译。
我在类似情况下(使用计费库)的解决方案是将所有引用排除库和相关 classes 的代码移动到一个源文件中,该文件也仅使用精简版构建, 然后为不引用该库的专业版提供虚拟实现。
例如,创建两个同名 java class 的 flavor-specific src 目录:
src/lite/java/com/example/myapp/util/AdUtil.java
src/pro/java/com/example/myapp/util/AdUtil.java
在精简版 AdUtil 中,您可以调用 google 播放服务并获取 AdView return:
View getAdView(...)
{
View adView = new AdView(...);
adView.setAdSize(...);
adView.setAdUnitId(...);
...
return adView;
}
并且在 class 的专业版中,您可以只放置一个不引用播放服务库的虚拟实现:
View getAdView(...)
{
return null;
}
然后在您的主应用程序代码中,当您调用 AdUtil.getAdView() 时,您将获得一个可以放置在屏幕上的精简版视图。在专业版中,您将得到一个空值,因此您可以跳过添加视图(但您可能已经在尝试首先创建 adview 之前检查您是专业版还是精简版)。
在 Google Play 支持聊天中,我在 Google Play 控制台中被告知 "No",尽管已检测到。所以包含 Google 库应该没有问题。
另一方面,道格的回答很优雅。
此致,