• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

tmiyamon/gradle-config: Gradle plugin to add multi-variants yaml settings to And ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

tmiyamon/gradle-config

开源软件地址(OpenSource Url):

https://github.com/tmiyamon/gradle-config

开源编程语言(OpenSource Language):

Groovy 88.4%

开源软件介绍(OpenSource Introduction):

Gradle Config Plugin

This plugin helps you to easily handle variant specific settings with yaml format.

Usage

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.com.tmiyamon:gradle-config:0.2.1"
  }
}

apply plugin: 'com.android.application'
apply plugin: "com.tmiyamon.config"

Build script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:

plugins {
  id "com.tmiyamon.config" version "0.2.1"
}

Settings class

The entries in config yaml will be available in Settings class as static members:

Settings.entry

Nested entries are supported:

Settings.entry.some_entry

Config files

Config entries are generated from:

config/default.yml
config/default_secret.yml
config/${productFlavor}.yml
config/${productFlavor}_secret.yml
config/${buildType}.yml
config/${buildType}_secret.yml

The lower one overwrites upper one deeply. The purpose of loading settings files with suffix _secret is to avoid to commit the secret files to remote repository. If you want to use this feature, don't forget to include those files to your .gitignore:

*_secret.yml

Accessing Configuration Settings

Consider the following config files.

config/default.yml

size: 1
server: google.com

config/debug.yml

size: 2
server: google.com
section:
  size: 3
  servers: [ {name: yahoo.com}, {name: amazon.com} ]

Notice that the variant specific config entries overwrite the common entries.

Settings.size   // => 2
Settings.server // => google.com

Notice that object member notation is maintained even in nested entries.

Settings.section.size // => 3

Notice array notation and object member notation is maintained.

Settings.section.servers.get(0).name // => yahoo.com
Settings.section.servers.get(1).name // => amazon.com

Type

Consider this config file:

string_entry: string
int_entry: 1
double_entry: 1.0
date_entry: 2001-11-23 15:03:17
list_entry:
  - 1
  - 2
failed_list_entry:
  - 1
  - 'test'
object_entry:
  entry_a: 1
  entry_b: 2
list_of_map_entry:
  - a: 'a1'
    b: 'b1'
  - a: 'a2'
    b: 'b2'

The snakeyaml library this plugin uses provides the following basic type conversions:

Integer i = Settings.int_entry;
String s = Settings.string_entry;
Double d = Settings.double_entry;
Date da = Settings.date_entry;
List<Integer> list = Settings.list_entry;

The list with mixed type elements are not supported and throw an exception.

Error:Execution failed for task ':app:generateDevDebugSettings'.
> Not supported list with mixed type: [class java.lang.Integer, class java.lang.String]

The nested map entries are converted a generated class to achieve dot access.

Settings.ObjectEntry oe = Settings.object_entry;
Integer entryA = object_entry.entry_a;
Integer entryB = object_entry.entry_b;

Also, the list of map are supported.

List<Settings.ListOfMapEntryElement> entries = Settings.list_of_map_entry;

Known issues

Not found Settings class

Sometimes Android Studio does not detect generated Settings class. When you fact to this problem, Refresh all Gradle projects in the right pane of Android Studio may be helpful.

License

Copyright 2015 Takuya Miyamoto

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap