Android Build Constants Plugin


Source link: https://github.com/jenzz/gradle-android-buildconstants-plugin

Android Build Constants Plugin

A Gradle plugin for Android which generates both Java and XML constants as part of the build process.

Why do I need this?

In short: To define constants that are accessible from everywhere in your Android app.

In long: There are two ways to define constants in Android. Both have their limitations.

  • Creating constants using buildConfigField only adds entries to BuildConfig.java which means you cannot access them via XML.
  • Creating a custom constants.xml resource file with entries like <string name="constant">constant_value</string> lets you easily reference the constant via @string/constant in XML, but requires you to have a Context object on the Java side to get the value using getResources().getString(R.string.constant).

None of the above solutions is ideal. This plugin lets you specify constants in your build.gradle file that will be translated into both a Java and an XML-based version with the same constant value.

How does it work?

The plugin creates a new task per build type, e.g. generateDebugBuildConstants. It hooks into the build process ahead of the processDebugResources task so it will be executed every time you assemble your project. It supports incremental builds, i.e. if none of the inputs or outputs have changed, Gradle can skip the task (up-to-date).

Usage

The plugin currently supports Integer, Boolean and String data types.

You can specify the constants using a closure in your build.gradle file like this:

buildConstants {

constants {

  aBoolean = true
  aString = 'string'
  aNumber = 123

}
 
}

The plugin will then generate both a Java and an XML version of the constants like so:

Java:

public final class BuildConstants {

public static final boolean ABOOLEAN = true;
public static final String ASTRING = "string";
public static final int ANUMBER = 123;

private BuildConstants() {

  throw new AssertionError("No instances.");

}
 
}

XML:

<resources>  <bool name="aboolean">true</bool>  <string name="astring">string</string>  <integer name="anumber">123</integer> </resources>

Alternatively, you can define the constants using a map:

buildConstants {

constants = [
  aBoolean : true,
  aString : 'string',
  aNumber : 123
] 
}

Or specify just a single constant (similar to buildConfigField):

buildConstants {

constant 'single_constant', 'single_string' 
}

The default generated file names are BuildConstants.java and build_constants.xml. You can change them like this:

buildConstants {

javaFileName 'SampleBuildConstants'
xmlFileName 'sample_build_constants' 
}

Example

Check out the sample project for an example implementation.

Download

Build script snippet for use in all Gradle versions:

buildscript {

repositories {

  maven {

 url 'https://plugins.gradle.org/m2/'
  
}

}

dependencies {

  classpath 'gradle.plugin.com.jenzz:buildconstants:1.1.0'

}
 
}
  apply plugin: 'com.jenzz.buildconstants'

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

plugins {

id 'com.jenzz.buildconstants' version '1.1.0' 
}

License

This project is licensed under the MIT License.

Resources

QuickUtils is a development library for the Android platform. It is intended to make application development easier and consistent through your applications.

Android library project for selecting/capturing multiple images from the device.

Features:

  • Allows taking pictures from camera as well.
  • Multi-selection of images from gallery.
  • Ability to select/capture images upto a specified limit.
  • Preview thumbnails of selected images.
  • No dependency.

It's Gradle Plug-in for Android Play-store Publisher API.

A type-safe interface of image processing algorithms.

Alexei has some predefined image processing calculus that are accessible from the class ImageProcessingThing:

  • Dominant Color
  • Color Palette
  • Average RGB

Blur Navigation Drawer like Etsy app.

Just replace your default toggle with this awesome blurred toggle effect by adding 4 letters!

Bubble Notification with an explosion animation.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes