gpt4 book ai didi

android - 当 Android 测试失败时,Gradle 构建失败并显示 "String Index out of range"

转载 作者:行者123 更新时间:2023-11-28 20:38:19 25 4
gpt4 key购买 nike

我在尝试使用 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/main/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/test/bad/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/test/bad/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。

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

最佳答案

这似乎是 Windows 上 Gradle 的一个已知问题。 There is a similar error description reported as issue 2077并发布了一个修复程序(显然这是由多个\r 字符引起的)。注意到此问题将在 Gradle 4.1 中修复。

更新: 4.1 现已发布,这似乎已得到修复。上面的简单应用程序的行为符合预期,而我遇到此问题的真实应用程序的行为也完全符合预期。

关于android - 当 Android 测试失败时,Gradle 构建失败并显示 "String Index out of range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45368202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com