Android测试失败时,Gradle构建失败,“字符串索引超出范围”

时间:2017-07-28 08:10:52

标签: android testing gradle android-gradle android-instrumentation

尝试使用Android进行检测测试时遇到问题。当其中一个测试失败时,构建失败,但没有说明测试失败。相反,我得到FAILURE: Build failed with an exception.对于出错的部分,我得到String index out of range: -1

在我的真实应用中,这导致它也不会生成html报告,并且偶尔会生成xml报告。我已经整理了一个简单的测试用例,但它正在生成报告,但是会给出相同的失败消息。

为了澄清,该测试旨在失败,但我希望发出A test has failed消息而不是看起来不同的失败原因。我怀疑失败导致其他原因在构建生命周期中出错导致我看到的消息。

这是一个非常简单的测试应用

的build.gradle

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.4.0-alpha7'
    }
}

apply plugin: 'com.android.application'

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.2"
    androidTestCompile "com.android.support.test.espresso:espresso-intents:2.2.2"
    androidTestCompile "com.android.support.test:runner:0.5"
}

android {
    compileSdkVersion 24
    buildToolsVersion '25.0.2'

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

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8 
    }

    buildTypes {        
        release {
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
}

的src /主/ AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test.bad"
      android:versionCode="1"
      android:versionName="1">
    <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="24" />     
    <application android:label="bad">
        <activity android:name="BadTestActivity" android:label="bad">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

的src /主/爪哇/测试/坏/ BadTestActivity.java

package com.test.bad;

import android.app.Activity;
import android.os.Bundle;

public class BadTestActivity extends Activity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

的src / androidTest / JAVA / COM /测试/坏/ BadTest.java

package com.test.bad;

import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.fail;
import android.support.test.runner.AndroidJUnit4;

@RunWith(AndroidJUnit4.class)
public class BadTest {

    @Test
    public void failTest() {
        fail("This test just fails");
    }

}

当我运行gradle connectedAndroidTest时,我看到了

:connectedDebugAndroidTest

FAILURE: Build failed with an exception.

* What went wrong:
String index out of range: -1

* Try:
Run with --stacktrace option to get the stack trace.  Run with --info or --debug 
option to get more log output.

我希望能给出一些关于构建失败的消息,因为测试失败而不是像字符串索引那样奇怪。在我的实际应用程序中(在测试中也使用了Kotlin和Mockito),并不总是生成报告html(还有更多的测试,我认为这与缺少报告有关)。

使用--info或--debug似乎没有显示任何有用的内容,但使用--stacktrace显示

* Exception is:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1967)
    at org.gradle.internal.logging.text.AbstractLineChoppingStyledTextOutput$StateContext.flushLineText(AbstractLineChoppingStyledTextOutput.java:115)
    at org.gradle.internal.logging.text.AbstractLineChoppingStyledTextOutput$2.execute(AbstractLineChoppingStyledTextOutput.java:167)
    at org.gradle.internal.logging.text.AbstractLineChoppingStyledTextOutput$2.execute(AbstractLineChoppingStyledTextOutput.java:160)
    at org.gradle.internal.logging.text.AbstractLineChoppingStyledTextOutput$1.execute(AbstractLineChoppingStyledTextOutput.java:146)
    at org.gradle.internal.logging.text.AbstractLineChoppingStyledTextOutput$1.execute(AbstractLineChoppingStyledTextOutput.java:131)
    at org.gradle.internal.logging.text.AbstractLineChoppingStyledTextOutput.doAppend(AbstractLineChoppingStyledTextOutput.java:41)
    at org.gradle.internal.logging.text.AbstractStyledTextOutput.text(AbstractStyledTextOutput.java:73)
    at org.gradle.internal.logging.text.AbstractStyledTextOutput.println(AbstractStyledTextOutput.java:68)
    at org.gradle.internal.logging.events.LogEvent.render(LogEvent.java:48)
    at org.gradle.internal.logging.console.StyledTextOutputBackedRenderer.onOutput(StyledTextOutputBackedRenderer.java:65)
    at org.gradle.internal.logging.sink.ProgressLogEventGenerator.doOutput(ProgressLogEventGenerator.java:72)
    at org.gradle.internal.logging.sink.ProgressLogEventGenerator.onOutput(ProgressLogEventGenerator.java:62)
    at org.gradle.internal.logging.console.BuildLogLevelFilterRenderer.onOutput(BuildLogLevelFilterRenderer.java:42)
    at org.gradle.internal.logging.sink.OutputEventRenderer$4.onOutput(OutputEventRenderer.java:265)
    at org.gradle.internal.logging.sink.OutputEventRenderer$LazyListener.onOutput(OutputEventRenderer.java:368)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:42)
    at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:230)
    at org.gradle.internal.event.BroadcastDispatch$SingletonDispatch.dispatch(BroadcastDispatch.java:149)
    at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:58)
    at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:324)
    at org.gradle.internal.event.BroadcastDispatch$CompositeDispatch.dispatch(BroadcastDispatch.java:234)
    at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:140)
    at org.gradle.internal.event.ListenerBroadcast.dispatch(ListenerBroadcast.java:37)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy0.onOutput(Unknown Source)
    at org.gradle.internal.logging.sink.OutputEventRenderer.onOutput(OutputEventRenderer.java:334)
    at org.gradle.launcher.daemon.client.DaemonClient.monitorBuild(DaemonClient.java:216)
    at org.gradle.launcher.daemon.client.DaemonClient.executeBuild(DaemonClient.java:178)
    at org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:141)
    at org.gradle.launcher.daemon.client.DaemonClient.execute(DaemonClient.java:92)
    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)
    at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:173)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:287)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:260)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:33)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:253)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:182)
    at org.gradle.launcher.Main.doAction(Main.java:33)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:60)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:37)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)

我正在使用Windows 7,使用Gradle 4.0(问题也存在于3.5)和Java 8.我正在物理设备上运行我的测试 - 运行Nougat 7.0的三星Galaxy S6。

导致此错误消息的原因是什么?这是我应该看到的吗?

1 个答案:

答案 0 :(得分:1)

看起来这是Gradle on Windows的一个已知问题。发出了There is a similar error description reported as issue 2077和修正(显然这是由多个\ r \ n字符引起的)。请注意,此问题将在Gradle 4.1中修复。

更新: 4.1现已推出,这似乎已修复。上面的简单应用程序表现得像预期的那样,我遇到此问题的真实应用程序的行为也与预期完全一样。