Using multiple res folders with Robolectric

后端 未结 2 1695
滥情空心
滥情空心 2020-12-07 05:48

My current Gradle configuration has multiple (Merged) res folders:

sourceSets {
    androidTest {
        setRoot(\'src/test\')
    }
    main {
        res.         


        
2条回答
  •  我在风中等你
    2020-12-07 05:57

    Ok, this is the easiest way to do it, You will have to extend RobolectricTestRunner getAppManifest and createAppResourceLoader.

    In getAppManifest you will simply have to store the manifest in a field, let's say mDefaultManifest.

    In createAppResourceLoader you will have to add the right resources injected.

    /**
     * TODO: Watch OUT this is copied from RobolectricTestRunner in Robolectric-2.4 keep it up to date!
     */
    @Override
    protected ResourceLoader createAppResourceLoader(ResourceLoader systemResourceLoader, AndroidManifest appManifest) {
        List appAndLibraryResourceLoaders = new ArrayList();
        for (ResourcePath resourcePath : appManifest.getIncludedResourcePaths()) {
            appAndLibraryResourceLoaders.add(createResourceLoader(resourcePath));
        }
    
            /* BEGIN EDIT */
            if(mDefaultManifest != null) {
                ResourcePath rpInjected = new ResourcePath(mDefaultManifest.getRClass(), mDefaultManifest.getPackageName(), Fs.fileFromPath("../app/src/main/res/features/registration"), mDefaultManifest.getAssetsDirectory());
                appAndLibraryResourceLoaders.add(createResourceLoader(rpInjected));
                rpInjected = new ResourcePath(mDefaultManifest.getRClass(), mDefaultManifest.getPackageName(), Fs.fileFromPath("../app/src/main/res/features/login"), mDefaultManifest.getAssetsDirectory());
                appAndLibraryResourceLoaders.add(createResourceLoader(rpInjected));
            }
            /* END EDIT */
    
        OverlayResourceLoader overlayResourceLoader = new OverlayResourceLoader(appManifest.getPackageName(), appAndLibraryResourceLoaders);
    
        Map resourceLoaders = new HashMap();
        resourceLoaders.put("android", systemResourceLoader);
        resourceLoaders.put(appManifest.getPackageName(), overlayResourceLoader);
        return new RoutingResourceLoader(resourceLoaders);
    }
    

    Do not forget to add @RunWith(YourTestRunner.class) in your test classes.

提交回复
热议问题