Skip to content

Commit 180bb34

Browse files
author
Jens Driller
committed
Initial import
0 parents  commit 180bb34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1262
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Gradle
2+
.gradle
3+
local.properties
4+
build
5+
6+
# Android Studio
7+
.idea
8+
*.iml
9+
10+
# Mac OSX
11+
.DS_Store

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Change Log
2+
==========
3+
4+
Version 1.0.0 *(2016/02/15)*
5+
--------------------------
6+
* Initial release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Jens Driller
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
Android Build Constants Plugin
2+
==============================
3+
A Gradle plugin for Android which generates both Java and XML constants as part of the build process.
4+
5+
Why do I need this?
6+
===================
7+
In short: To define constants that are accessible from everywhere in your Android app and/or library.
8+
9+
In long: There are two ways to define constants in Android. Both have their limitations.
10+
* Creating constants using `buildConfigField` only adds entries to `BuildConfig.java` which means you cannot access them via XML.
11+
* Creating a custom `constants.xml` resource file with entries like `<string name="constant">constant_value</string>`
12+
lets you easily reference the constant via `@string/constant` in XML, but requires you to have a `Context` object on the Java side
13+
to get the value using `getResources().getString(R.string.constant)`.
14+
15+
None of the above solutions is ideal. This plugin lets you specify constants in your `build.gradle` file
16+
that will be translated into both a Java and an XML-based version with the same constant value.
17+
18+
How does it work?
19+
=================
20+
The plugin creates a new task per build type, e.g. *generateDebugBuildConstants*.
21+
It hooks into the build process ahead of the *processDebugResources* task so it will be executed every time you assemble your project.
22+
It supports incremental builds, i.e. if none of the inputs or outputs have changed, Gradle can skip the task (up-to-date).
23+
24+
Usage
25+
-----
26+
The plugin currently supports **Integer**, **Boolean** and **String** data types.
27+
28+
You can specify the constants using a closure in your `build.gradle` file like this:
29+
30+
```groovy
31+
buildConstants {
32+
constants {
33+
aBoolean = true
34+
aString = 'string'
35+
aNumber = 123
36+
}
37+
}
38+
```
39+
40+
The plugin will then generate both a Java and an XML version of the constants like so:
41+
42+
**Java:**
43+
```java
44+
public final class BuildConstants {
45+
public static final boolean ABOOLEAN = true;
46+
public static final String ASTRING = "string";
47+
public static final int ANUMBER = 123;
48+
}
49+
```
50+
51+
**XML:**
52+
```xml
53+
<resources>
54+
<bool name="aboolean">true</bool>
55+
<string name="astring">string</string>
56+
<integer name="anumber">123</integer>
57+
</resources>
58+
```
59+
60+
Alternatively, you can define the constants using a map:
61+
62+
```groovy
63+
buildConstants {
64+
constants = [
65+
aBoolean : true,
66+
aString : 'string',
67+
aNumber : 123
68+
]
69+
}
70+
```
71+
72+
Or specify just a single constant (similar to `buildConfigField`):
73+
74+
```groovy
75+
buildConstants {
76+
constant 'single_constant', 'single_string'
77+
}
78+
```
79+
80+
The default generated file names are `BuildConstants.java` and `build_constants.xml`.
81+
You can change them like this:
82+
83+
```groovy
84+
buildConstants {
85+
javaFileName 'SampleBuildConstants'
86+
xmlFileName 'sample_build_constants'
87+
}
88+
```
89+
90+
Example
91+
-------
92+
Check out the [sample project](https://github.com/jenzz/gradle-android-buildconstants-plugin/tree/master/sample) for an example implementation.
93+
94+
Download
95+
--------
96+
Build script snippet for use in all Gradle versions:
97+
98+
```groovy
99+
buildscript {
100+
repositories {
101+
maven {
102+
url "https://plugins.gradle.org/m2/"
103+
}
104+
}
105+
dependencies {
106+
classpath "gradle.plugin.com.jenzz:plugin:1.0.0"
107+
}
108+
}
109+
110+
apply plugin: "com.jenzz.buildconstants"
111+
```
112+
113+
Build script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:
114+
115+
```groovy
116+
plugins {
117+
id "com.jenzz.buildconstants" version "1.0.0"
118+
}
119+
```
120+
121+
License
122+
-------
123+
This project is licensed under the [MIT License](https://raw.githubusercontent.com/jenzz/gradle-android-buildconstants-plugin/master/LICENSE).

build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
dependencies {
6+
classpath 'com.android.tools.build:gradle:2.0.0-beta4'
7+
}
8+
}
9+
10+
allprojects {
11+
repositories {
12+
jcenter()
13+
}
14+
}
15+
16+
ext {
17+
versionNumber = '1.0.0'
18+
}

gradle.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
15+
# When configured, Gradle will run in incubating parallel mode.
16+
# This option should only be used with decoupled projects. More details, visit
17+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18+
# org.gradle.parallel=true

gradle/wrapper/gradle-wrapper.jar

52.4 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Mon Dec 28 10:00:20 PST 2015
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

gradlew

Lines changed: 160 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)