GithubHelp home page GithubHelp logo

spring-xu / groovy-android-gradle-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from groovy/groovy-android-gradle-plugin

0.0 2.0 0.0 196 KB

A Gradle plugin to support the Groovy language for building Android apps

License: Apache License 2.0

Groovy 100.00%

groovy-android-gradle-plugin's Introduction

Groovy language support for Android

Build Status

This plugin adds support for writing Android applications using the Groovy language.

Usage

Edit your build.gradle file with the following:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.7'
    }
}

apply plugin: 'groovyx.grooid.groovy-android'

Then you must choose which version and modules of Groovy you use. So far, Android support is available in the 2.4.0 release and beyond, so you need to add the following repository to your build.gradle file:

repositories {
   jcenter() // or mavenCentral()
}

Then you can start using Groovy by adding a dependency on the grooid classifier:

dependencies {
    compile 'org.codehaus.groovy:groovy:2.4.5:grooid'
}

then use the assembleDebug task to test.

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:1.5.0'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.8-SNAPSHOT'
    }
}

Where to put sources?

Groovy sources must be found inside src/main/groovy. Note that the plugin follows the behavior of the Groovy plugin in Gradle, which means that if you want to be able to have Groovy classes referencing Java classes that themselves reference Groovy classes, then those Java classes must be found inside src/main/groovy too (this is called joint compilation).

Writing Groovy code

This plugin has been successfully tested with Android Studio. It will let you write an application in Groovy. It is recommended, for performance, memory and battery life, that you use @CompileStatic wherever possible.

Details can be found on my blog and here for more technical details

Configuring the Groovy compilation options

The default language level for both source and target is 1.6. You can change it by giving some options. Following example changes it to 1.7. Note that you have to change Android Studio project settings as well if you use the IDE.

project.androidGroovy {
    options {
        sourceCompatibility = '1.7'
        targetCompatibility = '1.7'
    }
}

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

project.androidGroovy {
    options {
        configure(groovyOptions) {
            encoding = 'UTF-8'
            forkOptions.jvmArgs = ['-noverify'] // maybe necessary if you use Google Play Services
        }
        sourceCompatibility = '1.7'
        targetCompatibility = '1.7'
    }
}

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 or/and global transformations, Android may complain about those files.

You can simply add the following rule:

packagingOptions {
    exclude 'META-INF/services/org.codehaus.groovy.transform.ASTTransformation'
    exclude 'META-INF/services/org.codehaus.groovy.runtime.ExtensionModule'
}

There is no problem 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 have a runtime exception.

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

groovy-android-gradle-plugin's People

Contributors

melix avatar andrewreitz avatar dmancilla avatar omo avatar songzhw avatar andresviedma avatar dovd-chabad avatar glaforge avatar powerje avatar leonard84 avatar mariogarcia avatar

Watchers

James Cloos avatar SpringXu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.