# A sample build.sbt to allow me easily specify source dirs and libraries?

I recently started learning Android development and I decided to follow the following development processes:

1. Preinstall Scala in emulator
2. Edit source files in IntelliJ IDEA 11
3. Compile the project with sbt in command line
4. Dex generated classes without Scala libraries
5. Build APK and test in emulator

In the above steps, except step 3, I think other steps can be easily handled by command line/makefile. So here I am, I want a sample build.sbt to allow me easily specify the following information:

1. Source directories
2. Reference libraries
3. Output directory

I am aware of sbt android plugin and sbt idea plugin but I want to avoid them for the following reason: projects generated by android plugin dex Scala libraries for each build but I want to have control on that part: I want to skip Scala libraries for development but include them for release build, which seams requires a lot of digging if I do it with the plugin but can be easily handled if it is done from command line/makefile.

If overall what I plan to do makes sense, could someone familiar with sbt provide such a sample build.sbt? I have already spent hours following sbt tutorials but felt it’s so hard to make each seemed simple change to the default behavior of sbt.

A side question, it seems that source directories added by unmanagedSourceDirectories get compiled every time regardless of the time stamp. If that’s the case, what’s the point of using sbt? I can just feed all sources files into scalac.

UPDATE
This is the build.sbt based on Daniel C. Sobral's answer. When I type in sbt compile, it only prints some info like "Set current project to HelloAndroid...". No compilation happens.

name := "HelloAndroid"

scalaVersion := "2.8.2"

unmanagedSourceDirectories := List(
file("src"),
file("gen"))

libraryDependencies := List() // remove Scala's library from dependencies

unmanagedJars := List(Attributed.blank(file("C:/bin/android/platforms/android-10/android.jar")))

target := file("target")


I didn't expect it compiles because I still need to work out the jar files but at least I should get some compiler error. Any hint?

UPDATE: the sbt compile outputs the following:

[info] Set current project to HelloAndroid (in build file:/C:/Users...
[success] Total time: 0 s, completed May 14, 2012 10:36:23 AM


I have two source files in src directory:

src/com/example/[HelloScala.scala MyActivity.java]


No class file is found under target folder.

• A side question, did you consider asking at Stack Overflow? – gnat May 13 '12 at 5:31
• I thought SO is for questions tightly related to coding. My question is more about a particular way of doing programming. – Codism May 13 '12 at 13:55
• fair enough, +1. I'll leave it to moderators to split hairs on how far it is in the gray area :) – gnat May 13 '12 at 15:21
• It would help knowing what did you try and what was wrong with it. – Daniel C. Sobral May 14 '12 at 1:08
• What's the error message? – Daniel C. Sobral May 14 '12 at 15:10

I'd like to understand better what trouble you are having, because there really isn't much to it. This is all pretty simple:

// build.sbt

unmanagedSourceDirectories in Compile := List(file("\path\to\my\source"))

libraryDependencies := List() // remove Scala's library from dependencies

unmanagedJars in Compile := List(Attributed.blank(file("""\path\to\my\library.jar""")))

target in Compile := file("""\path\to\my\target\directory""")


Note that the above completely raw: it doesn't let SBT manage libraries through ivy, it doesn't let SBT find the jar files inside the directories for the unmanaged libraries and it uses absolute paths for everything.

EDIT

As I said, the above use absolute paths, and it seems you want relative paths for your source. Use this:

unmanagedSourceDirectories <<= baseDirectory( base => List("src", "gen") map (base / _ ))


Also, you are using the default target, so you don't need it. However, SBT will create the files inside a subdirectory of that target, which is probably not what you want.

You can change that this way:

target in Compile <<= baseDirectory(_ / "sbt-stuff") // move everything else to sbt-stuff

classDirectory in Compile <<= baseDirectory(_ / "target")  // generate the class files on target


As for the error message you did not understand, it would have been easier to ask what it meant than to ask for the whole configuration.

• Thanks. I was having trouble with "unmanagedJars in Compile:=Seq(file..." and couldn't understand the error. Google didn't provide any hint at that time. – Codism May 14 '12 at 12:52
• Daniel: I updated my question with a build.sbt based on your answer. It does not compile. could you take a look? Thanks – Codism May 14 '12 at 14:08
• Daniel: if I change the configuration to unmanagedSourceDirectories as Compile then it picks up the source files. But as I mentioned in the side question, it compiles the source files regardless of their time stamp. Is that a designed behavior? – Codism May 14 '12 at 20:43
• @Codism No, it only recompiles when changed or if a transitive dependency gets changed. It works here fine, with all the configuration above. – Daniel C. Sobral May 15 '12 at 0:03