-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 27 additions & 12 deletions
39
src/main/java/com/github/sgtsilvio/gradle/android/retrofix/RetroFixPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Project> { | ||
|
||
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())); | ||
} | ||
} |