use of org.gradle.api.artifacts.ArtifactCollection in project atlas by alibaba.
the class BuildAtlasEnvTask method generate.
@TaskAction
void generate() throws TransformException {
Set<ResolvedArtifactResult> compileArtifacts = compileManifests.getArtifacts();
Set<ResolvedArtifactResult> jarArtifacts = compileJars.getArtifacts();
Set<ResolvedArtifactResult> nativeLibsArtifacts = nativeLibs.getArtifacts();
Set<ResolvedArtifactResult> javaResourcesArtifacts = javaResources.getArtifacts();
Set<ResolvedArtifactResult> androidRes = res.getArtifacts();
Set<ResolvedArtifactResult> androidAssets = assets.getArtifacts();
Set<ResolvedArtifactResult> androidRnames = symbolListWithPackageNames.getArtifacts();
AtlasDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
List<AwbBundle> bundles = new ArrayList<>();
bundles.add(androidDependencyTree.getMainBundle());
bundles.addAll(androidDependencyTree.getAwbBundles());
// this is no used ,if used in future add to transform!
Set<ResolvedArtifactResult> nativeLibsArtifacts2 = nativeLibs2.getArtifacts();
nativeLibsArtifacts.addAll(nativeLibsArtifacts2);
AtlasBuildContext.localLibs = nativeLibs2.getArtifactFiles().getFiles();
for (ResolvedArtifactResult resolvedArtifactResult : jarArtifacts) {
ComponentIdentifier componentIdentifier = resolvedArtifactResult.getId().getComponentIdentifier();
if (componentIdentifier instanceof DefaultModuleComponentIdentifier) {
allJars.add(new FileIdentity(((DefaultModuleComponentIdentifier) componentIdentifier).getGroup() + ":" + ((DefaultModuleComponentIdentifier) componentIdentifier).getModule(), resolvedArtifactResult.getFile(), resolvedArtifactResult.getId().getDisplayName().startsWith("classes.jar") ? false : true, false));
} else if (componentIdentifier instanceof DefaultProjectComponentIdentifier) {
String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
allJars.add(new FileIdentity(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile(), resolvedArtifactResult.getId().getDisplayName().startsWith("classes.jar") ? false : true, true));
} else if (componentIdentifier instanceof OpaqueComponentArtifactIdentifier) {
if (resolvedArtifactResult.getFile().getAbsolutePath().contains("renderscript"))
appLocalJars.add(new FileIdentity(componentIdentifier.getDisplayName(), resolvedArtifactResult.getFile(), true, false));
}
}
for (ResolvedArtifactResult resolvedArtifactResult : compileArtifacts) {
if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
allManifests.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult.getFile());
} else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
allManifests.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile());
}
}
for (ResolvedArtifactResult resolvedArtifactResult : nativeLibsArtifacts) {
if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
allSolibs.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult.getFile());
} else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
allSolibs.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile());
}
}
for (ResolvedArtifactResult resolvedArtifactResult : javaResourcesArtifacts) {
if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
allJavaRes.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult.getFile());
} else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
allJavaRes.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult.getFile());
}
}
for (ResolvedArtifactResult resolvedArtifactResult : androidRes) {
if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
allAndroidRes.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult);
} else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
allAndroidRes.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult);
}
}
for (ResolvedArtifactResult resolvedArtifactResult : androidAssets) {
if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
allAndroidAssets.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult);
} else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
allAndroidAssets.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult);
}
}
for (ResolvedArtifactResult resolvedArtifactResult : androidRnames) {
if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultModuleComponentIdentifier) {
allAndroidRnames.put(((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getGroup() + ":" + ((DefaultModuleComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getModule(), resolvedArtifactResult);
} else if (resolvedArtifactResult.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier) {
String projectPath = ((DefaultProjectComponentIdentifier) resolvedArtifactResult.getId().getComponentIdentifier()).getProjectPath();
allAndroidRnames.put(projectPath.substring(projectPath.lastIndexOf(":") + 1), resolvedArtifactResult);
}
}
// app localJar is not support , this may course duplicate localjars
appLocalJars.stream().forEach(fileIdentity -> AtlasBuildContext.atlasMainDexHelperMap.get(getVariantName()).addMainDex(fileIdentity));
AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
List<AndroidLibrary> androidLibraries = atlasDependencyTree.getAllAndroidLibrarys();
androidLibraries.stream().forEach(androidLibrary -> {
if (androidLibrary instanceof AtlasAndroidLibraryImpl) {
AndroidDependency fakeAndroidLibrary = ((AtlasAndroidLibraryImpl) androidLibrary).getAndroidLibrary();
File id = null;
if ((id = allManifests.get(androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId())) == null) {
id = allManifests.get(androidLibrary.getResolvedCoordinates().toString().split(":")[1]);
}
if (id == null) {
getLogger().warn("id == null---------------------" + androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId());
throw new GradleException("excute failed! ");
}
ReflectUtils.updateField(fakeAndroidLibrary, "extractedFolder", id.getParentFile());
ReflectUtils.updateField(fakeAndroidLibrary, "jarsRootFolder", id.getParentFile());
((AtlasAndroidLibraryImpl) androidLibrary).setAndroidLibrary(AndroidDependency.createExplodedAarLibrary(null, androidLibrary.getResolvedCoordinates(), androidLibrary.getName(), ((AtlasAndroidLibraryImpl) androidLibrary).getPath(), id.getParentFile()));
appVariantContext.manifestMap.put(androidLibrary.getManifest().getAbsolutePath(), appVariantContext.getModifyManifest(androidLibrary));
}
});
List<AndroidLibrary> mainDexAndroidLibraries = atlasDependencyTree.getMainBundle().getAndroidLibraries();
List<JavaLibrary> mainDexJarLibraries = atlasDependencyTree.getMainBundle().getJavaLibraries();
List<SoLibrary> mainSoLibraries = atlasDependencyTree.getMainBundle().getSoLibraries();
for (AndroidLibrary androidLibrary : mainDexAndroidLibraries) {
String name = androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId();
String moudleName = androidLibrary.getResolvedCoordinates().toString().split(":")[1];
fillMainManifest(name, moudleName);
fillMainJar(name, moudleName);
fillAllJavaRes(name, moudleName);
fillMainSolibs(name, moudleName);
}
for (JavaLibrary jarLibrary : mainDexJarLibraries) {
String moudleName = jarLibrary.getName().split(":")[1];
String name = jarLibrary.getResolvedCoordinates().getGroupId() + ":" + jarLibrary.getResolvedCoordinates().getArtifactId();
fillMainJar(name, moudleName);
fillAllJavaRes(name, moudleName);
}
for (SoLibrary soLibrary : mainSoLibraries) {
String name = soLibrary.getResolvedCoordinates().getGroupId() + ":" + soLibrary.getResolvedCoordinates().getArtifactId();
String moudleName = soLibrary.getResolvedCoordinates().toString().split(":")[1];
fillMainSolibs(name, moudleName);
}
for (AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
List<AndroidLibrary> awbAndroidLibraries = awbBundle.getAndroidLibraries();
List<JavaLibrary> awbJarLibraries = awbBundle.getJavaLibraries();
List<SoLibrary> awbSoLibraries = awbBundle.getSoLibraries();
for (AndroidLibrary androidLibrary : awbAndroidLibraries) {
String name = androidLibrary.getResolvedCoordinates().getGroupId() + ":" + androidLibrary.getResolvedCoordinates().getArtifactId();
String moudleName = androidLibrary.getResolvedCoordinates().toString().split(":")[1];
fillAwbManifest(name, moudleName, awbBundle);
fillAwbJar(name, moudleName, awbBundle);
fillAwbAllJavaRes(name, moudleName, awbBundle);
fillAwbSolibs(name, moudleName, awbBundle);
fillAwbAndroidRes(name, moudleName, awbBundle);
fillAwbAndroidAssets(name, moudleName, awbBundle);
fillAwbAndroidRs(name, moudleName, awbBundle);
}
for (JavaLibrary jarLibrary : awbJarLibraries) {
String moudleName = jarLibrary.getName().split(":")[1];
String name = jarLibrary.getResolvedCoordinates().getGroupId() + ":" + jarLibrary.getResolvedCoordinates().getArtifactId();
fillAwbJar(name, moudleName, awbBundle);
}
for (SoLibrary soLibrary : awbSoLibraries) {
String name = soLibrary.getResolvedCoordinates().getGroupId() + ":" + soLibrary.getResolvedCoordinates().getArtifactId();
String moudleName = soLibrary.getResolvedCoordinates().toString().split(":")[1];
fillAwbSolibs(name, moudleName, awbBundle);
}
String name = awbBundle.getResolvedCoordinates().getGroupId() + ":" + awbBundle.getResolvedCoordinates().getArtifactId();
String moudleName = awbBundle.getResolvedCoordinates().toString().split(":")[1];
fillAwbManifest(name, moudleName, awbBundle);
fillAwbJar(name, moudleName, awbBundle);
fillAwbAllJavaRes(name, moudleName, awbBundle);
fillAwbSolibs(name, moudleName, awbBundle);
fillAwbAndroidRes(name, moudleName, awbBundle);
fillAwbAndroidAssets(name, moudleName, awbBundle);
fillAwbAndroidRs(name, moudleName, awbBundle);
}
MergeResources mergeResources = appVariantContext.getScope().getMergeResourcesTask().get(new TaskContainerAdaptor(getProject().getTasks()));
try {
// mergeresources
Field field = MergeResources.class.getDeclaredField("libraries");
field.setAccessible(true);
field.set(mergeResources, new MainArtifactsCollection((ArtifactCollection) field.get(mergeResources), getProject(), mergeResources.getVariantName()));
appVariantOutputContext.getAwbTransformMap().values().stream().forEach(awbTransform -> {
if (isMBundle(appVariantContext, awbTransform.getAwbBundle())) {
try {
awbTransform.getAwbBundle().isMBundle = true;
awbTransform.getAwbBundle().bundleInfo.setIsMBundle(true);
field.set(mergeResources, new AppendMainArtifactsCollection(appVariantContext.getProject(), (ArtifactCollection) field.get(mergeResources), awbTransform.getAwbBundle(), ANDROID_RES));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
mergeResources.doLast(task -> FileUtils.listFiles(((MergeResources) task).getOutputDir(), new String[] { "xml" }, true).parallelStream().forEach(file -> {
if (!AppendMainArtifactsCollection.bundle2Map.containsKey(file.getName())) {
return;
}
List<String> lines = null;
List<String> newLines = new ArrayList<>();
try {
lines = FileUtils.readLines(file);
lines.forEach(s -> {
String s1 = s;
if (s.contains("http://schemas.android.com/apk/res/" + AppendMainArtifactsCollection.bundle2Map.get(file.getName()))) {
s1 = s.replace("http://schemas.android.com/apk/res/" + AppendMainArtifactsCollection.bundle2Map.get(file.getName()), "http://schemas.android.com/apk/res-auto");
}
newLines.add(s1);
});
FileUtils.writeLines(file, newLines);
} catch (IOException e) {
e.printStackTrace();
}
}));
// mergeSourcesSets
MergeSourceSetFolders mergeSourceSetFolders = appVariantContext.getScope().getMergeAssetsTask().get(new TaskContainerAdaptor(getProject().getTasks()));
Field assetsField = MergeSourceSetFolders.class.getDeclaredField("libraries");
assetsField.setAccessible(true);
assetsField.set(mergeSourceSetFolders, new MainArtifactsCollection((ArtifactCollection) assetsField.get(mergeSourceSetFolders), getProject(), mergeSourceSetFolders.getVariantName()));
appVariantOutputContext.getAwbTransformMap().values().stream().forEach(awbTransform -> {
if (isMBundle(appVariantContext, awbTransform.getAwbBundle())) {
try {
awbTransform.getAwbBundle().isMBundle = true;
awbTransform.getAwbBundle().bundleInfo.setIsMBundle(true);
assetsField.set(mergeSourceSetFolders, new AppendMainArtifactsCollection(appVariantContext.getProject(), (ArtifactCollection) assetsField.get(mergeSourceSetFolders), awbTransform.getAwbBundle(), ASSETS));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
AtlasBuildContext.atlasMainDexHelperMap.get(getVariantName()).getMainSoFiles().put(appVariantContext.getScope().getMergeNativeLibsOutputDir().getAbsolutePath(), true);
} catch (Exception e) {
}
// process resources
ProcessAndroidResources processAndroidResources = appVariantContext.getScope().getProcessResourcesTask().get(new TaskContainerAdaptor(appVariantContext.getProject().getTasks()));
FileCollection fileCollection = processAndroidResources.getSymbolListsWithPackageNames();
Set<String> filesNames = new HashSet<>();
for (String fileName : AtlasBuildContext.atlasMainDexHelperMap.get(getVariantName()).getMainManifestFiles().keySet()) {
filesNames.add(fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1));
}
FileCollection updateFileCollection = fileCollection.filter(element -> filesNames.contains(element.getParentFile().getParentFile().getName()));
ReflectUtils.updateField(processAndroidResources, "symbolListsWithPackageNames", updateFileCollection);
appVariantOutputContext.getAwbTransformMap().values().stream().forEach(awbTransform -> {
if (isMBundle(appVariantContext, awbTransform.getAwbBundle())) {
awbTransform.getAwbBundle().isMBundle = true;
awbTransform.getAwbBundle().bundleInfo.setIsMBundle(true);
FileCollection fc = new AppendMainArtifactsCollection(appVariantContext.getProject(), processAndroidResources.getSymbolListsWithPackageNames(), awbTransform.getAwbBundle(), SYMBOL_LIST_WITH_PACKAGE_NAME).getArtifactFiles();
ReflectUtils.updateField(processAndroidResources, "symbolListsWithPackageNames", fc);
}
});
appVariantContext.processResAwbsTask.mainDexSymbolFileCollection = updateFileCollection;
// FileCollection fs = appVariantContext.getScope().getArtifactFileCollection(AndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH,AndroidArtifacts.ArtifactScope.ALL,AndroidArtifacts.ArtifactType.CLASSES);
// fs.getFiles().forEach(new Consumer<File>() {
// @Override
// public void accept(File file) {
// if (file.exists()){
// try {
// JarFile jarFile = new JarFile(file);
// Enumeration<JarEntry>enumeration = jarFile.entries();
// while (enumeration.hasMoreElements()){
// JarEntry jarEntry = enumeration.nextElement();
// if (jarEntry.getName().endsWith(".class")){
// ClassReader classReader = new ClassReader(jarFile.getInputStream(jarEntry));
// String[]ss = classReader.getInterfaces();
// if (ss!= null){
// for (String s:ss){
// if (s.contains("IExternalComponentGetter")||s.contains("IExternalComponentGetter.class")){
// System.out.println("IExternalComponentGetter:"+jarEntry.getName());
// }else if (s.contains("IExternalModuleGetter")||s.contains("IExternalModuleGetter.class")){
// System.out.println("IExternalModuleGetter:"+jarEntry.getName());
// }
// }
// }
//
// }
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// });
allManifests.clear();
allJavaRes.clear();
allSolibs.clear();
allJars.clear();
allAndroidAssets.clear();
allAndroidRes.clear();
//
// try {
// duplicateClazzNote();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
use of org.gradle.api.artifacts.ArtifactCollection in project atlas by alibaba.
the class AtlasFeatureTaskManager method runTask.
@Override
public void runTask() {
if (featureExtension.getBaseFeature()) {
return;
}
featureExtension.getFeatureVariants().forEach(featureVariant -> {
FeatureVariantContext featureVariantContext = new FeatureVariantContext((FeatureVariantImpl) featureVariant, project, atlasExtension, featureExtension);
ArtifactCollection allArtifacts = featureVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.EXTERNAL, AndroidArtifacts.ArtifactType.CLASSES);
ArtifactCollection artifacts = featureVariantContext.getScope().getArtifactCollection(AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AndroidArtifacts.ArtifactType.FEATURE_TRANSITIVE_DEPS);
ArtifactCollection filterArtifacts = new FilteredArtifactCollection(featureVariantContext.getProject(), allArtifacts, artifacts.getArtifactFiles());
List<TransformTask> transformTasks = TransformManager.findTransformTaskByTransformType(featureVariantContext, FixStackFramesTransform.class);
if (transformTasks != null) {
for (TransformTask transformTask : transformTasks) {
try {
Field field = StreamBasedTask.class.getDeclaredField("consumedInputStreams");
Field field1 = StreamBasedTask.class.getDeclaredField("referencedInputStreams");
field1.setAccessible(true);
field.setAccessible(true);
Collection<TransformStream> consumedInputStreams = (Collection<TransformStream>) field.get(transformTask);
Collection<TransformStream> referencedInputStreams = (Collection<TransformStream>) field1.get(transformTask);
for (TransformStream stream : consumedInputStreams) {
if (stream.getContentTypes().contains(QualifiedContent.DefaultContentType.CLASSES) && stream.getScopes().contains(QualifiedContent.Scope.EXTERNAL_LIBRARIES)) {
ReflectUtils.updateField(stream, "fileCollection", filterArtifacts.getArtifactFiles());
ReflectUtils.updateField(stream, "artifactCollection", filterArtifacts);
break;
}
}
for (TransformStream transformStream : referencedInputStreams) {
if (transformStream.getContentTypes().contains(QualifiedContent.DefaultContentType.CLASSES) && transformStream.getScopes().contains(QualifiedContent.Scope.PROVIDED_ONLY)) {
ReflectUtils.updateField(transformStream, "fileCollection", project.files());
// ReflectUtils.updateField(transformStream, "artifactCollection", filterArtifacts);
}
}
} catch (Exception e) {
}
}
}
featureVariantContext.getScope().getProcessResourcesTask().get(new TaskContainerAdaptor(featureVariantContext.getProject().getTasks())).setEnableAapt2(true);
});
featureExtension.getLibraryVariants().forEach(libraryVariant -> {
LibVariantContext libVariantContext = new LibVariantContext((LibraryVariantImpl) libraryVariant, project, atlasExtension, featureExtension);
TBuildType tBuildType = libVariantContext.getBuildType();
if (null != tBuildType) {
try {
new AwoPropHandler().process(tBuildType, atlasExtension.getBundleConfig());
} catch (Exception e) {
throw new GradleException("process awo exception", e);
}
}
AwbGenerator awbGenerator = new AwbGenerator(atlasExtension);
Collection<BaseVariantOutput> list = libVariantContext.getBaseVariant().getOutputs();
if (null != list) {
for (BaseVariantOutput libVariantOutputData : list) {
Zip zipTask = ((LibraryVariantOutput) (libVariantOutputData)).getPackageLibrary();
if (atlasExtension.getBundleConfig().isJarEnabled()) {
new JarExtractTask().generateJarArtifict(zipTask);
}
// Build the awb and extension
// if (atlasExtension.getBundleConfig().isAwbBundle()) {
awbGenerator.generateAwbArtifict(zipTask, libVariantContext);
if (null != tBuildType && (StringUtils.isNotEmpty(tBuildType.getBaseApDependency()) || null != tBuildType.getBaseApFile()) && libraryVariant.getName().equals("debug")) {
atlasExtension.getTBuildConfig().setUseCustomAapt(true);
libVariantContext.setBundleTask(zipTask);
try {
libVariantContext.setAwbBundle(awbGenerator.createAwbBundle(libVariantContext));
} catch (IOException e) {
throw new GradleException("set awb bundle error");
}
}
}
}
});
}
use of org.gradle.api.artifacts.ArtifactCollection in project atlas by alibaba.
the class AtlasDependencyGraph method computeArtifactCollection.
public static ArtifactCollection computeArtifactCollection(VariantScope variantScope, @NonNull AtlasAndroidArtifacts.ConsumedConfigType configType, @NonNull AndroidArtifacts.ArtifactScope scope, @NonNull AtlasAndroidArtifacts.AtlasArtifactType artifactType) {
Configuration configuration;
switch(configType) {
case COMPILE_CLASSPATH:
configuration = variantScope.getVariantData().getVariantDependency().getCompileClasspath();
break;
case RUNTIME_CLASSPATH:
configuration = variantScope.getVariantData().getVariantDependency().getRuntimeClasspath();
break;
case BUNDLECOMPILE_CLASSPATH:
configuration = variantScope.getGlobalScope().getProject().getConfigurations().maybeCreate(AtlasPlugin.BUNDLE_COMPILE);
break;
case ANNOTATION_PROCESSOR:
configuration = variantScope.getVariantData().getVariantDependency().getAnnotationProcessorConfiguration();
break;
case METADATA_VALUES:
configuration = variantScope.getVariantData().getVariantDependency().getMetadataValuesConfiguration();
break;
default:
throw new RuntimeException("unknown ConfigType value");
}
Action<AttributeContainer> attributes = container -> container.attribute(ARTIFACT_TYPE, artifactType.getType());
Spec<ComponentIdentifier> filter = getComponentFilter(scope);
boolean lenientMode = Boolean.TRUE.equals(variantScope.getGlobalScope().getProjectOptions().get(BooleanOption.IDE_BUILD_MODEL_ONLY));
ArtifactCollection artifacts = configuration.getIncoming().artifactView(config -> {
config.attributes(attributes);
if (filter != null) {
config.componentFilter(filter);
}
// TODO somehow read the unresolved dependencies?
config.lenient(lenientMode);
}).getArtifacts();
if (configType == AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH && variantScope.getVariantConfiguration().getType() == VariantType.FEATURE && artifactType != AtlasAndroidArtifacts.AtlasArtifactType.FEATURE_TRANSITIVE_DEPS) {
artifacts = new FilteredArtifactCollection(variantScope.getGlobalScope().getProject(), artifacts, computeArtifactCollection(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, scope, AtlasAndroidArtifacts.AtlasArtifactType.FEATURE_TRANSITIVE_DEPS).getArtifactFiles());
}
return artifacts;
}
use of org.gradle.api.artifacts.ArtifactCollection in project atlas by alibaba.
the class AtlasDependencyGraph method createLevel4DependencyGraph.
/**
* Create a level 4 dependency graph.
*
* @see AndroidProject#MODEL_LEVEL_4_NEW_DEP_MODEL
*/
public DependencyGraphs createLevel4DependencyGraph(@NonNull VariantScope variantScope, boolean withFullDependency, boolean downloadSources, @NonNull Consumer<SyncIssue> failureConsumer) {
try {
// get the compile artifact first.
Set<AtlasDependencyGraph.HashableResolvedArtifactResult> compileArtifacts = getAllArtifacts(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH, dependencyFailureHandler);
compileArtifacts.addAll(getAllArtifacts(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.BUNDLECOMPILE_CLASSPATH, dependencyFailureHandler));
// the runtime-only is never used from the IDE.
if (downloadSources) {
Set<ComponentIdentifier> ids = Sets.newHashSetWithExpectedSize(compileArtifacts.size());
for (HashableResolvedArtifactResult artifact : compileArtifacts) {
ids.add(artifact.getId().getComponentIdentifier());
}
handleSources(variantScope.getGlobalScope().getProject(), ids, failureConsumer);
}
// provided bit.
if (!withFullDependency) {
// get the runtime artifacts. We only care about the ComponentIdentifier so we don't
// need to call getAllArtifacts() which computes a lot more many things, and takes
// longer on large projects.
// Instead just get all the jars to get all the dependencies.
ArtifactCollection runtimeArtifactCollection = computeArtifactList(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, AndroidArtifacts.ArtifactScope.ALL, AtlasAndroidArtifacts.AtlasArtifactType.JAR);
// build a list of the runtime ComponentIdentifiers
final Set<ResolvedArtifactResult> runtimeArtifacts = runtimeArtifactCollection.getArtifacts();
final Set<ComponentIdentifier> runtimeIdentifiers = Sets.newHashSetWithExpectedSize(runtimeArtifacts.size());
for (ResolvedArtifactResult result : runtimeArtifacts) {
runtimeIdentifiers.add(result.getId().getComponentIdentifier());
}
List<String> providedAddresses = Lists.newArrayList();
List<GraphItem> compileItems = Lists.newArrayListWithCapacity(compileArtifacts.size());
for (AtlasDependencyGraph.HashableResolvedArtifactResult artifact : compileArtifacts) {
final GraphItemImpl graphItem = new GraphItemImpl(computeAddress(artifact), ImmutableList.of());
compileItems.add(graphItem);
sLibraryCache.get(artifact);
sLibraryMap.put(computeAddress(artifact), artifact);
if (!runtimeIdentifiers.contains(artifact.getId().getComponentIdentifier())) {
providedAddresses.add(graphItem.getArtifactAddress());
}
}
return new SimpleDependencyGraphsImpl(compileItems, providedAddresses);
}
// now build the list of compile items
List<GraphItem> compileItems = Lists.newArrayListWithCapacity(compileArtifacts.size());
for (AtlasDependencyGraph.HashableResolvedArtifactResult artifact : compileArtifacts) {
compileItems.add(new GraphItemImpl(computeAddress(artifact), ImmutableList.of()));
sLibraryCache.get(artifact);
sLibraryMap.put(computeAddress(artifact), artifact);
}
// in this mode, compute GraphItem for the runtime configuration
// get the runtime artifacts.
Set<AtlasDependencyGraph.HashableResolvedArtifactResult> runtimeArtifacts = getAllArtifacts(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, dependencyFailureHandler);
runtimeArtifacts.addAll(getAllArtifacts(variantScope, AtlasAndroidArtifacts.ConsumedConfigType.BUNDLECOMPILE_CLASSPATH, dependencyFailureHandler));
List<GraphItem> runtimeItems = Lists.newArrayListWithCapacity(runtimeArtifacts.size());
for (AtlasDependencyGraph.HashableResolvedArtifactResult artifact : runtimeArtifacts) {
runtimeItems.add(new GraphItemImpl(computeAddress(artifact), ImmutableList.of()));
sLibraryCache.get(artifact);
sLibraryMap.put(computeAddress(artifact), artifact);
}
// compute the provided dependency list, by comparing the compile and runtime items
List<GraphItem> providedItems = Lists.newArrayList(compileItems);
providedItems.removeAll(runtimeItems);
final ImmutableList<String> providedAddresses = providedItems.stream().map(GraphItem::getArtifactAddress).collect(ImmutableCollectors.toImmutableList());
return new FullDependencyGraphsImpl(compileItems, runtimeItems, providedAddresses, // FIXME: actually get skip list
ImmutableList.of());
} finally {
dependencyFailureHandler.collectIssues().forEach(failureConsumer);
}
}
use of org.gradle.api.artifacts.ArtifactCollection in project atlas by alibaba.
the class AtlasDependencyGraph method getAllArtifacts.
/**
* Returns a set of HashableResolvedArtifactResult where the {@link
* ArtifactDependencyGraph.HashableResolvedArtifactResult#getDependencyType()} and {@link
* ArtifactDependencyGraph.HashableResolvedArtifactResult#isWrappedModule()} fields have been setup properly.
*/
public static Set<AtlasDependencyGraph.HashableResolvedArtifactResult> getAllArtifacts(@NonNull VariantScope variantScope, @NonNull AtlasAndroidArtifacts.ConsumedConfigType consumedConfigType, @Nullable DependencyFailureHandler dependencyFailureHandler) {
// FIXME change the way we compare dependencies b/64387392
// we need to figure out the following:
// - Is it an external dependency or a sub-project?
// - Is it an android or a java dependency
// Querying for JAR type gives us all the dependencies we care about, and we can use this
// to differentiate external vs sub-projects (to a certain degree).
ArtifactCollection allArtifactList = computeArtifactList(variantScope, consumedConfigType, AndroidArtifacts.ArtifactScope.ALL, AtlasAndroidArtifacts.AtlasArtifactType.JAR);
// Then we can query for MANIFEST that will give us only the Android project so that we
// can detect JAVA vs ANDROID.
ArtifactCollection manifestList = computeArtifactList(variantScope, consumedConfigType, AndroidArtifacts.ArtifactScope.ALL, AtlasAndroidArtifacts.AtlasArtifactType.MANIFEST);
// We still need to understand wrapped jars and aars. The former is difficult (TBD), but
// the latter can be done by querying for EXPLODED_AAR. If a sub-project is in this list,
// then we need to override the type to be external, rather than sub-project.
// This is why we query for Scope.ALL
// But we also simply need the exploded AARs for external Android dependencies so that
// Studio can access the content.
ArtifactCollection explodedAarList = computeArtifactList(variantScope, consumedConfigType, AndroidArtifacts.ArtifactScope.ALL, AtlasAndroidArtifacts.AtlasArtifactType.EXPLODED_AAR);
ArtifactCollection explodedAwbList = computeArtifactList(variantScope, consumedConfigType, AndroidArtifacts.ArtifactScope.ALL, AtlasAndroidArtifacts.AtlasArtifactType.EXPLODED_AWB);
// We also need the actual AARs so that we can get the artifact location and find the source
// location from it.
ArtifactCollection aarList = computeArtifactList(variantScope, consumedConfigType, AndroidArtifacts.ArtifactScope.EXTERNAL, AtlasAndroidArtifacts.AtlasArtifactType.AAR);
ArtifactCollection awbList = computeArtifactList(variantScope, consumedConfigType, AndroidArtifacts.ArtifactScope.EXTERNAL, AtlasAndroidArtifacts.AtlasArtifactType.AWB);
// collect dependency resolution failures
if (dependencyFailureHandler != null) {
// compute the name of the configuration
dependencyFailureHandler.addErrors(variantScope.getGlobalScope().getProject().getPath() + "@" + variantScope.getFullVariantName() + "/" + consumedConfigType.getName(), allArtifactList.getFailures());
}
// build a list of wrapped AAR, and a map of all the exploded-aar artifacts
final Set<ComponentIdentifier> wrapperModules = new HashSet<>();
final Set<ResolvedArtifactResult> explodedAarArtifacts = explodedAarList.getArtifacts();
final Set<ResolvedArtifactResult> explodedAwbArtifacts = explodedAwbList.getArtifacts();
final Map<ComponentIdentifier, ResolvedArtifactResult> explodedAarResults = Maps.newHashMapWithExpectedSize(explodedAarArtifacts.size());
final Map<ComponentIdentifier, ResolvedArtifactResult> explodedAwbResults = Maps.newHashMapWithExpectedSize(explodedAwbArtifacts.size());
for (ResolvedArtifactResult result : explodedAarArtifacts) {
final ComponentIdentifier componentIdentifier = result.getId().getComponentIdentifier();
if (componentIdentifier instanceof ProjectComponentIdentifier) {
wrapperModules.add(componentIdentifier);
}
explodedAarResults.put(componentIdentifier, result);
}
for (ResolvedArtifactResult result : explodedAwbArtifacts) {
final ComponentIdentifier componentIdentifier = result.getId().getComponentIdentifier();
if (componentIdentifier instanceof ProjectComponentIdentifier) {
wrapperModules.add(componentIdentifier);
}
explodedAwbResults.put(componentIdentifier, result);
}
final Set<ResolvedArtifactResult> aarArtifacts = aarList.getArtifacts();
final Set<ResolvedArtifactResult> awbArtifacts = awbList.getArtifacts();
final Map<ComponentIdentifier, ResolvedArtifactResult> aarResults = Maps.newHashMapWithExpectedSize(aarArtifacts.size());
final Map<ComponentIdentifier, ResolvedArtifactResult> awbResults = Maps.newHashMapWithExpectedSize(awbArtifacts.size());
for (ResolvedArtifactResult result : aarArtifacts) {
aarResults.put(result.getId().getComponentIdentifier(), result);
}
for (ResolvedArtifactResult result : awbArtifacts) {
awbResults.put(result.getId().getComponentIdentifier(), result);
}
// build a list of android dependencies based on them publishing a MANIFEST element
final Set<ResolvedArtifactResult> manifestArtifacts = manifestList.getArtifacts();
final Set<ComponentIdentifier> manifestIds = Sets.newHashSetWithExpectedSize(manifestArtifacts.size());
for (ResolvedArtifactResult result : manifestArtifacts) {
manifestIds.add(result.getId().getComponentIdentifier());
}
// build the final list, using the main list augmented with data from the previous lists.
final Set<ResolvedArtifactResult> allArtifacts = allArtifactList.getArtifacts();
// use a linked hash set to keep the artifact order.
final Set<AtlasDependencyGraph.HashableResolvedArtifactResult> artifacts = Sets.newLinkedHashSetWithExpectedSize(allArtifacts.size());
for (ResolvedArtifactResult artifact : allArtifacts) {
final ComponentIdentifier componentIdentifier = artifact.getId().getComponentIdentifier();
// check if this is a wrapped module
boolean isWrappedModule = wrapperModules.contains(componentIdentifier);
// check if this is an android external module. In this case, we want to use the exploded
// aar as the artifact we depend on rather than just the JAR, so we swap out the
// ResolvedArtifactResult.
AtlasDependencyGraph.DependencyType dependencyType = DependencyType.JAVA;
// optional result that will point to the artifact (AAR) when the current result
// is the exploded AAR.
ResolvedArtifactResult aarResult = null;
if (manifestIds.contains(componentIdentifier)) {
dependencyType = DependencyType.ANDROID;
// if it's an android dependency, we swap out the manifest result for the exploded
// AAR result.
// If the exploded AAR is null then it's a sub-project and we can keep the manifest
// as the Library we'll create will be a ModuleLibrary which doesn't care about
// the artifact file anyway.
ResolvedArtifactResult explodedAwb = explodedAwbResults.get(componentIdentifier);
if (explodedAwb != null) {
artifact = explodedAwb;
dependencyType = DependencyType.AWB;
aarResult = awbResults.get(componentIdentifier);
} else {
ResolvedArtifactResult explodedAar = explodedAarResults.get(componentIdentifier);
if (explodedAar != null) {
artifact = explodedAar;
aarResult = aarResults.get(componentIdentifier);
}
}
// and we need the AAR itself (if it exists)
}
artifacts.add(new AtlasDependencyGraph.HashableResolvedArtifactResult(artifact, dependencyType, isWrappedModule, aarResult));
}
return artifacts;
}
Aggregations