Skip to content

Latest commit

 

History

History
128 lines (90 loc) · 4.12 KB

Introduction.Android.md

File metadata and controls

128 lines (90 loc) · 4.12 KB

Adding Android

Setup

1. Do the initial setup described in the Getting Started Guide

2. Add the detox Android project as an androidTestCompile dependency

You need to add this into your android/settings.gradle:

include ':detox'
project(':detox').projectDir = new File(rootProject.projectDir, '../node_modules/detox/android/detox')

In case your project’s RN version is at least 0.46.0 change the oldOkhttp configuration string to newOkhttp, in the app/build.gradle here.

In the android/app/build.gradle you need to add this into your dependencies section:

androidTestCompile(project(path: ":detox", configuration: "newOkhttpDebug"), {
    exclude group: 'com.android.support', module: 'support-annotations'
})

Please be aware that the minSdkVersion needs to be at least 18.

3. Add jacoco-coverage as dependency in the buildscript

You need to add this to android/build.gradle into buildscript > dependencies:

classpath 'com.palantir:jacoco-coverage:0.4.0'

And in the same file you need to add this under allprojects > repositories:

maven {
    url "https://maven.google.com"
}

4. Introduce Espresso test runner

Detox Android is a standard Android integration test. Although, it is completely asynchronous.

It uses Espresso internally, therefore you have to use an AndroidJUnitRunner as your test runner (or a subclass of it). For this you need to modify your android/app/build.gradle:

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

5. Create Android Test class

You need to add the file android/app/src/androidTest/java/com/[your.package]/DetoxTest.java and fill it like this, expect that you need to change the package to your projects name.

6. Add Android configuration

Add this part to your package.json:

"detox": {
    "configurations": {
        "android.emu.debug": {
            "binaryPath": "android/app/build/outputs/apk/app-debug.apk",
            "build": "pushd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && popd",
            "type": "android.emulator",
            "name": "Nexus_5X_API_25"
        }        
    }
}

7. Run the tests

Using the android.emu.debug configuration from above, you can invoke it in the standard way.

detox test -c android.emu.debug

Troubleshooting

Problem: Duplicate files copied in ...

If you get an error like this:

Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE

You need to add this to the android section of your android/app/build.gradle:

packagingOptions {
    exclude 'META-INF/LICENSE'
}

Problem: Conflict with dependency 'com.fasterxml.jackson.core:jackson-core'.

You might encounter version conflicts with dependencies, an error may look like this:

Conflict with dependency 'com.fasterxml.jackson.core:jackson-core'. Resolved versions for app (2.8.7) and test app (2.2.3) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

The easiest way to solve these is by replacing the dependency, meaning excluding the dependency versions from detox and re-adding the versions that caused the conflict. You need to replace some parts in the android/app/build.gradle:

androidTestCompile(project(path: ":detox", configuration: "newOkhttpDebug"), {
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core'
    exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
})
androidTestCompile 'com.fasterxml.jackson.core:jackson-core:2.8.7'
androidTestCompile 'com.fasterxml.jackson.core:jackson-databind:2.8.7'