Android Studio を使わない ライブラリプロジェクトで Robolectric テストをする一番簡単なプロジェクト例 というエントリーを2年ほど前に書いたのですが、いろいろバージョンアップされて古くなってしまった。このテストプロジェクトをそこそこ最近のAndroid開発環境で動くようにしたので、その備忘録です。 作動確認はしていますが、Robolectric よくわかっていないのでその点はご了承ください。
AndroidStudio を使っている場合、基本的にここ(http://robolectric.org/getting-started/)に書いてある通りに、既存の build.gradle に追記すればよいのだと思います。
上記 robolectric.org/getting-started にあるサイトの記載をそのまま転記します。
android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}
dependencies {
  testImplementation 'junit:junit:4.13.2'
  testImplementation 'org.robolectric:robolectric:4.8'
}
このエントリーでは、例によって AndroidStudio は使わないで、コマンドラインで gradle と java コマンドを前提に Robolectric テスト用プロジェクトを作成していきます。
前回のエントリー をそこそこ新しい環境に移す手順。
root プロジェクト以下のファイルおよびディレクトリ構成はこれ:
.
├── build.gradle
├── color
│   ├── build.gradle
│   └── src
│       ├── main
│       │   ├── AndroidManifest.xml
│       │   └── java
│       │       └── com
│       │           └── example
│       │               └── color
│       │                   └── ColorUtils.kt
│       └── test
│           └── java
│               └── com
│                   └── example
│                       └── color
│                           └── ColorUtilsTest.kt
├── gradle.properties
└── settings.gradle
ColorUtils.kt や ColorUtilsTest.kt の内容は以前のエントリーと同じです。
root の build.gradle:
plugins {
    id 'com.android.library' version '7.3.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
colorというサブモジュールを用意したので、settings.gradle にそれ(include ':color')を追記。
settings.gradle:
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        mavenLocal()
    }
}
include ':color'
gradle.properties にAndroidXの使用指定を入れます。
android.useAndroidX=true
肝心の color モジュールの color/build.gradle:
plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}
android {
    namespace 'com.example.color'
    compileSdk 32
    defaultConfig {
        minSdk 23
        targetSdk 32
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}
dependencies {
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.robolectric:robolectric:4.8'
}
この build.gradle に、robolectric の指定が入っています。
color/src/main/AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
以前はこの AndroidManifest.xml にパッケージ名を記述していましたが、不要になりました。
代わりに build.gradle の android.namespace に記述する形になったらしい。
これであとはテストを実行するだけです。
テスト実行の前に、実行環境を確認します。
gradle のバージョン:
$ gradle -version
------------------------------------------------------------
Gradle 7.4.2
------------------------------------------------------------
Build time:   2022-03-31 15:25:29 UTC
Revision:     540473b8118064efcc264694cbcaa4b677f61041
Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          11.0.16 (Ubuntu 11.0.16+8-post-Ubuntu-0ubuntu120.04)
OS:           Linux 5.4.0-126-generic amd64
java のバージョン:
$ java --version
openjdk 11.0.16 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)
テストを実行します。
$ gradle test
うまく作動したら gradle wrapper しておきましょう。
以上です。