<commit_before>---
title: Getting Started
group: Setup
order: 1
---

# Getting Started

Robolectric works best with Gradle or Bazel. If you are starting a new project, we would recommend Gradle first (since it is the build system of choice in Android Studio) and Bazel second. Both environments provide first class support for Robolectric. If you are using another build system see how to configure [other environments](http://robolectric.org/other-environments) or learn how to provide first class [build system integration](http://robolectric.org/build-system-integration) support for other environments.

## Building with Gradle

### Android Studio 3.0+

Starting with Robolectric 3.3 there is now tighter integration with the tool chain, where the build system processes and merges your resources. You'll need Android Studio 3.0.

Add the following to your build.gradle:

```groovy
testImplementation "org.robolectric:robolectric:{{ site.robolectric.version.current | escape }}"

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}
```

Annotate your test with the Robolectric test runner:

```java
@RunWith(RobolectricTestRunner.class)
public class SandwichTest {
}
```

## Building with Bazel
Robolectric works with [Bazel](https://bazel.build) 0.10.0 or higher. Bazel integrates with Robolectric through the android_local_test rule. The Robolectric Java test code is the same for a Bazel project as a new Gradle project.

Robolectric needs to be added as a dependency to your Bazel project. Add the following to your WORKSPACE file:
```python
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
 name = "robolectric",
 urls = ["https://github.com/robolectric/robolectric-bazel/archive/robolectric-bazel-<VERSION>.tar.gz"],
 strip_prefix = "robolectric-<VERSION>",
 sha256 = "<HASH>",
)
load("@robolectric//bazel:setup_robolectric.bzl", "robolectric_repositories")
robolectric_repositories()
```

Currently supported versions are 3.8 and 4.0-alpha-3.

Add an android_local_test rule to your BUILD file:
```python
android_local_test(
  name = "MyTest",
  srcs = ["MyTest.java"],
  manifest = "TestManifest.xml",
  deps = [
    ":sample_test_lib",
    "@robolectric//bazel:robolectric",
  ],
)

android_library(
    name = "sample_test_lib",
    srcs = ["Lib.java"],<commit_msg>Explicitly mention robolectric version 4.0-alpha-3<commit_after>---
title: Getting Started
group: Setup
order: 1
---

# Getting Started

Robolectric works best with Gradle or Bazel. If you are starting a new project, we would recommend Gradle first (since it is the build system of choice in Android Studio) and Bazel second. Both environments provide first class support for Robolectric. If you are using another build system see how to configure [other environments](http://robolectric.org/other-environments) or learn how to provide first class [build system integration](http://robolectric.org/build-system-integration) support for other environments.

## Building with Gradle

### Android Studio 3.0+

Starting with Robolectric 3.3 there is now tighter integration with the tool chain, where the build system processes and merges your resources. You'll need Android Studio 3.0.

Add the following to your build.gradle:

```groovy
testImplementation "org.robolectric:robolectric:{{ site.robolectric.version.current | escape }}"

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}
```

Annotate your test with the Robolectric test runner:

```java
@RunWith(RobolectricTestRunner.class)
public class SandwichTest {
}
```

## Building with Bazel
Robolectric works with [Bazel](https://bazel.build) 0.10.0 or higher. Bazel integrates with Robolectric through the android_local_test rule. The Robolectric Java test code is the same for a Bazel project as a new Gradle project.

Robolectric needs to be added as a dependency to your Bazel project. Add the following to your WORKSPACE file:
```python
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
 name = "robolectric",
 urls = ["https://github.com/robolectric/robolectric-bazel/archive/4.0-alpha-3.tar.gz"],
 strip_prefix = "robolectric-bazel-4.0-alpha-3",
 sha256 = "7730b46281c2f8c23f6c045ddfe449bf077328f0128200043a5ac57210643f22",
)
load("@robolectric//bazel:setup_robolectric.bzl", "robolectric_repositories")
robolectric_repositories()
```

Robolectric version 3.8 is also supported.

Add an android_local_test rule to your BUILD file:
```python
android_local_test(
  name = "MyTest",
  srcs = ["MyTest.java"],
  manifest = "TestManifest.xml",
  deps = [
    ":sample_test_lib",
    "@robolectric//bazel:robolectric",
  ],
)

android_library(
    name = "sample_test_lib",
    srcs = ["Lib.java"],
{'fraction_non_alphanumeric': 0.10016729401923881, 'fraction_numerical': 0.01902969468841489, 'mean_word_length': 4.797575757575758, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1502544', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1461, 'n_words': 482}