4

I'd like to do Android development in Clojure, using the Cursive plugin in Android Studio. I've seen several posts where people have stated that it is possible, but I haven't been able to get it working.

When I try to build, I get errors reported where I'm trying to call Clojure from Java:

Error:(6, 18) error: package com.domain does not exist
Error:(15, 49) error: cannot find symbol variable tiny

Here's what I tried:

  1. I already have Cursive running in IntelliJ with leiningen, building and running standalone Clojure apps

  2. Start Android Studio, version 1.5.1

  3. Create a new Android project. Selected API level 15, Empty Activity

  4. Install Cursive plugin, as per this post: Is it possible to get the Cursive Plugin installed with Android Studio?

  5. Add clojure jar to classpath: Open module settings, dependencies, +Library Dependency, search for and select org.clojure:clojure:1.8.0-RC5

  6. Switch from Android view to Project view, to create Clojure src directory: select app/src/main and create new directory clojure

  7. The next step is to create a Clojure source file, which can be called from Java, using the example in this post: Calling clojure from java

  8. Select src directory clojure, right-click New Clojure Namespace and enter: com.domain.tiny (to match the ns used in the example)

  9. Paste the code from the example above into the new file (note that the ns in the new file was tiny and should have been com.domain.tiny)

  10. It will say File tiny.clj is not under a source root so click on Add source root. Now we have both src/java and src/clojure set as src roots

  11. Build the project. At this point it looks like everything is happy, no syntax errors in either the Java or Clojure code. We can deploy and run the (almost) empty Android project, and run a local REPL to load and test our Clojure code.

  12. We want to call some Clojure code from Java, so add the following code to MainActivity (again, from the example above):

    System.out.println("(binomial 5 3): " + tiny.binomial(5, 3));
    System.out.println("(binomial 10042, 111): " + tiny.binomial(10042, 111));
    
  13. Android Studio can find the package com.domain.tiny and the tiny class, no syntax errors, so looks good

  14. Now try and build the project - at this point I get an error:

    Error:(6, 18) error: package com.domain does not exist
    Error:(15, 49) error: cannot find symbol variable tiny
    

At this point, I'm stuck. I'm guessing that even though the compiler is seeing the Clojure code ok, there's still something missing in the build process. Does leiningen need to be involved? If so, I don't know how, or what I might have to put in the project.clj configuration file.

Anyone have any ideas? Thanks!

Community
  • 1
  • 1
Steve Moseley
  • 5,797
  • 2
  • 19
  • 22

1 Answers1

1

Edit:

This appears to not be possible right now. This is because Android Studio requires projects to use Gradle, and the Clojure Gradle plugin is incompatible with the Android one. This may work in the future, but we couldn't make it work right now.

Original answer:

Thanks for the detailed issue. I guess that your Clojure code has to be AOT compiled for this to work. To enable this, check the settings under Settings->Build, Execution, Deployment->Compiler->Clojure Compiler. If you enable compilation for your Clojure namespaces, does that help?

Colin
  • 1,112
  • 2
  • 7
  • 16
  • Didn't seem to help. BTW, in Android Studio 1.5.1, the Clojure compiler settings are under: Other Settings->Clojure Compiler. I set two things there, "Compile all Clojure namespaces" and "Compile Clojure files before Java". Anything else I should have done after changing those settings, prior to "Build Clean", "Make Project" ? – Steve Moseley Jan 15 '16 at 01:57
  • This has also been raised as an issue with Cursive, here: https://github.com/cursive-ide/cursive/issues/1218 – Steve Moseley Jan 15 '16 at 19:38
  • I'm trying a different approach using the Graclj plugin for Gradle, inside Android Studio, to see if I can integrate Clojure code compilation into the Android build process. If I can get this working, maybe it'll help. http://stackoverflow.com/questions/35965305/how-do-i-use-clojure-in-android-studio-using-graclj – Steve Moseley Mar 13 '16 at 01:25