An easy-to-use sbt plugin for working with all Android projects
An easy-to-use sbt plugin for working with all Android projects
Auto-import from gradle using sbt-android-gradle
NOTE: 1.6.0 is the last version published using
addSbtPlugin("com.hanhuy.sbt" % "android-sdk-plugin" % "1.6.0"),
all future updates can be accessed by using
addSbtPlugin("org.scala-android" % "sbt-android" % VERSION)
This is an easy-to-use plugin for existing and newly created android projects. It requires SBT 0.13.8+
The plugin supports all android projects configurations. 3rd party libraries
can be included by placing them in libs, or they can be added by using sbt's
libraryDependencies feature. This build plugin is 100% compatible with
the standard Android build system due to the use of the same underlying
com.android.tools.build:builder implementation.
NOTE: proguard 5.1 does not like old versions of scala. Projects that wish
to use Proguard 5.1 or newer with Scala should use scalaVersion := "2.11.5"
or newer. For compatible scala projects and java-based projects which wish to
use proguard 5.1 (to fix issues around generic types being removed from
base-classes) a workaround is to add this setting into your build.sbt:
proguardVersion := "5.1".
See proguard bug #549 and
SI-8931
NOTE: support-v4 22.2.x triggers compilation errors, see #173 and SI-7741
The first line of support is reading this README, beyond that, help can be found on the #sbt-android IRC channel on Freenode, or the scala-android/sbt-android gitter
sbt scripted, they require a device
or emulator to be running in order to pass.build.properties and auto_plugins.sbt
files that set the current version of sbt and the sbt-android to use
for testing.Install sbt (from scala-sbt.org or use your local packaging system like macports, brew, etc.)
~/.sbt/0.13/plugins/android.sbt:addSbtPlugin("org.scala-android" % "sbt-android" % "1.7.7")Set the environment variable ANDROID_HOME pointing to the path where the
Android SDK is installed. If ANDROID_HOME is not set, an Android SDK
will be installed automatically at ~/.android/sbt/sdk. If any components
are missing from your SDK, they will be installed automatically.
ANDROID_NDK_HOME if NDK building is desired and an NDK
already installed. If neither are set, or an NDK is not installed, an
NDK will be installed to ~/.android/sbt/sdk/ndk-bundle automatically
if an NDK build is detected (Android.mk and friends)(N/A if globally configured) Create a directory named project within
your project and add the file project/plugins.sbt, in it, add the
following line:
addSbtPlugin("org.scala-android" % "sbt-android" % "1.7.7")Create a new android project using gen-android if the plugin is installed
globally
sbt gen-android-sbt to make sure everything is properly setup
in an existing project.Create or edit the file named build.sbt and add the
following line, (automatically performed if using gen-android) :
enablePlugins(AndroidApp)(OPTIONAL) Select the target platform API you're building against in build.sbt,
if not selected, the newest available will be selected automatically:
// for Android 7.0, Nougat, API Level 24:
platformTarget := "android-24"The Android Developer pages provides a list of applicable version codes.
Now you will be able to run SBT, some available commands in sbt are:
compileandroid:package-releaseandroid:package-debugandroid:packagedebugandroid:testandroid:installandroid:runandroid:uninstall~. ~ android:package-debug
will continuously build a debug build any time one of the project's
source files is modified.If you want sbt-android to automatically sign release packages
add the following lines to local.properties (or any file.properties of
your choice that you will not check in to source control):
key.alias: KEY-ALIASkey.alias.password: PASSWORD (optional, defaults to key.store.password)key.store: /path/to/your/.keystorekey.store.password: KEYSTORE-PASSWORDkey.store.type: pkcs12 (optional, defaults to jks)IDE integration
Android Support and Scala
plugins are installed.Run command to execute an SBT
android:package task instead of Make (remove the make entry); this is
found under Run Configurations.Scala plugin is still required for non-Scala projects in order to
edit sbt build files from inside the IDE.sbt-android. The process generally works well, however there
are still several caveats:idea-sbt-plugin is still required to actually perform the buildaar resources do not show up in editor or autocomplete automaticallyProject Structure -> Modules -> + -> Import Module$HOME/.android/sbt/exploded-aars/AAR-PACKAGE-FOLDERCreate from existing sourcesNext all the until to the Finish button, finish.Dependencies tab for the Module you want to be able to
access the AAR resources, click + -> Module Dependency$HOME/.IntelliJVERSION/config/plugins/scala/launcher/sbt-structure-0.13.jarConsuming apklib and aar artifacts from other projects
apklib() or aar()apklib() and aar() is only necessary if the pom
packaging for the dependency is not apklib or aarlibraryDependencies += aar("groupId" % "artifactId" % "version", "optionalArtifactFilename")transitiveAndroidLibs := falseapklib and aar that transitively depend on apklib and aar will
automatically be processed. To disable set
transitiveAndroidLibs := falseunmanagedJars in Compile ~= {
_ filterNot (_.data.getName startsWith "android-support-v4")
}Using the google gms play-services aar:
libraryDependencies +=
"com.google.android.gms" % "play-services" % "VERSION"Generating aar artifacts
aar
artifact simply change the enablePlugins(AndroidApp)
line to enablePlugins(AndroidLib)Multi-project builds
exportJars := true.
Android projects automatically set this variable.proguardScala := trueConfiguring sbt-android by editing build.sbt
useProguard := true to enable proguard. Note: if you
disable proguard for scala, you must specify uses-library on a
pre-installed scala lib on-device or enable multi-dex.android: at the
sbt shellConfiguring proguard, some options are available
proguardOptions ++= Seq("-keep class com.foo.bar.Baz") -
will tell proguard not to obfuscute nor optimize code (any valid proguard
option is usable here)proguardConfig ... can be used to replace the entire
proguard config included with sbt-android
proguardConfig -= "-dontobfuscate"proguardConfig -= "-dontoptimize"On-device testing, use android:test and see
Android Testing Fundamentals
Unit testing with robolectric and Junit (use the test task), see how
it works in the
robo-junit-test test case
fork in Test := true otherwise the classloading black
magic in robolectric will fail.Device Management
devices and device are implemented. The former lists
all connected devices. The latter command is for selecting a target
device if there is more than one device. If there is more than one
device, and no target is selected, all commands will execute against the
first device in the list.android:install, android:run and android:test are tasks that can
be used to install, run and test the built apk on-device, respectively.Full list of sbt-android added commands, all commands have full tab
completion w
No open issues yet, or sync has not completed.