1

I saw this question: Mockito + Dexmaker on Android

And after the using the answer I came into another problem: java.lang.RuntimeException: java.lang.IllegalArgumentException: Optimized data directory /sdcard is not owned by the current user. Shared storage cannot protect your application from code injection attacks.

I use the latest version of mockito and dexmaker and dexmaker-mockito.

I'm using a separate testproject.

Full stacktrace (testParse is my test function):

java.lang.RuntimeException: java.lang.IllegalArgumentException: Optimized data directory /sdcard is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
at com.google.dexmaker.DexMaker.generateAndLoad(DexMaker.java:388)
at com.google.dexmaker.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:252)
at com.google.dexmaker.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:54)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:49)
at org.mockito.Mockito.mock(Mockito.java:1245)
at org.mockito.Mockito.mock(Mockito.java:1139)
at com.myproject.parser.JSONParserTest.testParse(JSONParserTest.java:29)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: java.lang.IllegalArgumentException: Optimized data directory /sdcard is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.loadDex(DexFile.java:149)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:259)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:223)
at dalvik.system.DexPathList.<init>(DexPathList.java:106)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:46)
at dalvik.system.DexClassLoader.<init>(DexClassLoader.java:57)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at com.google.dexmaker.DexMaker.generateAndLoad(DexMaker.java:382)
... 19 more
Community
  • 1
  • 1
Nieck
  • 43
  • 1
  • 7

2 Answers2

1

I found the solution on http://code.google.com/p/dexmaker/issues/detail?id=2

What worked was putting the following line in the setUp():

System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());

Let's hope a fix will be made soon instead of using this workaround.

Nieck
  • 43
  • 1
  • 7
1
//Depending on the type of test, you can also do this:
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());