use of com.android.build.gradle.internal.scope.VariantScope in project atlas by alibaba.
the class AwbProguradHook method hookProguardTask.
/**
* hook混淆的任务,加入awb的混淆配置
*
* @param appVariantContext
*/
public void hookProguardTask(final AppVariantContext appVariantContext) {
final VariantScope variantScope = appVariantContext.getScope();
List<TransformTask> proguaradTransformTasks = getTransformTaskByTransformType(appVariantContext, ProGuardTransform.class);
for (TransformTask proguaradTransformTask : proguaradTransformTasks) {
final ProGuardTransform proGuardTransform = (ProGuardTransform) proguaradTransformTask.getTransform();
if (null != proGuardTransform) {
proguaradTransformTask.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
GlobalScope globalScope = variantScope.getGlobalScope();
File proguardOut = new File(Joiner.on(File.separatorChar).join(String.valueOf(globalScope.getBuildDir()), FD_OUTPUTS, "mapping", variantScope.getVariantConfiguration().getDirName()));
//为了方便排查,先把configuration打印到目录
proGuardTransform.printconfiguration(new File(proguardOut, "tmp_config.cfg"));
final File outConfigFile = new File(proguardOut, "awb_config.cfg");
//增加awb的配置
AndroidDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
if (null == dependencyTree) {
throw new StopExecutionException("DependencyTree cannot be null!");
}
if (dependencyTree.getAwbBundles().size() > 0) {
BaseVariantOutputData vod = appVariantContext.getVariantData().getOutputs().get(0);
AppVariantOutputContext appVariantOutputContext = getAppVariantOutputContext(appVariantContext, vod);
File awbObfuscatedDir = new File(globalScope.getIntermediatesDir(), "/classes-proguard/" + variantScope.getVariantConfiguration().getDirName());
AwbProguardConfiguration awbProguardConfiguration = new AwbProguardConfiguration(appVariantOutputContext.getAwbTransformMap().values(), awbObfuscatedDir, appVariantOutputContext);
try {
awbProguardConfiguration.printConfigFile(outConfigFile);
} catch (IOException e) {
throw new GradleException("", e);
}
proGuardTransform.setConfigurationFiles(new Supplier<Collection<File>>() {
@Override
public Collection<File> get() {
Set<File> proguardFiles = new HashSet<File>();
((HashSet<File>) proguardFiles).add(outConfigFile);
return proguardFiles;
}
});
}
File mappingFile = null;
if (null != appVariantContext.apContext.getApExploredFolder() && appVariantContext.apContext.getApExploredFolder().exists()) {
mappingFile = new File(appVariantContext.apContext.getApExploredFolder(), "mapping.txt");
} else {
mappingFile = new File(appVariantContext.getScope().getGlobalScope().getProject().getProjectDir(), "mapping.txt");
}
if (null != mappingFile && mappingFile.exists()) {
proGuardTransform.applyTestedMapping(mappingFile);
}
}
});
}
}
}
use of com.android.build.gradle.internal.scope.VariantScope in project atlas by alibaba.
the class AtlasProguardHelper method doBundleProguard.
public static void doBundleProguard(final AppVariantContext appVariantContext, List<File> mainDexJars) throws Exception {
Profiler.enter("preparebundleproguard");
VariantScope variantScope = appVariantContext.getScope();
AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(variantScope.getVariantConfiguration().getFullName());
if (dependencyTree.getAwbBundles().isEmpty()) {
return;
}
List<File> libs = new ArrayList<>(appVariantContext.getScope().getGlobalScope().getAndroidBuilder().getBootClasspath(true));
// All dependent classes
Profiler.enter("getDefaultClasses");
Set<String> defaultLibClasses = getClassList(libs);
Profiler.release();
libs.addAll(mainDexJars);
// Get the basic proguard configuration
List<File> defaultProguardFiles = new ArrayList<>(appVariantContext.getVariantConfiguration().getBuildType().getProguardFiles());
Collections.sort(defaultProguardFiles);
BaseVariantOutput vod = (BaseVariantOutput) appVariantContext.getVariantOutputData().iterator().next();
AppVariantOutputContext appVariantOutputContext = appVariantContext.getAppVariantOutputContext(ApkDataUtils.get(vod));
int parallelCount = appVariantContext.getAtlasExtension().getTBuildConfig().getProguardParallelCount();
// //FIXME
// parallelCount = 1;
Map<BundleInfo, AwbTransform> bundleInfoAwbTransformMap = new HashMap<>();
for (AwbTransform awbTransform : appVariantOutputContext.getAwbTransformMap().values()) {
bundleInfoAwbTransformMap.put(awbTransform.getAwbBundle().bundleInfo, awbTransform);
}
Profiler.release();
BundleGraphExecutor.execute(new ArrayList<>(bundleInfoAwbTransformMap.keySet()), parallelCount, new BundleItemRunner() {
@Override
public void execute(BundleItem bundleItem) {
try {
Input input = new Input();
AwbTransform awbTransform = bundleInfoAwbTransformMap.get(bundleItem.bundleInfo);
input.getAwbBundles().add(awbTransform);
for (BundleInfo sub : bundleItem.circles) {
input.getAwbBundles().add(bundleInfoAwbTransformMap.get(sub));
}
input.getDefaultProguardFiles().addAll(defaultProguardFiles);
input.getLibraries().addAll(libs);
input.getDefaultLibraryClasses().addAll(defaultLibClasses);
input.printConfiguration = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "proguard.cfg");
input.printUsage = new File(appVariantContext.getAwbProguardDir(input.getAwbBundles().get(0).getAwbBundle()), "usage.cfg");
addLibraryProguardFiles(appVariantContext, input);
addChildDependency(bundleItem, input, bundleInfoAwbTransformMap);
addParentKeeps(bundleItem, input, awbTransform, bundleInfoAwbTransformMap, appVariantContext);
BundleProguarder.execute(appVariantContext, input);
} catch (Exception e) {
e.printStackTrace();
throw new GradleException("proguard " + bundleItem.bundleInfo.getPkgName(), e);
}
}
});
}
use of com.android.build.gradle.internal.scope.VariantScope in project atlas by alibaba.
the class PostProcessManifestAction method execute.
@Override
public void execute(Task task) {
AtlasExtension atlasExtension = appVariantContext.getAtlasExtension();
File bundleBaseLineInfo = appVariantContext.getBundleBaseInfoFile();
VariantScope variantScope = appVariantContext.getScope();
GradleVariantConfiguration config = variantScope.getVariantConfiguration();
AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(config.getFullName());
File androidManifest = null;
File file = variantScope.getInstantRunManifestOutputDirectory();
if (null != file && file.exists() && variantScope.getInstantRunBuildContext().isInInstantRunMode()) {
androidManifest = FileUtils.join(baseVariantOutputData.getProcessManifest().getInstantRunManifestOutputDirectory(), baseVariantOutputData.getDirName(), SdkConstants.ANDROID_MANIFEST_XML);
} else {
androidManifest = FileUtils.join(baseVariantOutputData.getProcessManifest().getManifestOutputDirectory(), baseVariantOutputData.getDirName(), SdkConstants.ANDROID_MANIFEST_XML);
}
try {
Result result = ManifestFileUtils.postProcessManifests(androidManifest, getLibManifestMap(), getLibManifestDepenendyMap(), bundleBaseLineInfo, atlasExtension.manifestOptions, isMultiDexEnabled(), variantScope.getInstantRunBuildContext().isInInstantRunMode(), appVariantContext.getBuildType().isDebuggable(), atlasExtension.getTBuildConfig().getOutOfApkBundles(), atlasExtension.getTBuildConfig().getInsideOfApkBundles(), atlasExtension.getTBuildConfig().isPushInstall());
File proxySrcDir = appVariantContext.getAtlasProxySourceDir();
if (AtlasProxy.genProxyJavaSource(proxySrcDir, result)) {
// appVariantContext.getVariantData().javacTask.source(proxySrcDir);
}
ManifestHelper.checkManifest(appVariantContext, androidManifest, dependencyTree, atlasExtension);
} catch (DocumentException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
// ManifestFileUtils.postProcessManifests(
// instantRunAndroidManifest,
// getLibManifestMap(),
// getLibManifestDepenendyMap(),
// bundleBaseLineInfo,
// atlasExtension.manifestOptions,
// isMultiDexEnabled(),
// true,
// appVariantContext.getBuildType().isDebuggable(),
// atlasExtension.getTBuildConfig()
// .getOutOfApkBundles(),atlasExtension.getTBuildConfig().getInsideOfApkBundles(),atlasExtension.getTBuildConfig().isPushInstall());
// }
// manifest list check
// TODO??
// AtlasBuildContext.androidBuilderMap.get(appVariantContext.getProject()).generateKeepList(
// baseVariantOutputData.manifestProcessorTask.getManifestOutputFile(),
// appVariantContext.getScope()
// .getManifestKeepListProguardFile());
}
use of com.android.build.gradle.internal.scope.VariantScope in project atlas by alibaba.
the class PreProcessManifestAction method execute.
@Override
public void execute(Task task) {
AtlasExtension atlasExtension = appVariantContext.getAtlasExtension();
ManifestProcessorTask manifestProcessorTask = baseVariantOutput.getProcessManifest();
Set<String> notMergedArtifacts = Sets.newHashSet();
if (null != atlasExtension.getManifestOptions() && null != atlasExtension.getManifestOptions().getNotMergedBundles()) {
notMergedArtifacts = atlasExtension.getManifestOptions().getNotMergedBundles();
}
if (manifestProcessorTask instanceof MergeManifests) {
MergeManifests mergeManifests = (MergeManifests) manifestProcessorTask;
VariantScope variantScope = appVariantContext.getScope();
GradleVariantConfiguration config = variantScope.getVariantConfiguration();
AtlasDependencyTree dependencyTree = AtlasBuildContext.androidDependencyTrees.get(config.getFullName());
List<ManifestProvider> bundleProviders = ManifestHelper.getBundleManifest(appVariantContext, dependencyTree, atlasExtension);
List<ManifestProvider> mainDexProviders = ManifestHelper.getMainDexManifest(appVariantContext, dependencyTree, atlasExtension);
List<ManifestProvider> allManifest = new ArrayList<>();
modifyForIncremental(mergeManifests, allManifest);
// allManifest.addAll(ManifestHelper.convert(mergeManifests.getProviders(), appVariantContext));
allManifest.addAll(bundleProviders);
allManifest.addAll(mainDexProviders);
AtlasBuildContext.androidBuilderMap.get(appVariantContext.getProject()).manifestProviders = allManifest;
mergeManifests.setAndroidBuilder(AtlasBuildContext.androidBuilderMap.get(appVariantContext.getProject()));
// if (sLogger.isInfoEnabled()) {
// for (ManifestProvider manifestProvider : allManifest) {
// sLogger.warn("[manifestLibs] " + manifestProvider.getManifest().getAbsolutePath());
// }
// }
// Without this step, each time getLibraries It's going to be recomputed from the mapping
// mergeManifests.(allManifest);
}
}
use of com.android.build.gradle.internal.scope.VariantScope in project atlas by alibaba.
the class ProcessResAwbsTask method run.
@TaskAction
void run() throws ExecutionException, InterruptedException {
AtlasDependencyTree atlasDependencyTree = AtlasBuildContext.androidDependencyTrees.get(getVariantName());
if (null == atlasDependencyTree) {
return;
}
final VariantScope outputScope = appVariantOutputContext.getScope();
ExecutorServicesHelper executorServicesHelper = new ExecutorServicesHelper(taskName, getLogger(), 0);
List<Runnable> runnables = new ArrayList<>();
for (final AwbBundle awbBundle : atlasDependencyTree.getAwbBundles()) {
if (awbBundle.isMBundle) {
continue;
}
runnables.add(new Runnable() {
@Override
public void run() {
File symbolLocation = new File(outputScope.getGlobalScope().getIntermediatesDir(), "awb-symbols/" + outputScope.getVariantConfiguration().getDirName() + "/" + awbBundle.getName());
// Write the resources to the ap for debug
if ("debug".equals(appVariantOutputContext.getVariantContext().getBaseVariantData().getName())) {
appVariantOutputContext.appBuildInfo.getOtherFilesMap().put("awo/" + awbBundle.getPackageName() + ".R.txt", new File(symbolLocation, "R.txt"));
}
ProcessAwbAndroidResources.ConfigAction configAction = new ProcessAwbAndroidResources.ConfigAction(outputScope, symbolLocation, true, awbBundle, getBuilder(), appVariantOutputContext, baseVariantOutput);
ProcessAwbAndroidResources processAwbAndroidResources = TaskCreater.create(getProject(), configAction.getName(), configAction.getType());
processAwbAndroidResources.mainDexSymbolFileCollection = mainDexSymbolFileCollection;
appVariantContext.awbsProcessResourcesTask.put(awbBundle.getName(), processAwbAndroidResources);
configAction.execute(processAwbAndroidResources);
try {
processAwbAndroidResources.doFullTaskAction();
} catch (IOException e) {
throw new GradleException("process awb res exception", e);
}
}
});
}
executorServicesHelper.execute(runnables);
}
Aggregations