The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.

Code Generation

One common thing to do in a Gradle plugin is to generate Java code. Code that writes other code based upon supplied inputs is referred to as a “code generator”.

At its core, Java code is just text. Developers generate text on the fly quite a bit, particularly in Web development. There are two main approaches to such text generation:

  1. Use templates, when the text is largely fixed but has some values that need to be replaced at runtime
  2. Use regular code to generate the text, perhaps using APIs that facilitate writing out text formats with as little coding overhead as possible

In this chapter, we will examine what it takes to generate Java code from a Gradle plugin. In particular, we will look at the second approach, where we will have Java code that generates other Java code, using a library that makes this a lot easier than writing a lot of append() calls to a StringBuilder.


Understanding this chapter requires that you have read the preceding chapter and all of its prerequisites.

What Drives the Custom Code?

The preview of this section was abducted by space aliens.

Java as Poetry

The preview of this section took that left turn at Albuquerque.

Writing a Code Generation Plugin

The preview of this section was stepped on by Godzilla.

Using the Generated Code

The preview of this section was stepped on by Godzilla.