diff --git a/README.md b/README.md index edd9528..5b87d7c 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ buildscript { } apply plugin: 'com.android.application' // mandatory for android apps -apply plugin: 'com.github.sgtsilvio.gradle.android-retrofix' // must be applied after com.android.application +apply plugin: 'com.github.sgtsilvio.gradle.android-retrofix' // should be applied after com.android.application ... android { diff --git a/src/main/java/com/github/sgtsilvio/gradle/android/retrofix/RetroFixPlugin.java b/src/main/java/com/github/sgtsilvio/gradle/android/retrofix/RetroFixPlugin.java index 230ec2a..2691a8f 100644 --- a/src/main/java/com/github/sgtsilvio/gradle/android/retrofix/RetroFixPlugin.java +++ b/src/main/java/com/github/sgtsilvio/gradle/android/retrofix/RetroFixPlugin.java @@ -1,34 +1,49 @@ package com.github.sgtsilvio.gradle.android.retrofix; -import com.android.build.gradle.AppExtension; import com.android.build.gradle.AppPlugin; + import org.gradle.api.GradleException; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Silvio Giebl */ public class RetroFixPlugin implements Plugin { + private static final @NotNull Logger logger = Logging.getLogger(RetroFixPlugin.class); + + private @Nullable AppPlugin appPlugin; + @Override public void apply(final @NotNull Project project) { - final boolean isAndroid = project.getPlugins().hasPlugin(AppPlugin.class); - if (!isAndroid) { - throw new GradleException("'com>.android.application' or 'com.android.library' plugin required."); - } - - final AppExtension android = (AppExtension) project.getExtensions().findByName("android"); - if (android == null) { - throw new GradleException("'com.android.application' or 'com.android.library' plugin required."); + if (project.getPlugins().hasPlugin(AppPlugin.class)) { + init(project.getPlugins().getPlugin(AppPlugin.class)); + } else { + project.getPlugins().whenPluginAdded(plugin -> { + if (plugin instanceof AppPlugin) { + logger.warn("The 'com.android.application' plugin should be applied before the RetroFix plugin."); + init((AppPlugin) plugin); + } + }); } - android.registerTransform(new RetroFixTransform(android)); project.afterEvaluate(project1 -> { - if (android.getDefaultConfig().getMinSdkVersion().getApiLevel() >= 24) { - throw new GradleException("the RetroFix plugin should not be used when the minSdkVersion >= 24"); + if (appPlugin == null) { + throw new GradleException("The RetroFix plugin requires the 'com.android.application' plugin."); + } + if (appPlugin.getExtension().getDefaultConfig().getMinSdkVersion().getApiLevel() >= 24) { + throw new GradleException("The RetroFix plugin should not be used when the minSdkVersion >= 24."); } }); } + + private void init(final @NotNull AppPlugin appPlugin) { + this.appPlugin = appPlugin; + appPlugin.getExtension().registerTransform(new RetroFixTransform(appPlugin.getExtension())); + } }