Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a combination of Scala, Groovy, and Java code with Gradle?

Recently I'm trying to make some Android apps with Android Studio.

Does the built-in Gradle plugin support using a combination of Java, Groovy, and Scala code for building an Android app?

Looking at the the gradle-android wiki, I've found that sourceSet does not work with Android Studio (get "R redefined" error).

I'm using the default Gradle Wrapper way in Android Studio.

How do I use a mix of Scala, Groovy, and Java code in my Android Studio project?

My Project Screenshot here

I've tried to add some directives to build.gradle (like using apply plugin and sourceSets), but they don't seem to work.

I think changing some code like homeactivity.java into homeactivity.groovy might work, but I've not found the way to do this yet.

When I uncomment the sourceSets block, I get this error message:

Gradle: A problem occurred evaluating project ':AgricolaScore'.
> Could not find method groovy() for arguments [build_3cmthdr3hv9648pq38oficf549$_run_closure3_closure6_closure8_closure10@15f565e7] on source set main.
like image 559
Gohan Avatar asked Jul 20 '13 09:07

Gohan


People also ask

How do I run Scala with gradle?

From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 5: Scala as implementation language. Next you can choose the DSL for writing buildscripts - 1 : Groovy or 2: Kotlin .

Does gradle work with Scala?

Gradle supports version 1.6. 0 of Zinc and above. The Zinc compiler itself needs a compatible version of scala-library that may be different from the version required by your application. Gradle takes care of specifying a compatible version of scala-library for you.

Does Gradle requires a Java JDK or JRE and Groovy to be installed?

Gradle requires a Java JDK or JRE to be installed, version 6 or higher (to check, use java -version ). Gradle ships with its own Groovy library, therefore Groovy does not need to be installed. Any existing Groovy installation is ignored by Gradle. Gradle uses whatever JDK it finds in your path.


1 Answers

Well, this might not be the answer you're looking for, but AFAIK, the porting of Groovy to Android has been tried twice and never completed. There was a GSoC proposition this summer, but I don't know if it has been picked up. Which means no groovy code can run on a Dalvik VM just yet, and of course gradle cannot do anything about that.

I don't know much about Scala, but it looks like Scala code can be compiled to run on Dalvik, so you should have more luck there. Look at this, for example, to use the Scala Build Tool to build for Android.

EDIT: This needs to be edited since things have changed just recently: the last gr8conf saw the introduction of grooid by Cédric Champeau, which allows to run groovy code on android. Yay !

like image 180
sensei Avatar answered Oct 18 '22 16:10

sensei