美文网首页Android技术知识Android开发Android开发
Robolectric被墙下载缓慢的解决方法

Robolectric被墙下载缓慢的解决方法

作者: Charle_Lin | 来源:发表于2017-05-17 10:40 被阅读127次

    Robolectric被墙下载缓慢的解决方法

    把下载地址换成阿里的maven,速度杠杠的。

    public final class XLTestRunner extends RobolectricGradleTestRunner {
    
        public XLTestRunner(Class<?> testClass) throws InitializationError {
            super(testClass);
        }
    
        @Override protected DependencyResolver getJarResolver() {
            return new CustomDependencyResolver();
        }
    
        static final class CustomDependencyResolver implements DependencyResolver {
            private final Project project = new Project();
    
            public URL[] getLocalArtifactUrls(DependencyJar... dependencies) {
                DependenciesTask dependenciesTask = new DependenciesTask();
                RemoteRepository repository = new RemoteRepository();
                repository.setUrl("http://maven.aliyun.com/nexus/content/groups/public/");
                repository.setId("alimaven");
                dependenciesTask.addConfiguredRemoteRepository(repository);
                dependenciesTask.setProject(project);
                for (DependencyJar dependencyJar : dependencies) {
                    Dependency dependency = new Dependency();
                    dependency.setArtifactId(dependencyJar.getArtifactId());
                    dependency.setGroupId(dependencyJar.getGroupId());
                    dependency.setType(dependencyJar.getType());
                    dependency.setVersion(dependencyJar.getVersion());
                    if (dependencyJar.getClassifier() != null) {
                        dependency.setClassifier(dependencyJar.getClassifier());
                    }
                    dependenciesTask.addDependency(dependency);
                }
                dependenciesTask.execute();
    
                @SuppressWarnings("unchecked")
                Hashtable<String, String> artifacts = project.getProperties();
                URL[] urls = new URL[dependencies.length];
                for (int i = 0; i < urls.length; i++) {
                    try {
                        urls[i] = Util.url(artifacts.get(key(dependencies[i])));
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                }
                return urls;
            }
    
            @Override
            public URL getLocalArtifactUrl(DependencyJar dependency) {
                URL[] urls = getLocalArtifactUrls(dependency);
                if (urls.length > 0) {
                    return urls[0];
                }
                return null;
            }
    
            private String key(DependencyJar dependency) {
                String key =
                        dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getType();
                if (dependency.getClassifier() != null) {
                    key += ":" + dependency.getClassifier();
                }
                return key;
            }
        }
    
        @Override
        protected AndroidManifest getAppManifest(Config config) {
            AndroidManifest appManifest = super.getAppManifest(config);
            FsFile androidManifestFile = appManifest.getAndroidManifestFile();
    
            if (androidManifestFile.exists()) {
                return appManifest;
            } else {
                String moduleRoot = getModuleRootPath(config);
                androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("bundles", "manifests/full"));
                FsFile resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath().replace("/res", "").replace("bundles", "res"));
                FsFile assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath().replace("/assets", "").replace("bundles", "assets"));
                return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory);
            }
        }
    
        private String getModuleRootPath(Config config) {
            String moduleRoot = config.constants().getResource("").toString().replace("file:", "");
            return moduleRoot.substring(0, moduleRoot.indexOf("/build"));
        }
    }
    

    相关文章

      网友评论

        本文标题:Robolectric被墙下载缓慢的解决方法

        本文链接:https://www.haomeiwen.com/subject/fmfdxxtx.html