Initial version.
handte/gradle-android-eclipse/master There was a failure building this commit

This commit is contained in:
2019-11-01 16:58:35 +01:00
commit ac0d9a7fd5
18 changed files with 929 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/main/
+5
View File
@@ -0,0 +1,5 @@
description = "Gradle Android Eclipse Plugin"
dependencies {
compile gradleApi()
}
@@ -0,0 +1,148 @@
/**
* Copyright 2017 David Green
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.handte.gradle.android.eclipse;
import static java.text.MessageFormat.format;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.plugins.ide.eclipse.model.Classpath;
import org.gradle.plugins.ide.eclipse.model.ClasspathEntry;
import org.gradle.plugins.ide.eclipse.model.Library;
import org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory;
/**
* Adds the dependencies for aars by including references to the
* folders containing the exploded versions of the library.
*
* @author Marcus
*
*/
public class AddAndroidArchiveLibrariesAction implements Action<Classpath> {
/**
* The project.
*/
private final Project project;
/**
* Creates the action for the project.
*
* @param project The project.s
*/
public AddAndroidArchiveLibrariesAction(Project project) {
this.project = project;
}
@Override
public void execute(Classpath classpath) {
List<ClasspathEntry> entries = classpath.getEntries().stream().flatMap(entry -> mapToJars(entry))
.collect(Collectors.toList());
entries.forEach(entry -> {
GradleAndroidEclipsePlugin.log().info("Adding Android classpath entry: " + entry.toString());
});
classpath.setEntries(entries);
}
private Stream<ClasspathEntry> mapToJars(ClasspathEntry entry) {
if (entry instanceof Library) {
Library library = (Library) entry;
if (library.getPath().endsWith(".aar")) {
return explodeAarJarFiles(library);
}
}
return Stream.of(entry);
}
private Stream<ClasspathEntry> explodeAarJarFiles(Library aarLibrary) {
File aarFile = new File(aarLibrary.getPath());
String jarId = aarLibrary.getModuleVersion().toString().replaceAll(":", "-");
File targetFolder = new File(new File(new File(project.getProjectDir(), "build"), "exploded-aars"), jarId);
if (!targetFolder.exists()) {
if (!targetFolder.mkdirs()) {
throw new RuntimeException(format("Cannot create folder: {0}", targetFolder.getAbsolutePath()));
}
try (ZipFile zipFile = new ZipFile(aarFile)) {
zipFile.stream().forEach(f -> {
if (f.getName().endsWith(".jar")) {
String targetName = jarId + ".jar";
File targetFile = new File(targetFolder, targetName);
ensureParentFolderExists(targetFile);
int index = 1;
while (targetFile.exists()) {
targetFile = new File(targetFolder, format("{0}_{1}", ++index, targetName));
}
copy(zipFile, targetFile, f);
}
});
} catch (IOException e) {
throw new RuntimeException(
format("Cannot explode aar: {0}: {1}", e.getMessage(), aarFile.getAbsolutePath()), e);
}
}
List<File> files = listFilesTraversingFolders(targetFolder);
FileReferenceFactory fileReferenceFactory = new FileReferenceFactory();
return files.stream().filter(f -> f.getName().endsWith(".jar")).map(f -> {
Library library = new Library(fileReferenceFactory.fromFile(f));
library.setSourcePath(aarLibrary.getSourcePath());
return library;
});
}
private List<File> listFilesTraversingFolders(File folder) {
List<File> files = new ArrayList<>();
File[] children = folder.listFiles();
if (children != null) {
for (File child : children) {
if (child.isFile()) {
files.add(child);
} else if (child.isDirectory()) {
files.addAll(listFilesTraversingFolders(child));
}
}
}
return files;
}
private void ensureParentFolderExists(File targetFile) {
File parentFolder = targetFile.getParentFile();
if (!parentFolder.exists()) {
if (!parentFolder.mkdirs()) {
throw new RuntimeException(format("Cannot create folder: {0}", parentFolder.getAbsolutePath()));
}
}
}
private void copy(ZipFile zipFile, File targetFile, ZipEntry entry) {
try (InputStream inputStream = zipFile.getInputStream(entry)) {
Files.copy(inputStream, targetFile.toPath());
} catch (IOException e) {
throw new RuntimeException(
format("Cannot write entry to file: {0}: {1}", e.getMessage(), targetFile.getAbsolutePath()), e);
}
}
}
@@ -0,0 +1,86 @@
/**
* Copyright 2017 David Green
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.handte.gradle.android.eclipse;
import java.lang.reflect.InvocationTargetException;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.plugins.ide.eclipse.model.Classpath;
import org.gradle.plugins.ide.eclipse.model.ClasspathEntry;
import org.gradle.plugins.ide.eclipse.model.Variable;
import org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory;
/**
* Adds the references to the android sdk libraries based on
* the platform version.
*
* @author Marcus
*
*/
public class AddAndroidSdkLibrariesAction implements Action<Classpath> {
/**
* The project.
*/
private final Project project;
/**
* Creates a new action using the project.
*
* @param project Theproject.
*/
public AddAndroidSdkLibrariesAction(Project project) {
this.project = project;
}
@Override
public void execute(Classpath classpath) {
GradleAndroidEclipsePlugin.log().info("Adding Android SDK classpath entry");
classpath.getEntries().add(androidSdkEntry());
}
/**
* Creates a variable entry for the android sdk that points to the
* ANDROID_HOME environment variable and the sdk version defined in
* the build file.
*
* @return The classpath entry.
*/
private ClasspathEntry androidSdkEntry() {
FileReferenceFactory fileReferenceFactory = new FileReferenceFactory();
Object compileSdkVersion = compileSdkVersion();
Variable variable = new Variable(
fileReferenceFactory.fromPath("ANDROID_HOME/platforms/" + compileSdkVersion + "/android.jar"));
variable.setSourcePath(fileReferenceFactory.fromPath("ANDROID_HOME/sources/" + compileSdkVersion));
return variable;
}
/**
* Returns the compile sdk version of the project.
*
* @return The compile sdk version of the project.
*/
private Object compileSdkVersion() {
Object android = project.property("android");
try {
return android.getClass().getMethod("getCompileSdkVersion").invoke(android);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
| SecurityException e) {
throw new RuntimeException("Cannot get 'compileSdkVersion' property of 'android'.", e);
}
}
}
@@ -0,0 +1,76 @@
/**
* Copyright 2017 David Green
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.handte.gradle.android.eclipse;
import java.io.File;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.plugins.ide.eclipse.model.Classpath;
import org.gradle.plugins.ide.eclipse.model.SourceFolder;
/**
* Adds the source folders used by the android gradle pugin.
* In addition to the original version, this version will also
* ensure that the source folders exist. Since some folders
* such as *aidl* are only generated if there is an idl file,
* this will break the default import of eclipse.
*
* @author Marcus
*/
public class AddSourceFoldersAction implements Action<Classpath> {
/**
* The reference to the project to create source folders.
*/
private Project project;
/**
* Creates the action.
*
* @param project The project.
*/
public AddSourceFoldersAction(Project project) {
this.project = project;
}
@Override
public void execute(Classpath classpath) {
GradleAndroidEclipsePlugin.log().info("Adding Android source folders");
addToClasspathAndCreateFolder(classpath, "src/main/java");
addToClasspathAndCreateFolder(classpath, "build/generated/source/r/debug");
addToClasspathAndCreateFolder(classpath, "build/generated/source/buildConfig/debug");
addToClasspathAndCreateFolder(classpath, "build/generated/source/aidl/debug");
addToClasspathAndCreateFolder(classpath, "build/generated/source/aidl/debug");
addToClasspathAndCreateFolder(classpath, "build/generated/not_namespaced_r_class_sources/debug/r");
}
/**
* Adds a source folder entry to the class path and creates the
* folder on the disk, if it is not present already. The latter
* ensures that eclipse will not complain about missing source
* folders and invalid build classpaths.
*
* @param classpath The classpath.
* @param folder The folder.
*/
public void addToClasspathAndCreateFolder(Classpath classpath, String folder) {
classpath.getEntries().add(new SourceFolder(folder, "bin"));
File file = project.file(folder);
if (!file.exists()) file.mkdirs();
}
}
@@ -0,0 +1,80 @@
/**
* Copyright 2017 David Green
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.handte.gradle.android.eclipse;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import groovy.lang.MissingPropertyException;
/**
* The class that implements the plugin. When a project
* is evaluated, it attaches the actions to modify the
* eclipse .classpath file generation.
*
* @author Marcus
*/
public class GradleAndroidEclipsePlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
Logger logger = GradleAndroidEclipsePlugin.log();
project.afterEvaluate(new Action<Project>() {
@Override
public void execute(Project project) {
logger.info("Updating eclipse model with Android dependencies");
EclipseModel eclipseModel = eclipseModel(project);
eclipseModel.getClasspath().getFile().beforeMerged(new AddSourceFoldersAction(project));
eclipseModel.getClasspath().getFile().whenMerged(new AddAndroidArchiveLibrariesAction(project));
eclipseModel.getClasspath().getFile().whenMerged(new AddAndroidSdkLibrariesAction(project));
eclipseModel.getClasspath().getFile().whenMerged(new SanitizeClasspathEntriesAction());
project.getTasksByName("eclipseClasspath", false).forEach(t -> t.dependsOn("generateDebugSources"));
logger.info("Android dependencies done");
}
});
}
/**
* Returns the eclipse model from the project or throws an exception, if the
* eclipse plugin has not been applied to the project.
*
* @param project The project.
* @return The project's eclipse model.
*/
private EclipseModel eclipseModel(Project project) {
try {
return (EclipseModel) project.property("eclipse");
} catch (MissingPropertyException e) {
throw new RuntimeException(
"Cannot find 'eclipse' property.\nEnsure that the following is in your project: \n\napply plugin: 'eclipse'\n\n",
e);
}
}
/**
* Returns a reference to the logger.
*
* @return The logger.
*/
public static Logger log() {
return LoggerFactory.getLogger(GradleAndroidEclipsePlugin.class);
}
}
@@ -0,0 +1,43 @@
package org.handte.gradle.android.eclipse;
import java.util.ArrayList;
import org.gradle.api.Action;
import org.gradle.plugins.ide.eclipse.model.AbstractClasspathEntry;
import org.gradle.plugins.ide.eclipse.model.Classpath;
import org.gradle.plugins.ide.eclipse.model.ClasspathEntry;
import org.gradle.plugins.ide.eclipse.model.Container;
import org.gradle.plugins.ide.eclipse.model.SourceFolder;
/**
* This funky action fixes the final classpath entries by removing the
* reference to the JRE container and to a source folder that is setup
* automatically but is actually wrong, since it should not contain
* the configuration. The correct version is added in the add source
* folders action.
*
* @author Marcus
*/
public class SanitizeClasspathEntriesAction implements Action<Classpath> {
@Override
public void execute(Classpath classpath) {
for (ClasspathEntry entry: new ArrayList<>(classpath.getEntries())) {
if (entry instanceof AbstractClasspathEntry) {
AbstractClasspathEntry o = (AbstractClasspathEntry)entry;
// remove the jre container depenndency
if (o instanceof Container && o.getPath().startsWith("org.eclipse.jdt.launching.JRE_CONTAINER")) {
classpath.getEntries().remove(entry);
}
// remove the invalid reference to r classes
if (o instanceof SourceFolder && o.getPath().startsWith("build/generated/not_namespaced_r_class_sources/debug/processDebugResources/r")) {
classpath.getEntries().remove(entry);
}
}
}
}
}
@@ -0,0 +1 @@
implementation-class=org.handte.gradle.android.eclipse.GradleAndroidEclipsePlugin