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

尝试使用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 / main / JAVA /测试/坏/ 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。

什么是造成这个错误讯息? 这是我应该看到的?

One Solution collect form web for “Android测试失败时,Gradle构建失败,“字符串索引超出范围””

这似乎是一个与Windows上的Gradle已知的问题。 还有一个类似的问题2077所报告的错误描述和修复(显然这是由多个\ r字符引起的)。 有一个注意事项,这个问题是要修复在Gradle 4.1中。

更新: 4.1现在出来了,这似乎已经修复了。 上面的简单应用程序的行为如预期,我有这个问题的真正的应用程序也表现完全符合预期。

  • 使用yeoman生成器Gradle + Kotlin无法生成新项目
  • Kotlin单元测试没有发现模块依赖性接口
  • Kotlin原始字符串抛出空白错误
  • 错误:执行失败的任务':app:kaptDemoTestingDebugKotlin'
  • 由于重复,Kotlin gradle.build ZipException
  • 在Android Studio中Kotlin构建失败
  • 需要Groovy知识来理解Gradle?
  • Android Studio构建失败,Kotlin
  • 与Gradle一起使用Kotlin
  • 如何使用kotlin-frontend-plugin设置package.json配置
  • Kotlin胶囊Gradle错误
  • Kotlin language will be the best programming language for Android.