简体   繁体   English

Android仪器测试由于RenderScript而失败

[英]Android instumentation tests fail because of RenderScript

I am building some ui tests with Espresso and everything works fine until I access a view in the application where RenderScript is used to blur an image. 我正在用Espresso构建一些ui测试,并且一切正常,直到我访问应用程序中使用RenderScript来模糊图像的视图。

The test fails with the error: 测试失败,并显示以下错误:

java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation java.lang.IllegalAccessError:预验证类中的类ref被解析为意外的实现

From what I googled it seems this happens because there are 2 instances of the same class in the test application, and the majority of solutions hint that the problem may be the guava library added twice. 根据我的搜索,似乎发生这种情况是因为测试应用程序中有2个相同类的实例,并且大多数解决方案都暗示问题可能是guava库被添加了两次。 But this is not my case, as it fails very clearly on a line of code where RenderScript is instantiated. 但这不是我的情况,因为它在实例化RenderScript的代码行中非常明显地失败了。

Have you experienced something similar? 您是否经历过类似的经历? I would appreciate any guidance. 我将不胜感激。

OK, so I used a little hack to fix this problem. 好的,因此我使用了一些技巧来解决此问题。

The hack is to have a flag class (can be empty) in androidTest->java->com (location is not particular but is important to be part of androidTest ). 技巧是在androidTest->java->com拥有一个标志类(可以为空)(位置不是特别重要,但成为androidTest一部分很重要)。

Something like this: 像这样:

public class TestsRunningFlag {
}

Then have an utility method that checks if that class can be loaded. 然后有一个实用程序方法来检查是否可以加载该类。

    /**
     * A hacky way to determine whether the application is running normally,
     * or as part of an instrumentation test.
     */
    public static boolean isTestMode(Context context) {
        boolean result;
        try {
            context.getClassLoader().loadClass("com.TestsRunningFlag");
            result = true;
        } catch (final Exception e) {
            result = false;
        }
        return result;
    }

And then based on whether I am in test mode or not, I use RenderScript to process the image or not. 然后根据我是否处于测试模式,我使用RenderScript处理图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM