LoginSignup
2
3

More than 5 years have passed since last update.

インストルメンテーションテストアプリにMultidexを適用する

Last updated at Posted at 2017-07-22

Multidex 1.0.2でインストルメンテーションテストアプリに対するmultidexingがサポートされました。
https://developer.android.com/topic/libraries/support-library/revisions.html#25-4-0

例えば以下のライブラリを併用すると、64K制限にひっかかります。

app/build.gradle
androidTestCompile 'org.mockito:mockito-android:2.8.47'
androidTestCompile 'com.squareup.assertj:assertj-android-appcompat-v7:1.1.1', {
    exclude group: 'com.android.support'
}
androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.8.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2', {
    exclude group: 'com.android.support'
}

Multidexはこのような場合に有効な手段なのですが、残念なことに現在ドキュメントがないようですので、以下に導入方法を示します。

2017/07/29追記
ドキュメントの不備について指摘したところ、gradleプラグイン側の対応が未完了との回答を頂いたので、以下の内容を試す場合は自己責任でお願いします。

まず、build.gradleのrepositoriesmaven { url 'https://maven.google.com' }を追加します。

build.gradle
allprojects {
    repositories {
        // Gradle 4.0以上を使用している場合は、`google()`でも可
        maven { url 'https://maven.google.com' }
    }
}

次に、app/build.gradleにて、Multidexを有効にします。

app/build.gradle
android {
    defaultConfig {
        multiDexEnabled true
    }
}

configurations {
    all {
        // 強制的に1.0.2を適用します
        resolutionStrategy.force 'com.android.support:multidex:1.0.2'
    }
}

dependencies {
    compile 'com.android.support:multidex:1.0.2'
}

AndroidJUnitRunnerを継承したJUnitRunnerを用意し、以下のようにMultiDex#installInstrumentation(Context, Context)を呼び出します。

app/src/androidTest/java/com/example/myapplication/JUnitRunner.java
// パッケージ名は適宜読みかえて下さい
package com.example.myapplication;

import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.test.runner.AndroidJUnitRunner;

public class JUnitRunner extends AndroidJUnitRunner {
    @Override
    public void onCreate(Bundle arguments) {
        MultiDex.installInstrumentation(getContext(), getTargetContext());
        super.onCreate(arguments);
    }
}

最後に、app/build.gradleのtestInstrumentationRunnerに、作成したJUnitRunnerを指定します。

app/build.gradle
android {
    defaultConfig {
        testInstrumentationRunner 'com.example.myapplication.JUnitRunner'
    }
}

以上です。

2
3
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
3