2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AndroidX + Robolectric + MultiDex のビルドがエラーになるとき

Posted at

Robolectric の MultiDex 対応に org.robolectric:shadows-multidex をいれてて
AndroidX への移行している場合、
ビルド時に Jetifier まわりでエラーが出ることがあります:

> Execution failed for JetifyTransform: /home/circleci/.gradle/caches/modules-2/files-2.1/org.robolectric/shadows-support-v4/3.3.2/120e08a6d6d9bd56614f9a3e91c853039107761d/shadows-support-v4-3.3.2.jar.
> Failed to transform '/home/circleci/.gradle/caches/modules-2/files-2.1/org.robolectric/shadows-support-v4/3.3.2/120e08a6d6d9bd56614f9a3e91c853039107761d/shadows-support-v4-3.3.2.jar' using Jetifier.
Reason: The given artifact contains a string literal with a package reference 'android.support.v4.content' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.

ブラックリストに追加

この場合 Jetifier と相性が悪いので
Workaround としてブラックリスト入りさせる方法が提案されてます:

👉 https://github.com/robolectric/robolectric/issues/4491#issuecomment-498603341

gradle.properties
android.jetifier.blacklist=shadows

ShadowAndroidXMultiDex を使うように変更

あとは Config を与えるときの Config.Builder.setShadows(ShadowMultiDex.class)Config.Builder.setShadows(ShadowAndroidXMultiDex::class) に変更しておくのも必要です。

バージョンによっては ShadowAndroidXMultiDex がないことがあるので、shadows-multidex のバージョンを上げておくことも必要です:

app/build.gradle
testImplementation "org.robolectric:shadows-multidex:4.3"

GlobalConfigProvider の設定

で、この Config を設定するために GlobalConfigProvider を設定しておく必要があります:

👉 https://github.com/robolectric/robolectric/issues/4770#issuecomment-479559317

app/src/test/java/your/own/app/provider/CustomGlobalConfigProvider.kt
package your.own.app.provider

import com.google.auto.service.AutoService
import org.robolectric.annotation.Config
import org.robolectric.pluginapi.config.GlobalConfigProvider
import org.robolectric.shadows.multidex.ShadowAndroidXMultiDex

@AutoService(GlobalConfigProvider::class)
class CustomGlobalConfigProvider: GlobalConfigProvider {
    override fun get(): Config {
        return Config.Builder
                .defaults()
                .setShadows(ShadowAndroidXMultiDex::class.java)
                .setPackageName("your.own.app")
                .build()
    }
}

AutoService を使ってるので以下を追加します:

app/build.gradle
testImplementation "com.google.auto.service:auto-service:1.0-rc6"
kaptTest "com.google.auto.service:auto-service:1.0-rc6"

これで Robolectric の Multidex が Jetifier 有効でもビルドできるようになるかと思います。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?