Gradle Building Schema动态地从组合风味维度

时间:2018-01-26 09:30:00

标签: android gradle android-gradle-3.0 android-flavordimension

我们有两个不同的风味维度,如本例所示,我们希望动态生成像

这样的applicationId
  

com.company.apple

相反,此脚本将生成

的applicationId
  

null.company.apple

我相信这是因为底部的函数遍历完整的productFlavor列表而不是组合列表。

flavorDimensions "product", "license"

productFlavors.all {
    ext.scheme = null
    ext.scheme_tail = ""
    ext.kind = null
    ext.license = null
}

productFlavors {
    apple {
        dimension "product"
        kind = "apple"
        scheme = "companyapple"
    }

    orange {
        dimension "product"
        scheme = "companyorange"
        kind = "orange"
    }

    kiwi {
        dimension "product"
        scheme = "companykiwi"
        kind = "kiwi"
    }

    com {
        dimension "license"
        license = "com"
        scheme_tail = ''
    }

    eu {
        dimension "license"
        license = "eu"
        scheme_tail = "eu"
    }

    uk {
        dimension "license"
        license = "uk"
        scheme_tail = "uk"
    }

}

productFlavors.all {
    applicationId license + ".company." + kind
    ext.fileProvider = applicationId + ".fileprovider"
    manifestPlaceholders = [scheme: "$scheme$scheme_tail", fileProvider: "$fileProvider"]
    buildConfigField("String", "FILE_PROVIDER", "\"$fileProvider\"")
    buildConfigField("String", "SCHEME", "\"$scheme$scheme_tail\"")
    buildConfigField("String", "KIND", "\"$kind\"")
    buildConfigField("String", "LICENSE", "\"$license\"")
}

如何迭代组合列表以构建我的buildConfig字段和applicationIds?

manifestPlaceholders的使用方式如下:

    <activity android:name=".FruitViewerActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <data android:scheme="${scheme}" />

            <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="${fileProvider}"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />

    </provider>

谢谢, 罗里

2 个答案:

答案 0 :(得分:1)

我找到了一个可以实现目标的解决方案,然后使用this post by David Medenjak中的信息对其进行了改进。

首先使用applicationId和applicationIdSuffix构建applicationId。 为BuildConfig和AndroidManifest单独构建文件提供程序。 分别为AndroidManifest发布scheme_prefix和scheme_suffix,并在以后连接它们。 从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}\""
}

以下是相应的Manifest更改:

    <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>

答案 1 :(得分:0)

apple {
    dimension "product"
    kind = "apple"
    licence = "apple"
    scheme = "companyapple"
}

您遗失的许可证的风格因此默认为null