简体   繁体   中英

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?

我的项目截图在这里

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.

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 !

Regarding Groovy, the answer from Sensei seems to me good.

Regarding Scala you really need to replace Gradle by SBT for the dependency management.

Then you need a plugin.

I have tried two of them:

The second one has a nice tuto, but doesn't work so well with Intellij Idea.

So I am using the first one. It s pretty easy to setup (read the ... read me) and using Java + Scala in the same project doesn't imply any special configuration. The last commit is 6 days old (as of today, the 11/11/13), and the project is two years old so I believe it won't die soon.

I know there is a third plugin to support Android on SBT but as far as I know the dev is stopped.

Hope it will help you.

Regards

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