j2emanue
j2emanue

Reputation: 62519

Groovy in Android - how to run Hello world

I'm having trouble referencing a groovy class i've created. Here are the steps:

my build.gradle file has all required dependencies:

apply plugin: 'com.android.application'

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

android { compileSdkVersion 22 buildToolsVersion "21.1.2"

packagingOptions {
    // workaround for http://stackoverflow.com/questions/20673625/android-gradle-plugin-0-7-0-duplicate-files-during-packaging-of-apk
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/groovy-release-info.properties'
}

defaultConfig {
    applicationId "com.example.uen.myrxjavaandroidproject"
    minSdkVersion 17
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.netflix.rxjava:rxjava-android:0.20.7'
    compile 'com.squareup:otto:1.3.5'
    compile 'org.codehaus.groovy:groovy:2.4.3:grooid'
}

And gradle is synched successfully.

then i created a folder under main called 'groovy' such that my project structure looks like the following:

enter image description here

notice i have a groovy class called "Hello", it looks like this:

public class Hello {
String name;

public void sayHello() {
    System.out.println("Hello "+getName()+"!");
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

}

now in my MainActivity.java class i try to instantiate Hello class but its not being recognized. I am doing this:

Hello myHello = new Hello();

what am i doing wrong ?

Upvotes: 0

Views: 248

Answers (1)

melix
melix

Reputation: 1530

If you want to use a Groovy class from your Java sources, then your Java file must be found in the "groovy" directory. It's done this way to enforce joint compilation (Java classes consuming Groovy classes or the other way).

And independently of this, for the sake of Grooviness, your Hello Groovy class can be reduced to:

class Hello {
   String name
   void sayHello() {
       println "Hello $name!"
   }
}

Upvotes: 4

Related Questions