Successfully reported this slideshow.

Creating Gradle Plugins

5

Share

Loading in …3
×
1 of 36
1 of 36

Creating Gradle Plugins

5

Share

Download to read offline

If you find yourself copying and pasting code from your application's build.gradle file every time you start a new project, then it's time you learn about creating Gradle plugins. In this presentation you will learn about what it takes to create a custom Gradle plugin for use in your Android applications.

It covers: Plugin Structure, Key Components, Testing, and Publishing

If you find yourself copying and pasting code from your application's build.gradle file every time you start a new project, then it's time you learn about creating Gradle plugins. In this presentation you will learn about what it takes to create a custom Gradle plugin for use in your Android applications.

It covers: Plugin Structure, Key Components, Testing, and Publishing

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Creating Gradle Plugins

  1. 1. Creating Gradle Plugins Annyce Davis - @brwngrldev
  2. 2. @brwngrldev Without Plugins
  3. 3. @brwngrldev With Plugins
  4. 4. Overview • Plugin Skeleton • Dependencies • Plugin.groovy • CustomTask.groovy • Publishing @brwngrldev
  5. 5. Plugin Skeleton @brwngrldev
  6. 6. Plugin Skeleton
  7. 7. Plugin Skeleton How Gradle finds the Plugin Implementation
  8. 8. Dependencies @brwngrldev apply plugin: ‘groovy' dependencies { compile gradleApi() compile localGroovy() }
  9. 9. Plugin.groovy Implement the Plugin interface and override apply method class CustomPlugin implements Plugin<Project> { @Override void apply(Project project) { } }
  10. 10. QualityChecksPlugin.groovy Implement the Plugin interface and override apply method
  11. 11. Apply Method @brwngrldev Creates the tasks for the plugin and performs any setup
  12. 12. Project Extension @brwngrldev Allows the user to customize the behavior of the plugin
  13. 13. Project Extension @brwngrldev Back in the application’s build.gradle file…
  14. 14. Creating tasks @brwngrldev Give it a name and a type
  15. 15. @brwngrldev
  16. 16. CustomTask.groovy @brwngrldev class CustomTask extends DefaultTask { }
  17. 17. CustomTask.groovy @brwngrldev class CustomTask extends DefaultTask { @TaskAction def defaultAction() { } }
  18. 18. CustomTask.groovy @brwngrldev class CustomTask extends DefaultTask { @TaskAction def defaultAction() { < do your cool stuff > } }
  19. 19. So far… • Plugin Skeleton • Dependencies • Plugin.groovy • CustomTask.groovy @brwngrldev
  20. 20. Questions?
  21. 21. Publishing @brwngrldev
  22. 22. Publishing buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "com.gradle.publish:plugin-publish-plugin:0.9.2" } } apply plugin: 'com.gradle.plugin-publish'
  23. 23. version = "0.1.3" group = "info.adavis"
  24. 24. version = "0.1.3" group = "info.adavis" pluginBundle { website = 'https://github.com/adavis/quality-checks' vcsUrl = 'https://github.com/adavis/quality-checks.git' description = 'Gradle Plugin for…’ tags = ['Checkstyle', 'FindBugs', 'PMD'] }
  25. 25. version = "0.1.3" group = "info.adavis" pluginBundle { website = 'https://github.com/adavis/quality-checks' vcsUrl = 'https://github.com/adavis/quality-checks.git' description = 'Gradle Plugin for…’ tags = ['Checkstyle', 'FindBugs', 'PMD'] plugins { qualityChecksPlugin { id = 'info.adavis.qualitychecks' displayName = 'Quality Checks Plugin' } } }
  26. 26. Publishing @brwngrldev Once your Plugin is published on the Gradle portal
  27. 27. We’re done… @brwngrldev WRONG!
  28. 28. We’re done… @brwngrldev TESTS
  29. 29. Testing @brwngrldev You can use JUnit and the ProjectBuilder for most testing
  30. 30. Testing @brwngrldev @Test void shouldBeAbleToCreateTask() { assertTrue(task instanceof WriteConfigFileTask) }
  31. 31. Testing @brwngrldev @Test void shouldBeAbleToCreateTask() { assertTrue(task instanceof WriteConfigFileTask) } @Test void pluginShouldBeApplied() { def project = ProjectBuilder.builder().build() project.apply(plugin: QualityChecksPlugin) assertNotNull(project.tasks.findByName(‘mytask’)) }
  32. 32. Bonus: README @brwngrldev
  33. 33. Bonus: README @brwngrldev
  34. 34. Summary • Helps avoid copy/paste horror • Simple project structure • Extending DefaultTask • Testing techniques • Easy to publish @brwngrldev
  35. 35. Next Steps ✓Sample: https://git.io/vzJ2w ✓Sample: https://git.io/vzJwS ✓Applaud 😉 @brwngrldev
  36. 36. Thanks! @brwngrldev +AnnyceDavis www.adavis.info @brwngrldev

×