groovy-android-gradle-plugin


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

Groovy language support for Android

This plugin adds Groovy Language support for Android applications and libraries.

Quick Start

Use a lazybones template from grooid-template

Usage

Edit your build.gradle file to contain the following:

buildscript {

repositories {

  jcenter()

}

 dependencies {

  classpath 'com.android.tools.build:gradle:2.3.2'
  classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.2.0'

}
 
}
  apply plugin: 'com.android.application' apply plugin: 'groovyx.android'

The latest version of the Groovy Android Plugin can be found here

Then you must choose which version of Groovy you use. Android support is available in starting at the 2.4.x releases. You will need to add the following repository to your build.gradle file:

repositories {

jcenter() 
}

Then you can start using Groovy by adding the groovy dependency with the grooid classifier:

dependencies {

compile 'org.codehaus.groovy:groovy:2.4.12:grooid' 
}

Full list of releases can be found here here. Then use the assembleDebug gradle task to test out your build and make sure everything compiles.

Should you want to test development versions of the plugin, you can add the snapshot repository and depend on a SNAPSHOT:

buildscript {

repositories {

  jcenter()
  maven {
 url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' 
}

}
 
}

dependencies {

  classpath 'com.android.tools.build:gradle:3.0.0'
  classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:2.0.0-SNAPSHOT'

}
 
}

Go here to see what the latest SNAPSHOT version is.

Where to put sources?

Groovy sources may be placed in src/main/groovy, src/test/groovy, src/androidTest/groovy and any src/${ buildVariant } /groovy configured by default. A default project will have the release and debug variants but these can be configured with build types and flavors. See the android plugin docs for more about configuring different build variants.

Extra groovy sources may be added in a similar fashion as the android plugin using the androidGroovy.sourceSets block. This is especially useful for sharing code between the different test types, and also allows you to add Groovy to an existing project. For example

androidGroovy {

sourceSets {

  main {

 groovy {

srcDirs += 'src/main/java'

 
}

  
}

}
 
}

would add all of the Java files in src/main/java directory to the Groovy compile task. These files will be removed from the Java compile task, instead being compiled by GroovyC, and will allow for the Java sources to be referenced in the Groovy sources (joint compilation). Please note, that you may need to also add these extra directories to the Java source sets in the android plugin for Android Studio to recognize the Groovy files as source.

Writing Groovy Code

This plugin has been successfully tested with Android Studio and will make no attempts to add support for other IDEs. This plugin will let you write an application in Groovy but it is recommended, for performance, memory and battery life, that you use @CompileStatic wherever possible.

Details can be found on Melix’s blog and here for more technical details

Including Groovy Libraries

In order to include libraries written in groovy that include the groovy or groovy-all jars, you will need to exclude the groovy dependency allowing the grooid jar to be the one to be compiled against.

For example to use the groovy-xml library you would simply need to do exclude the group org.codehaus.groovy.

compile ('org.codehaus.groovy:groovy-xml:2.4.3') {

  exclude group: 'org.codehaus.groovy' 
}

Skipping Groovy Compile

As of version 1.2.0 only build types/build flavors with groovy sources included in them will have the groovy compile task added. If you would like to skip the groovy compilation tasks on older versions or on newer version wish to skip them in build types that have groovy sources you can use the following to disable the groovy compiler task.

tasks.whenTaskAdded {
 task ->
if (task.name == 'compileDebugGroovyWithGroovyc') {
 (1)
  task.enabled = false

}
 
}
  1. Disables groovy compilation only for the debug build type, simply replace compileDebugGroovyWithGroovyc with whichever compilation task you would like skip to disable it.

Configuring the Groovy compilation options

The Groovy compilation tasks can be configured in the androidGroovy block using the options block:

androidGroovy {

options {

  configure(groovyOptions) {

 encoding = 'UTF-8'

 forkOptions.jvmArgs = ['-noverify'] // maybe necessary if you use Google Play Services
  
}

}
 
}

See GroovyCompile for more options. See Example Application for an example of using these settings to enable custom compilation options.

Only Use GroovyC

For integration with plain java projects or for working with generated files (such as BuildConfig) it may be desirable to only have GroovyC run in order to have Java files reference Groovy files. This is roughly the equivalent of placing all java source files into the groovy source directory (including auto generated files like BuildConfig). In order to only have GroovyC run simply set the flag skipJavaC in the androidGroovy block to true.

androidGroovy {

skipJavaC = true 
}

Annotation Processing

As of 1.2.0 Release annotation processing is configured by default.

Previous versions would require javaAnnotationProcessing to be set to true.

androidGroovy {

options {

  configure(groovyOptions) {

 javaAnnotationProcessing = true
  
}

}
 
}

Android packagingOptions

Groovy Extension Modules and Global transformations both need a file descriptor in order to work. Android packaging has a restriction related to files having the same name located in the same path.

If you are using several Groovy libraries containing extension modules and/or global transformations, Android may complain about those files.

You can simply add the following rule:

android {

packagingOptions {

 exclude 'META-INF/services/org.codehaus.groovy.transform.ASTTransformation'

 exclude 'META-INF/services/org.codehaus.groovy.runtime.ExtensionModule'

}
 
}

There are no problems excluding global transformation descriptors because those are only used at compile time, never at runtime.

The problem comes with module extensions. Unless you statically compile classes using extension modules with @CompileStatic they won’t be available at runtime and you’ll get a runtime exception.

There is an alternative. The emerger gradle plugin will add excludes for you and merges all extension module descriptors into a single file which will be available at runtime.

Resources

Asynchronous Workers and Worker Managers for Android.

KVS Schema is a code generation library to manage key-value data for Android.

A 3d database ORM experiment for Android.

A bottom sliding menu with gradient background and transparency.

Loading is a library project with kinds of Android loading views.

RingButton is a button with two parts. Each part has its text, icon and click event.

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