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

Simple and light library to do validate observable fields with MVVM for android

Each ValidatedObservableField work like ObservableField, but it have 3 observables properties:

  • value - contains the value of type T
  • valid - boolean true if ALL Rules are valid
  • errorMessage - contains NULL or errorMessage from first invalid Rule

Rules are validated one by one (the order is important), the first invalid Rule will break the chain and set errorMessage. Rules are validated onPropertyChange. You can create your own rules using Rule interface. You can use single Rule or many by RuleCommand.

Trianglify is an Android library that helps creates views with beautiful patterns. Trianglify is based on MVP architecture and licensed under MIT license.

Simple threading library using annotations for Android. This library makes it very easier to do any task on any thread. You can simply annotate a method to execute on any particular task and you are ready to go.

Clean MVP Architecture with Dagger2 + Retrofit2 + Fresco + GenericRecyclerAdapter for Kotlin.

This repo contains demo module which retrieves mars images from the NASA API. The main purpose of the repo is to reduce the time spent on starting a new project or adding new modules to your existing project. You can add new modules with just 2-3 clicks without writing any code.

This application is example of Android Architecture Components which implements MVVM Pattern.

Configurable typing indicator for Android

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