android - Gradle Building Schema 动态地从组合的 flavor 维度

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

我们有两个不同的 flavor 维度,就像在这个例子中一样,我们想要像这样动态生成 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>

谢谢, 罗里

最佳答案

我找到了一个可以实现我的目标的解决方案,然后利用来自 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}\""
}

以下是相应的 list 更改:

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

关于android - Gradle Building Schema 动态地从组合的 flavor 维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48458655/

相关文章:

java - 在代码中设置String的资源ID

java - 在列表中加载静态图像会减慢滚动速度

android - 如何找到比例以在尺寸文件夹中保持不同的尺寸

android - Gradle:如何将现有的完整项目包含为子项目?

android - 找不到 com.android.tools.build :gradle:4. 0.1

android - 添加编译 'com.google.android.gms:play-services:11.0.1'时出错

android - Qt Android输出APK在〜/android-build\build\outputs\apk\debug中生成

Android:禁用屏幕点击按钮

gradle - Gradle-launch4j-在Proguard之后包装 jar