Android Robolectric won';t使用SecretKeyFactory.getInstance(KEYGEN_算法)运行

Android Robolectric won';t使用SecretKeyFactory.getInstance(KEYGEN_算法)运行,android,robolectric,Android,Robolectric,Robolectric在我从日志开始的每个测试中都会抛出错误: java.lang.RuntimeException: java.lang.RuntimeException: Invalid environment at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240) at org.junit.runners.ParentRunner.runLeaf(Pare

Robolectric在我从日志开始的每个测试中都会抛出错误:

    java.lang.RuntimeException: java.lang.RuntimeException: Invalid environment
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: Invalid environment
    at com.android.vending.licensing.AESObfuscator.<init>(AESObfuscator.java:68)
    at com.myproject.ApplicationClass.onCreate(ApplicationClass.java:65)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:164)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:430)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
    ... 16 more
Caused by: java.security.NoSuchAlgorithmException: PBEWITHSHAAND256BITAES-CBC-BC SecretKeyFactory not available
    at javax.crypto.SecretKeyFactory.<init>(DashoA13*..)
    at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
    at com.android.vending.licensing.AESObfuscator.<init>(AESObfuscator.java:57)
    ... 20 more

如有任何建议,我将不胜感激。JavaSDK是1.6(用于测试android项目)。测试项目的android.jar来自18个android版本。

问题是,当您运行测试时,您似乎没有该算法的实现。我从你的问题中猜测,这段代码在Android设备上运行正常,只在测试中失败。如果是这样的话,可能是Android包含了一些实现,但是当您运行Robolectric测试(在JVM上运行,而不是在Android设备上运行)时,您就没有了


尝试获取,并确保获得您拥有的JDK版本的正确版本(可能与6不同,因为Robo测试也会很高兴地使用较新版本的Java构建和运行,即使Android在Java中使用了6次)。将其添加到您的类路径中,仅用于robolectric测试,因为似乎不需要将其与应用程序一起分发,然后重试

谢谢你的回答。这是正确的。是的,在Android设备上一切正常,因为测试失败。找到bouncy castle解决了这个问题。很高兴它成功了!确保bouncy castle jar的范围仅用于测试,因为不需要随应用程序一起提供(如果您确实决定需要随应用程序一起提供,则有一个特定于android的分支)。
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHSHAAND256BITAES-CBC-BC");