摇篮Android插件 - 添加自定义属性的味道?自定义、摇篮、插件、属性

2023-09-03 21:51:29 作者:青春微凉不悲伤

有没有什么办法可以添加自定义属性productFlavor或buildType在Android插件的摇篮? 我想在buildVariants的配置,例如更大的灵活性,这样我可以buildVariants指定任务时检查我的自定义属性。

Is there any way to add custom attributes to productFlavor or buildType in android plugin for gradle? I'd like to have such additional flexibility in configuration of buildVariants, so that I can check my custom property when specifying tasks for buildVariants.

productFlavors {
    flavorGroups "drm", "storeType"
    googlePlay {
        flavorGroup "storeType"
        buildConfig "public static final String TARGET_STORE = \"google\";"
    }
    samsungApps {
        flavorGroup "storeType"
        buildConfig "public static final String TARGET_STORE = \"samsung\";"
    }

    platformDrm {
        flavorGroup "drm"
    }

    widevineAppDrm {
        flavorGroup "drm"
        minSdkVersion 9
        useWidevineAppDrmLib true
    }

}

所以在这里你可以看到我已经添加了自定义属性useWidevineAppDrmLib打造的味道。 这将是很好看到buildVariant.mergedFlavor相同的属性,这样我可以检查的属性值,也增建任务,如包的其他.so文件时,该属性设置为true:

so here you can see I've added custom attribute "useWidevineAppDrmLib" to build flavor. It would be nice to see the same attribute in buildVariant.mergedFlavor, so that I can check that attribute value and do build additional tasks, such as package additional .so files when the attribute is set to true:

android.applicationVariants.each {变种 - > 如果(variant.mergedFlavor.useWidevineAppDrmLib){   ... //添加副本。所以任务   } }

android.applicationVariants.each { variant -> if(variant.mergedFlavor.useWidevineAppDrmLib ) { ... // add copy .so task } }

也许有一种方法可以做到这一点了,但是我没有发现出来呢? 检查构建子(风味名)变量名对我的作品,但它看起来很脏。

maybe there is a way to do that already but I didn't find out yet... checking build variant name for substring (flavor name) works for me, but it looks dirty.

在理想情况下,我想有不同的类型buildType和productFlavor自定义属性的映射。

Ideally I'd like to have a map of custom attributes of different types for buildType and productFlavor.

推荐答案

有不需要创建自己的自定义类的替代解决方案。你可以利用已有的额外属性它已经连接到productFlavors类。这是一个特别设计,让用户自定义的变量。

There's an alternative solution which doesn't require the creation of your own custom class. You can utilize the already existing extras property which is already attached to the 'productFlavors' class. This was specifically designed to allow custom user defined variables.

android {
  // We can add any custom variable so long as it's prefaced with ext
  productFlavors {
    widevineAppDrm {
      ext.useWidevineAppDrmLib = true
    }
  }
}

然后你就可以在以后引用像这样。请注意,如果您没有添加自定义变量到其他 productFlavors 您需要请务必首先检查它,否则摇篮会抱怨没有找到该属性。

Then you can later reference like so. Note, if you didn't add the custom variable to other productFlavors you'll need to be sure to check for it first, else gradle will complain about not finding the property.

android.applicationVariants.each { variant ->
    if (variant.productFlavors[0].ext.has("useWidevineAppDrmLib")) {
        if (variant.productFlavors.get(0).ext.useWidevineAppDrmLib) {
            ...
        }
    }
}