package com.github.tmurakami.dexmockito;

import android.support.test.InstrumentationRegistry;

import net.bytebuddy.android.AndroidClassLoadingStrategy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;

import org.mockito.internal.creation.bytebuddy.SubclassLoader;

import java.io.File;

final class DexSubclassLoader implements SubclassLoader {

    @Override
    public ClassLoadingStrategy<ClassLoader> getStrategy(Class<?> mockedType) {
        File dexCacheDir = getDexCacheDir();
        if (!dexCacheDir.exists() && !dexCacheDir.mkdirs()) {
            throw new Error("Cannot access DexMockito cache directory");
        }
        return new AndroidClassLoadingStrategy.Injecting(dexCacheDir);
    }

    private static File getDexCacheDir() {
        String property = System.getProperty("dexmaker.dexcache", "");
        return property.length() > 0 ? new File(property) : new File(getCacheDir(), "dexmockito");
    }

    private static File getCacheDir() {
        try {
            return InstrumentationRegistry.getTargetContext().getCacheDir();
        } catch (NoClassDefFoundError e) {
            return CacheDir.get(new File("/"), DexSubclassLoader.class.getClassLoader());
        }
    }

}

@@ -87,16 +87,47 @@
 egistry;
+%0Aimport android.text.TextUtils;
 %0A%0Aimport
@@ -383,16 +383,157 @@
 ader %7B%0A%0A
+    private static final String%5B%5D CACHE_PROPERTY_KEYS = %7B%0A            %22dexmaker.dexcache%22,%0A            %22org.mockito.android.target%22,%0A    %7D;%0A%0A
     @Ove
@@ -941,130 +941,233 @@
     
-String property = System.getProperty(%22dexmaker.dexcache%22, %22%22);%0A        return property.length() %3E 0 ? new File(property) :
+for (String key : CACHE_PROPERTY_KEYS) %7B%0A            String property = System.getProperty(key);%0A            if (!TextUtils.isEmpty(property)) %7B%0A                return new File(property);%0A            %7D%0A        %7D%0A        return
  new

Support 'org.mockito.android.target' system property