You can run any of the tasks on bitrise from a Script step by calling gradle task-name-to-run (for example: gradle assemle)
or by using our Gradle Runner step (https://github.com/bitrise-io/steps-gradle-runner)
and specifying the task as the value of the gradle_task input.

Instead of running gradle directly, you should run the gradle commands through gradlew (the Gradle Wrapper)!
The Gradle Runner step does this, and as you can see it in the related input description of the step:

Using a Gradle Wrapper (gradlew) is strongly suggested, as the wrapper is what makes sure
that the right Gradle version is installed and used for the build.

The preferred way to do this is to use the Install missing Android tools step.
Please only use a Script solution if you really have to, as you'll have to update
the Script if the Android tools change (which did happen).

All you have to do is to add a Script step to your workflow,
and use the Android sdkmanager tool to install the additional packages you want to.

As an example, to install the Android SDK v18 and the related build-tools v18.0.1,
you can add a Script step (can be the very first step in the Workflow)
with the following content:

You can get the full list of available packages by running:
sdkmanager --list --include_obsolete --verbose.
You can run this on your own machine if you have $ANDROID_HOME/tools/bin in your $PATH.
If not then you can run it with /PATH/TO/ANDROID-SDK-HOME/tools/bin/sdkmanager ....

First you have to create an emulator with a Create Android emulator step,
where you can set the Android version and a couple of other parameters for the new emulator,
then you can boot this emulator with the Start Android emulator step,
which makes sure that the emulator is booted and ready for subsequent steps.

Instead of using a Script step to create an android emulator please use the Create Android emulator step!
There are simply too many edge cases to cover here, as well as the commands and working configurations change quite frequently.

The section below is kept here for referencing purposes, and might be outdated.

Note about Android SDK versions: at this time there are lots of known issues reported for Android Emulators
with Android SDK version 22 & 23 when combined with Google Play services
(see 1
and 2).
The script above creates an emulator with SDK version 21, which should work properly with Google Play services.

There are possible workarounds for newer versions
(see 1
and 2),
but that requires some customization in your project.

Java 8 is now the pre-installed Java version on the Bitrise.io Linux Stack.
This section is kept here for future reference, in case you'd need another Java version.

If you'd need a Java / JDK version which is not preinstalled on the Android stacks,
you can follow this guide to install it. This example will install Java/JDK 8,
please adapt it to the version you need.

If your build requires JDK 8, you can install and activate it with a Script step: