use of com.android.builder.model.JavaArtifact in project android by JetBrains.
the class AndroidJunitPatcher method patchJavaParameters.
@Override
public void patchJavaParameters(@Nullable Module module, @NotNull JavaParameters javaParameters) {
// Only patch if the project is a Gradle project.
if (module == null || !isBuildWithGradle(module.getProject())) {
return;
}
AndroidModuleModel androidModel = AndroidModuleModel.get(module);
if (androidModel == null) {
return;
}
// Modify the class path only if we're dealing with the unit test artifact.
JavaArtifact testArtifact = androidModel.getUnitTestArtifactInSelectedVariant();
if (testArtifact == null) {
return;
}
PathsList classPath = javaParameters.getClassPath();
TestArtifactSearchScopes testScopes = TestArtifactSearchScopes.get(module);
if (testScopes == null) {
return;
}
// Filter the library / module dependencies that are in android test
FileRootSearchScope excludeScope = testScopes.getUnitTestExcludeScope();
// complexity. TODO change the {@code PathList} API.
for (String path : classPath.getPathList()) {
if (excludeScope.accept(new File(path))) {
classPath.remove(path);
}
}
AndroidPlatform platform = AndroidPlatform.getInstance(module);
if (platform == null) {
return;
}
String originalClassPath = classPath.getPathsString();
try {
handlePlatformJar(classPath, platform, testArtifact);
handleJavaResources(module, androidModel, classPath);
} catch (RuntimeException e) {
throw new RuntimeException(String.format("Error patching the JUnit class path. Original class path:%n%s", originalClassPath), e);
}
}
use of com.android.builder.model.JavaArtifact in project android by JetBrains.
the class CompilerOutputModuleSetupStep method doSetUpModule.
@Override
protected void doSetUpModule(@NotNull Module module, @NotNull IdeModifiableModelsProvider ideModelsProvider, @NotNull AndroidModuleModel androidModel, @Nullable SyncAction.ModuleModels gradleModels, @Nullable ProgressIndicator indicator) {
GradleVersion modelVersion = androidModel.getModelVersion();
if (modelVersion == null) {
// We are dealing with old model that does not have the 'class' folder.
return;
}
Variant selectedVariant = androidModel.getSelectedVariant();
File mainClassesFolder = selectedVariant.getMainArtifact().getClassesFolder();
JavaArtifact testArtifact = androidModel.getUnitTestArtifactInSelectedVariant();
File testClassesFolder = testArtifact == null ? null : testArtifact.getClassesFolder();
ModifiableRootModel rootModel = ideModelsProvider.getModifiableRootModel(module);
myCompilerSettingsSetup.setOutputPaths(rootModel, mainClassesFolder, testClassesFolder);
}
use of com.android.builder.model.JavaArtifact in project android by JetBrains.
the class AndroidJunitPatcher method handleJavaResources.
/**
* Puts folders with merged java resources for the selected variant of every module on the classpath.
*
* <p>The problem we're solving here is that CompilerModuleExtension supports only one directory for "compiler output". When IJ compiles
* Java projects, it copies resources to the output classes dir. This is something our Gradle plugin doesn't do, so we need to add the
* resource directories to the classpath here.
*
* <p>We need to do this for every project dependency as well, since we're using classes and resources directories of these directly.
*
* @see <a href="http://b.android.com/172409">Bug 172409</a>
*/
private static void handleJavaResources(@NotNull Module module, @NotNull AndroidModuleModel androidModel, @NotNull PathsList classPath) {
CompilerManager compilerManager = CompilerManager.getInstance(module.getProject());
CompileScope scope = compilerManager.createModulesCompileScope(new Module[] { module }, true, true);
// The only test resources we want to use, are the ones from the module where the test is. They should go first, before main resources.
JavaArtifact testArtifact = androidModel.getUnitTestArtifactInSelectedVariant();
if (testArtifact != null) {
try {
classPath.add(testArtifact.getJavaResourcesFolder());
} catch (UnsupportedMethodException ignored) {
// Java resources were not present in older versions of the gradle plugin.
}
}
FileRootSearchScope excludeScope = null;
TestArtifactSearchScopes testScopes = TestArtifactSearchScopes.get(module);
if (testScopes != null) {
excludeScope = testScopes.getUnitTestExcludeScope();
}
for (Module affectedModule : scope.getAffectedModules()) {
AndroidFacet facet = AndroidFacet.getInstance(affectedModule);
if (facet != null) {
AndroidModuleModel affectedAndroidModel = AndroidModuleModel.get(facet);
if (affectedAndroidModel != null) {
try {
File resourceFolder = affectedAndroidModel.getMainArtifact().getJavaResourcesFolder();
if (excludeScope != null && excludeScope.accept(resourceFolder)) {
continue;
}
classPath.add(resourceFolder);
} catch (UnsupportedMethodException ignored) {
// Java resources were not present in older versions of the gradle plugin.
}
}
}
}
}
Aggregations