简体   繁体   English

如何将Scala,Groovy和Java代码与Gradle结合使用?

[英]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. 最近我正在尝试用Android Studio制作一些Android应用程序。

Does the built-in Gradle plugin support using a combination of Java, Groovy, and Scala code for building an Android app? 内置的Gradle插件是否支持使用Java,Groovy和Scala代码组合来构建Android应用程序?

Looking at the the gradle-android wiki, I've found that sourceSet does not work with Android Studio (get "R redefined" error). 看看gradle-android wiki,我发现sourceSet不适用于Android Studio(得到“R redefined”错误)。

I'm using the default Gradle Wrapper way in Android Studio. 我在Android Studio中使用默认的Gradle Wrapper方式。

How do I use a mix of Scala, Groovy, and Java code in my Android Studio project? 如何在Android Studio项目中混合使用Scala,Groovy和Java代码?

我的项目截图在这里

I've tried to add some directives to build.gradle (like using apply plugin and sourceSets ), but they don't seem to work. 我试图在build.gradle中添加一些指令(比如使用apply pluginsourceSets ),但它们似乎不起作用。

I think changing some code like homeactivity.java into homeactivity.groovy might work, but I've not found the way to do this yet. 我认为将homeactivity.java等一些代码更改homeactivity.groovy可能会有效,但我还没有找到办法。

When I uncomment the sourceSets block, I get this error message: 当我取消注释sourceSets块时,我收到以下错误消息:

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. 好吧,这可能不是您正在寻找的答案,但AFAIK,将Groovy移植到Android已经尝试了两次并且从未完成。 There was a GSoC proposition this summer, but I don't know if it has been picked up. 今年夏天有一个GSoC主张 ,但我不知道它是否已被选中。 Which means no groovy code can run on a Dalvik VM just yet, and of course gradle cannot do anything about that. 这意味着没有Groovy代码可以在Dalvik VM上运行,当然gradle也无法做任何事情。

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. 我对Scala了解不多,但看起来Scala代码可以编译为在Dalvik上运行,所以你应该有更多的运气。 Look at this , for example, to use the Scala Build Tool to build for Android. 例如,看看这个 ,使用Scala Build Tool为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. 编辑:这需要编辑,因为最近事情发生了变化:最后一个gr8conf看到CédricChampeau引入了grooid ,允许在android上运行groovy代码。 Yay ! 好极了 !

Regarding Groovy, the answer from Sensei seems to me good. 关于Groovy,Sensei的答案对我来说似乎很好。

Regarding Scala you really need to replace Gradle by SBT for the dependency management. 关于Scala,你真的需要用SBT替换Gradle来进行依赖管理。

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. 第二个有一个很好的tuto,但与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. 它很容易设置(阅读...阅读我)并在同一个项目中使用Java + Scala并不意味着任何特殊配置。 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. 最后一次提交是6天(截至今天,11/11/13),该项目已有两年了,所以我相信它不会很快消失。

I know there is a third plugin to support Android on SBT but as far as I know the dev is stopped. 我知道在SBT上有第三个支持Android的插件,但据我所知,dev已经停止了。

Hope it will help you. 希望它会对你有所帮助。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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