我正在尝试尝试 Android 的 Jetpack Benchmark 库,但似乎无法克服这个错误。已经尝试了我能想到的所有东西,如果能帮助我完成这个简单的示例,我将不胜感激。
java.lang.AssertionError: ERRORS (not suppressed): ACTIVITY-MISSING
(Suppressed errors: DEBUGGABLE EMULATOR UNLOCKED)
WARNING: Not using IsolationActivity via AndroidBenchmarkRunner
AndroidBenchmarkRunner should be used to isolate benchmarks from interference
from other visible apps. To fix this, add the following to your module-level
build.gradle:
android.defaultConfig.testInstrumentationRunner
= "androidx.benchmark.junit4.AndroidBenchmarkRunner"
我已按照示例指南 here ,和我的
defaultConfig
包含:testInstrumentationRunner 'androidx.benchmark.junit4.AndroidBenchmarkRunner'
// Suppressing errors I can ignore while I'm testing. Unfortunately, suppressing
// the ACTIVITY-MISSING error causes runtime crashes
testInstrumentationRunnerArgument 'androidx.benchmark.suppressErrors', 'EMULATOR,DEBUGGABLE,UNLOCKED'
我的依赖项包含:
androidTestImplementation 'androidx.annotation:annotation:1.1.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.benchmark:benchmark-junit4:1.0.0'
我的基准代码如下:
import android.util.Log;
import androidx.benchmark.BenchmarkState;
import androidx.benchmark.junit4.BenchmarkRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class BenchmarkTest {
@Rule
public BenchmarkRule benchmarkRule = new BenchmarkRule();
@Test
public void log() {
final BenchmarkState state = benchmarkRule.getState();
while (state.keepRunning()) {
Log.d("LogBenchmark", "the cost of writing this log method will be measured");
}
}
}
最佳答案
当我关注 quick start guide 时,我遇到了同样的问题.但是,我通过添加一个新的 benchmark module 让它工作了。并从 sample app 中获取依赖项.
添加基准模块的步骤:
1.文件>新建>新模块
2.选择基准模块
依赖项:
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation project("<enter your project>")
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.appcompat:appcompat:1.1.0'
androidTestImplementation 'androidx.constraintlayout:constraintlayout:1.1.3'
androidTestImplementation 'androidx.cardview:cardview:1.0.0'
androidTestImplementation 'androidx.recyclerview:recyclerview:1.0.0'
androidTestImplementation 'androidx.benchmark:benchmark-junit4:1.1.0-alpha01'
buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "benchmark-proguard-rules.pro"
}
release {
isDefault = true
isDefault = true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'benchmark-proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
请务必定义发布签名配置,因为默认情况下创建基准测试模块不会添加它。
关于java - 不通过 AndroidBenchmarkRunner 使用 IsolationActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61593995/