不使用 AndroidBenchmarkRunner 通过 IsolationActivity。

huangapple go评论69阅读模式
英文:

Not using IsolationActivity via AndroidBenchmarkRunner

问题

我试图尝试使用Android的Jetpack Benchmark库,但似乎无法解决这个错误。已经尝试了我所能想到的一切,希望能够得到帮助,使这个简单的示例能够运行。

java.lang.AssertionError: 错误未被抑制):ACTIVITY-MISSING
被抑制的错误DEBUGGABLE EMULATOR UNLOCKED

警告未使用 AndroidBenchmarkRunner 通过 IsolationActivity 进行隔离
应当使用 AndroidBenchmarkRunner 来将基准测试与其他可见应用的干扰隔离开要解决此问题请将以下内容添加到您的模块级 build.gradle 文件中
android.defaultConfig.testInstrumentationRunner
= "androidx.benchmark.junit4.AndroidBenchmarkRunner"

我已经按照这里的示例指南进行了操作1,我的 defaultConfig 包含:

testInstrumentationRunner 'androidx.benchmark.junit4.AndroidBenchmarkRunner'
// 抑制我在测试时可以忽略的错误。不幸的是,抑制 ACTIVITY-MISSING 错误会导致运行时崩溃
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");
        }
    }
}
英文:

I'm trying to try out Android's Jetpack Benchmark library and cannot seem to get past this error. Have tried everything I can think of, would appreciate any help making this simple example work.

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"

I have followed the sample guide here, and my defaultConfig contains:

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'

and my dependencies contain:

    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'

My benchmark code is as follows:

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");
        }
    }
}

答案1

得分: 2

我在按照快速入门指南时遇到了相同的问题。然而,我通过添加一个新的基准测试模块并从示例应用程序中获取依赖项来使其工作。

添加基准测试模块的步骤:

1. 文件 > 新建 > 新建模块

不使用 AndroidBenchmarkRunner 通过 IsolationActivity。

2. 选择基准测试模块

不使用 AndroidBenchmarkRunner 通过 IsolationActivity。

依赖项:

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    androidTestImplementation 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
        }
    }

请确保定义发布签名配置,因为创建基准测试模块不会默认添加它。

英文:

I faced the same issue when I followed the quick start guide. However, I got it to work by adding a new benchmark module and picking up the dependencies from the sample app.

Steps to add the benchmark module:

1. File > New > New Module

不使用 AndroidBenchmarkRunner 通过 IsolationActivity。

2. Select Benchmark Module

不使用 AndroidBenchmarkRunner 通过 IsolationActivity。

Dependencies:

    implementation fileTree(dir: &#39;libs&#39;, include: [&#39;*.jar&#39;])

    androidTestImplementation project(&quot;&lt;enter your project&gt;&quot;)
    androidTestImplementation &quot;org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version&quot;

    androidTestImplementation &#39;junit:junit:4.12&#39;

    androidTestImplementation &#39;androidx.test:runner:1.2.0&#39;
    androidTestImplementation &#39;androidx.test:rules:1.2.0&#39;
    androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;

    androidTestImplementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
    androidTestImplementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
    androidTestImplementation &#39;androidx.cardview:cardview:1.0.0&#39;
    androidTestImplementation &#39;androidx.recyclerview:recyclerview:1.0.0&#39;
    androidTestImplementation &#39;androidx.benchmark:benchmark-junit4:1.1.0-alpha01&#39;
 buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile(&quot;proguard-android-optimize.txt&quot;), &quot;benchmark-proguard-rules.pro&quot;
        }
        release {
            isDefault = true
            isDefault = true
            minifyEnabled true
            proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;benchmark-proguard-rules.pro&#39;
            signingConfig signingConfigs.debug
        }
    }

Be sure to define the release signing config, since creating a benchmark module does not add it by default.

答案2

得分: 0

我之前也遇到过同样的问题,只有在查看他们的示例应用后才能解决。看起来他们忘记了提及 ProGuard 规则。不幸的是,这种情况经常发生,所以要检查示例应用。

我认为如果你缺少了这个部分,可能就是你遇到那个错误的原因:

-keepattributes *Annotation* 
-keepclasseswithmembers @org.junit.runner.RunWith public class * 

另一个有趣的事情是,他们在项目中并未使用 instrumentation runner。

testInstrumentationRunner "androidx.benchmark.junit4.AndroidBenchmarkRunner" 

链接:https://github.com/android/performance-samples/blob/master/BenchmarkSample/benchmark/benchmark-proguard-rules.pro

英文:

I had this same problem and was only able to solve it after checking out their sample app. It looks like they forgot to mention the pro guard rules. Unfortunately this kind of thing happens a lot so check the sample apps.

I believe if you're missing this it might be why you get that error:

-keepattributes *Annotation*
-keepclasseswithmembers @org.junit.runner.RunWith public class *

Another funny thing I am seeing is that they do not use the instrumentation runner in their project.

testInstrumentationRunner &quot;androidx.benchmark.junit4.AndroidBenchmarkRunner&quot;

https://github.com/android/performance-samples/blob/master/BenchmarkSample/benchmark/benchmark-proguard-rules.pro

答案3

得分: 0

https://developer.android.com/studio/profile/benchmark

>为了设置定期基准测试而不是一次性基准测试,您需要将基准测试分离到它们自己的模块中。

因此,请确保将基准测试放入一个 模块 库中(应用插件为 'com.android.library',而不是 'com.android.application')

此外,添加插件:

apply plugin: &#39;androidx.benchmark&#39;
英文:

https://developer.android.com/studio/profile/benchmark

>To set up benchmarking for regular benchmarking rather than one-off benchmarking, you isolate benchmarks into their own module.

So, make sure that you put the benchmark in a module library (It should apply plugin 'com.android.library', not 'com.android.application')

Also, put the plugin:

apply plugin: &#39;androidx.benchmark&#39;

答案4

得分: 0

如果您正在使用中国手机进行测试,请尝试为应用程序 com.example.benchmark.test 禁用后台限制,并启用“自启动”复选框。这对于其他制造商生产的设备也可能有效。请参阅 https://dontkillmyapp.com/

英文:

If you are using a Chinese phone to run tests, try to disable background restrictions for the app com.example.banckmark.test and enable the Auto-Start checkbox. This may also work for devices from other manufacturers. See https://dontkillmyapp.com/

huangapple
  • 本文由 发表于 2020年5月4日 21:57:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61593995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定