我找到了一个实现目标的解决方案,然后利用
this post by David Medenjak
.
首先使用applicationId和applicationIdSuffix构建applicationId。
分别为BuildConfig和AndroidManifest构建文件提供程序。
分别为AndroidManifest发布scheme\u前缀和scheme\u后缀,并稍后将它们连接起来。
从ApplicationVariant中的组件构建方案。
flavorDimensions "product", "license"
productFlavors.all {
ext.scheme_prefix = null
ext.scheme_suffix = ""
ext.license = null
}
productFlavors {
apple {
applicationId = "apple.company"
dimension "product"
scheme_prefix = "companyapple"
manifestPlaceholders = [scheme_prefix: "$scheme_prefix"]
buildConfigField("String", "KIND", "\"apple\"")
}
orange {
applicationId = 'orange.company'
dimension "product"
scheme_prefix = "companyorange"
manifestPlaceholders = [scheme_prefix: "$scheme_prefix"]
buildConfigField("String", "KIND", "\"orange\"")
}
kiwi {
applicationId = "kiwi.company"
dimension "product"
scheme_prefix = "companykiwi"
manifestPlaceholders = [scheme_prefix: "$scheme_prefix"]
buildConfigField("String", "KIND", "\"kiwi\"")
}
com {
dimension "license"
license = "com"
applicationIdSuffix = "$license"
scheme_suffix = ''
manifestPlaceholders = [scheme_suffix: "$scheme_suffix"]
}
eu {
dimension "license"
license = "eu"
applicationIdSuffix = "$license"
scheme_suffix = "eu"
manifestPlaceholders = [scheme_suffix: "$scheme_suffix"]
}
uk {
dimension "license"
license = "uk"
applicationIdSuffix = "$license"
scheme_suffix = "uk"
manifestPlaceholders = [scheme_suffix: "$scheme_suffix"]
}
}
android.applicationVariants.all {
variant ->
ext.fileProvider = variant.applicationId + ".fileprovider"
buildConfigField("String", "FILE_PROVIDER", "\"$fileProvider\"")
def product = variant.productFlavors[0]
def license = variant.productFlavors[1]
variant.buildConfigField("String", "LICENSE", "\"${license.license}\"")
variant.buildConfigField "String", "SCHEME", "\"${product.scheme_prefix}${license.scheme_suffix}\""
}
以下是相应的清单更改:
<activity android:name=".FruitViewerActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="${scheme_prefix}${scheme_suffix}" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>