Current version is 1.6.7 (Change log)
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 is tested and developed against 0.13.6+.
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
- A growing collection of tests can be found under sbt-test/android-sdk-plugin/. These projects are examples of how to use the plugin in various configurations.
- Testing the plugin can be run via
sbt scripted, they require a device or emulator to be running in order to pass. - All tests have auto-generated
build.propertiesandauto_plugins.sbtfiles 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.)
- (OPTIONAL) Install the plugin globally by adding the following line
in the file
~/.sbt/0.13/plugins/android.sbt:
addSbtPlugin("org.scala-android" % "sbt-android" % "1.6.7") - (OPTIONAL) Install the plugin globally by adding the following line
in the file
-
Set the environment variable
ANDROID_HOMEpointing to the path where the Android SDK is installed. IfANDROID_HOMEis 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.- (OPTIONAL) Set
ANDROID_NDK_HOMEif 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-bundleautomatically if an NDK build is detected (Android.mk and friends)
- (OPTIONAL) Set
-
Create a new android project using
gen-androidif the plugin is installed globally- Instead of creating a new project, one can also do
sbt gen-android-sbtto make sure everything is properly setup in an existing project.
- Instead of creating a new project, one can also do
-
(N/A if globally configured) Create a directory named
projectwithin your project and add the fileproject/plugins.sbt, in it, add the following line:addSbtPlugin("org.scala-android" % "sbt-android" % "1.6.7") -
Create or edit the file named
build.sbtand add the following line, (automatically performed if usinggen-android) :androidBuild -
Now you will be able to run SBT, some available commands in sbt are:
compile- Compiles all the sources in the project, java and scala
- Compile output is automatically processed through proguard if there are any Scala sources, otherwise; it can be enabled manually.
android:package-release- Builds a release APK and signs it with a release key if configured
android:package-debug- Builds a debug APK and signs it using the debug key
android:package- Builds an APK for the project of the last type selected, by default
debug
- Builds an APK for the project of the last type selected, by default
android:test- run instrumented android unit tests
android:install- Install the application to device
android:run- Install and run the application on-device
android:uninstall- Uninstall the application from device
- Any task can be repeated continuously whenever any source code changes
by prefixing the command with a
~.~ android:package-debugwill continuously build a debug build any time one of the project's source files is modified.
-
If you want
sbt-androidto automatically sign release packages add the following lines tolocal.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 tokey.store.password)key.store: /path/to/your/.keystorekey.store.password: KEYSTORE-PASSWORDkey.store.type: pkcs12(optional, defaults tojks)
-
IDE integration
- The recommended IDE is IntelliJ, not Android Studio. However Android Studio can be used with some massaging (i.e install the Scala Plugin).
- When loading a project into IntelliJ, it is required that the
AndroidandScalaplugins are installed - To ensure proper building, configure the IDE
Runcommand to execute an SBTandroid:packagetask instead ofMake(remove the make entry); this is found underRun Configurations - The SBT plugin for IntelliJ is the one from orfjackal/idea-sbt-plugin
- The
Scalaplugin is still required for non-Scala projects in order to edit sbt build files from inside the IDE. - IntelliJ 14 and newer now includes native support for importing projects
from
sbt-android. The process generally works well, however there are still several caveats:- The
idea-sbt-pluginis still required to actually perform the build aarresources do not show up in editor or autocomplete automatically- They can be added manually, but must be added everytime the project is refreshed from SBT (SBT toolwindow -> Refresh)
- To add:
Project Structure->Modules->+->Import Module$HOME/.android/sbt/exploded-aars/AAR-PACKAGE-FOLDERCreate from existing sourcesNextall the until to theFinishbutton, finish.- Go to the
Dependenciestab for the Module you want to be able to access the AAR resources, click+->Module Dependency - Select the newly added AAR module above, and it will now be visible.
- Steps 5 and 6 will need to be repeated any time the build description is refreshed (SBT toolwindow -> refresh)
- The
-
Consuming apklib and aar artifacts from other projects
- Optionally use
apklib()oraar()- specifying
apklib()andaar()are only necessary if there are multiple filetypes for the dependency, such asjar, etc.
- specifying
libraryDependencies += apklib("groupId" % "artifactId" % "version", "optionalArtifactFilename")- Basically, wrap the typical dependency specification with either apklib() or aar() to consume the library
- If aars or apklibs are duplicately included in a multi-project build,
specify
transitiveAndroidLibs := false apklibandaarthat transitively depend onapklibandaarwill automatically be processed. To disable settransitiveAndroidLibs := false
- Sometimes library projects and apklibs will incorrectly bundle
android-support-v4.jar, to rectify this, add this setting, repeat for any
other incorrectly added jars:
unmanagedJars in Compile ~= { _ filterNot (_.data.getName startsWith "android-support-v4") }
- Optionally use
-
Using the google gms play-services aar:
libraryDependencies += "com.google.android.gms" % "play-services" % "VERSION" -
Generating apklib and/or aar artifacts
- To specify that your project will generate and publish either an
aarorapklibartifact simply change theandroid.Plugin.androidBuildline to one of the variants that will build the desired output type.- For
apklibuseandroid.Plugin.androidBuildApklib - For
aaruseandroid.Plugin.androidBuildAar
- For
- Alternatively, use
android.Plugin.buildAarand/orandroid.Plugin.buildApklibin addition to any of the variants above- In
build.sbt, addandroid.Plugin.buildAarand/orandroid.Plugin.buildApklibon a new line. - It could also be specified, for example, like so:
android.Plugin.androidBuild ++ android.Plugin.buildAar
- In
- To specify that your project will generate and publish either an
-
Multi-project builds
- See multi-project build examples in the test cases for an example of configuration.
androidBuild(...)should be used to specify all dependent library-projects- All sub-projects in a multi-project build must specify
exportJars := true. Android projects automatically set this variable. - When using multi-project builds in Scala, where library projects have
scala code, but the main project(s) do(es) not, you will need to specify
that proguard must run. To do this, the following must be set for each
main project:
proguardScala := true
-
Configuring
sbt-androidby editingbuild.sbt- Add configuration options according to the sbt style:
useProguard := trueto 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.
- Configurable keys can be discovered by typing
android:<tab>at the sbt shell
- Add configuration options according to the sbt style:
-
Configuring proguard, some options are available
proguardOptions ++= Seq("-dontobfuscate", "-dontoptimize")- 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 -
On-device testing, use
android:testand see Android Testing Fundamentals -
Unit testing with robolectric and Junit (use the
testtask), see how it works in the robo-junit-test test case- Be sure to set
fork in Test := trueotherwise the classloading black magic in robolectric will fail.
- Be sure to set
-
Device Management
- The commands
devicesanddeviceare 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:runandandroid:testare tasks that can be used to install, run and test the built apk on-device, respectively.
- The commands
-
Full list of
sbt-androidadded commands, all commands have full tab completion when possible.adb-ls <path>adb-cat <file>adb-rm <file>adb-pull <file> [destination]adb-push <file> <destination>adb-shell <command>adb-runas <command>adb-kill[/project]logcat [-p pid] [-s tags] [options...]logcat-grep [-p pid] [regex]pidcat[/project] [partial pkg] [TAGs...]pidcat-grep[/project] [partial pkg] [regex]gen-android <platform> <package> <name>gen-android-sbtdevice <serial>devicesadb-screenonadb-wifiadb-reboot [recovery|bootloader]variant[/project] [buildType] [flavor]variant-reset[/project]android-install <package>android-update <all|package>android-license <license-id>
autolibsdo not properly processapklibandaarresources. If anything in anautolibuses resources from such a library, the answer is to create a standard multi-project build configuration rather than utilizeautolibs.autolibscan be disabled by manually configuringlocalProjects