简体   繁体   中英

Android + Groovy + Android Annotation

i'm playing with android and android-annotiations, and why not play with groovy too... maybe to much :D

AndroidAnnotations, with help of APT (Android Processing Tools) generate helper class, that can be use in source code, and this source code compile before APT create...

Groovy its a great language that can run in Android, and with help of swissknife can create a rapid apps, a few examples out there.

Its possible mix both approachs? I think not, but may i ask :)

See this code...

build.gradle

buildscript {
    repositories {
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
    }
}

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

...

dependencies {
    ...
    compile 'com.github.batuypn:SmartConfigLibrary:v1.0.6'

The code

...
import groovy.transform.CompileStatic

import batuypn.android.smartconfiglibrary.SmartConfigLibrary_
import batuypn.android.smartconfiglibrary.SmartConfigLibrary_.*

// @CompileStatic
class SmartConfigActivity extends Activity implements SmartConfigLibrary_.Callback {
    private SmartConfigLibrary_ smartConfigLibrary;
...

The error

SmartConfigActivity.groovy: 16: unable to resolve class SmartConfigLibrary_.Callback
@ line 16, column 1. class SmartConfigActivity extends Activity implements SmartConfigLibrary_.Callback { ^

1 error

:compileDebugGroovy FAILED

Since Gradle 2.5, you can use "annotation processing" on Groovy source code. This is technically not Groovy processing, because actually Gradle forces the Groovy compiler to create Java stubs, then processes that stubs. The processors will create Java sources, which will be compiled with Groovy in joint compilation. This means you can only use annotations on Groovy constructs which have equivalents in the Java language. But basically this should not be a problem, since processors designed to work with Java. Also you can only use this feature to generate new classes, not modify existing ones, like lombok does in a really hackish way. From a user point of view this Groovy processing should be transparent despite of the technic behind it.

I created an example project which demonstrates this: https://github.com/WonderCsabo/androidannotations/tree/groovyExample/examples/gradle

EDIT: the android-apt plugin now supports Groovy Android processing, so the Gradle configuration is much easier. I changed the example above to utilize android-apt .

You can also add Java sources and process them, the generated classes will be available for Groovy.

In case melix misunderstood you...

If you use annotations on Java code to produce new classes and want to use those in Groovy, then this is possible. An example is using Dagger2 and Groovy code that uses classes generated from annotated Java code.

About the exact problem:

Change this:

class SmartConfigActivity extends Activity implements SmartConfigLibrary_.Callback {
    private SmartConfigLibrary_ smartConfigLibrary;

to this:

class SmartConfigActivity extends Activity implements SmartConfigLibrary.Callback {
    private SmartConfigLibrary smartConfigLibrary;

This way your project can be (almost) built. To fully fix the problem, i suggest to build the project from scratch based on my example .

No. Android Annotations works on Java sources only. For Groovy sources, you must use SwissKnife.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM