Can I mix java and scala in one Android project?

Hi,

I am using IntellJ IDEA 13.1.2, community version. And have installed scala and sbt related plugins. I have an Android project which is using Gradle and is working fine. Now I am trying to add Scala support to this project, but I don't want to convert all Java code to Scala. Currently, my project directory looks like:

root/
  src/
    main/
      java/
     scala/
     res/

I have marked "scala" as source folder. After I did so, the IDE say my scala code looks fine, but it complains my java code not being scala like.

Is it possible to mix java code and scala code in one project? How can I do that?


Thanks,
David

10 comments

Can you attach sample Gradle project? I'm pretty sure, that it's possible to mix Java/Scala in one Android project. From your words I didn't understand, what "it complains my java code not being scala like" means.

Best regards,
Alexander Podkhalyuzin.

0

Hi Alexander,

I shared the sample at http://1drv.ms/1jTMiL5. Bascially, I just create a Gradle Android project, then created a .scala file at src/scala directory and build.sbt at project root. Then I installed sbt and scala plugin. Now, the IDE shows my scala file is alright. But when I open the .java files, it complains about syntax error. I think the syntax checker is expecting scala in my .java file.


Thanks,
David

0

Currently, I am also seeing these issues:

  • A warning always there:Warning:Gradle: module 'app-build' won't be compiled. Unfortunately you can't have non-Gradle Java module and Android-Gradle module in one project.
  • Every time I start the IDE and open the project, it notifies me that it found a new module (the scala module) and ask if I want to import it.
0

My project structure looks like below. The "Compiler library" is marked red. How I can fix this? Thanks!
scala-android.png

0

I created a dummy non-sbt scala project and setup the 'scala-compiler' and 'scala-library' global libraries. Then I come back to my Android project and setup the project structure. Now the IDEA does not complain any more. But when I do Ctrl+F9, Gradle complains about unable to find symbols. The symbols are my scala classes. Obviously, it did not include my scala code in the compilation. In my Android project, I don't have the build.sbt file. When I tread Android project that include scala code, should I set the project with Gradle, or SBT?


Thanks,
David

0

The problem is that you are trying to mix Gradle and SBT projects in one Android project (and both are linked in IntelliJ IDEA project, which makes IDEA crazy). I think it's better to try avoid using SBT, see the following project: https://github.com/yareally/android-scala-intellij-no-sbt-plugin

Best regards,
Alexander Podkhalyuzin.

0

Hi Alexander,

I just tried that project. It is an Android project with scala code only. I added a Java code and try to reference the scala code from it. The IDE looks all good. But I got "cannot find symbol" error when trying to compile.

Error:(14, 23) java: ...\android-scala-intellij-no-sbt-plugin\src\com\example\android_scala\TestActivity.java:14: cannot find symbol

symbol  : class MyActivity

location: class com.example.android_scala.TestActivity

It looks like the gradle build system does not know how to handle the situation when java and scala code are mixed.


Thanks,
David

0

If I understand correctly, problem is exactly with mixed Scala/Java Gradle project (Android is irrelevant here). Please read this guide: http://chrismarks.wordpress.com/2013/07/31/compiling-a-mixed-scala-project-with-gradle/. It looks like it can solve your problem.

Best regards,
Alexander Podkhalyuzin.

0

No, it doesn't :(

I got this error:

Error:The 'java' plugin has been applied, but it is not compatible with the Android plugins.

I think the scala plugin depends on java plugin, which is not compatible with android plugin.

0

As our support team suggested there are two ways to solve your problem:

1. Use only SBT: https://github.com/pfn/android-sdk-plugin (it's not supported in IntelliJ IDEA SBT support, you will need to use gen-idea, command line project generator)
2. Use only Gradle. To use it, you need to use special plugin: https://github.com/saturday06/gradle-android-scala-plugin. This plugin is available only from Gradle 1.12.

Additionally Gradle has problems with Scala plugin: http://forums.gradle.org/gradle/topics/enable_to_setup_a_simple_scala_build

Best regards,
Alexander Podkhalyuzin.

0

Please sign in to leave a comment.