Gradle Interview Questions
Published on

Gradle Interview Questions

Authors

Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration.

Table of Contents

What is Gradle Wrapper?

The Gradle Wrapper is the preferred way of starting a Gradle build. The wrapper is a batch script on Windows, and a shell script for other operating systems. When we start a Gradle build via the wrapper, Gradle will be automatically downloaded and used to run the build.

Why use Gradle instead of Maven or Ant?

Ant gave us total flexibility, and Maven gives better dependency management , but there isn’t great support for multi-project builds.

You end up doing a lot of coding to support multi-project builds. Also having some build-by-convention is nice and makes build scripts more concise. With Maven, it takes build by convention too far, and customizing your build process becomes a hack. Also, Maven promotes every project publishing an artifact. Sometimes you have a project split up into subprojects but you want all of the subprojects to be built and versioned together. Not really something Maven is designed for.

With Gradle you can have the flexibility of Ant and build by convention of Maven. For example, it is trivial to extend the conventional build lifecycle with your own task. And you aren’t forced to use a convention if you don’t want to. Groovy is much nicer to code than XML. In Gradle, you can define dependencies between projects on the local file system without the need to publish artifacts for each to a repository.

How do I force Gradle to download dependencies always?

you may refresh dependencies in your cache using the command line option –refresh-dependencies. Also deleting the cached files under ~/.gradle/caches would get the next Gradle build to download them again.

What is Gradle Daemon?

The Daemon is a long-lived process that helps with the faster build process, by avoiding the cost of JVM startup for every build and also caches information about project structure, files, tasks, and more in memory.

Difference between settings.gradle & gradle.properties.

The settings.gradle is a Groovy script that defines build related settings and not project related. It is evaluated against a Settings object and with this Settings object, you can add sub projects to your build, modify the parameter from the command line (StartParameter) and access the Gradle object to register lifecycle handlers.

The gradle.properties file is a simple Java Properties file. It’s a simple key-value store that only allows string values.

What are the advantages of Gradle?

  • Declarative builds and build-by-convention
  • Language for dependency based programming
  • Structure your build
  • Deep API
  • Gradle scales
  • Multi-project builds
  • Gradle provides partial builds
  • Many ways to manage your dependencies
  • Gradle is the first build integration tool

Explain Groovy?

Gradle uses a programming language that is written in a script form, and the name of that script is Groovy. The features of this language are:

  • It interoperates with Java easily as Groovy operates on JVM (Java Virtual Machine).
  • To write a build script, you don’t have to learn Groovy.
  • It is simple to write and read a Groovy due to its smaller codes than Java.
  • It is a dynamic and flexible language that works somewhat similarly to Java. It is also compatible with the byte code of JVM.

What is the file name built by Gradle?

Build.gradle is the name of the file name that Gradle builds.

Different types of plugins in Gradle?

Binary plugins – Binary plugins can reside within a build script, within the project hierarchy or externally in a plugin jar

Script plugins – . Script plugins are additional build scripts that further configure the build and usually implement a declarative approach to manipulating the build.

A plugin often starts out as a script plugin (because they are easy to write) and then, as the code becomes more valuable, it’s migrated to a binary plugin that can be easily tested and shared between multiple projects or organizations.

Different build phases in Gradle?

Initialization – During the initialization phase, Gradle determines which projects are going to take part in the build

Configuration – During this phase the project objects are configured. The build scripts of all projects which are part of the build are executed.

Execution – Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed.

What Are The Core Components Of Gradle Build Script?

Project and task are the core components. Groovy organizes projects as a list of tasks.

To view the list of available projects, use the command gradle projects, for the tasks list the command is gradle tasks.

How Do You Find Gradle Project Dependencies?

Use the Gradle command gradle dependencies that lists the dependencies of the selected project. It includes both direct and transitive dependencies.

How Do I Configure The Gradle Daemon To Speed Up Builds?

The Gradle daemon helps greatly in eliminating startup overhead. This feature may potentially be enabled by default in the future, but in the meantime you need to instruct Gradle to launch the daemon process. This can be achieved by passing the –daemon flag to gradle at the command line, by exporting a GRADLE_OPTS environment variable that includes -Dorg.gradle.daemon=true, or by adding org.gradle.daemon=true to the gradle.properties file in your gradle user home directory (e.g., ~/.gradle/gradle.properties).

If you are building against JDK 9 and using the Gradle daemon, you may encounter an Unrecognized VM option error which halts the build. To avoid this error, you can add org.gradle.jvmargs=-XX:MaxMetaspaceSize=1024m -Xmx1024m to the gradle.properties file in your gradle user home directory. See also GRADLE-3256 for details.

In what language should I develop my plugins for Gradle?

You can develop your plugins in any JVM language, but as part of this effort, we are working on making Kotlin the language of choice for developing Gradle plugins.

At broad level what are the two real objects used by Gradle?

Project Object : Script describes about one or multiple projects while in the execution, this script configures the Project Object.

Script Object : Gradle takes script code into classes, which implements Script Interface and then executes.

What are the gradle build configuration files ?

  • build.gradle
  • gradle.properties
  • settings.gradle.

How do I find the Gradle version?

Open a console (or a Windows command prompt) and run gradle -v to run gradle and display the version.

Difference between api and implementation in Gradle?

The API configuration should be used to declare dependencies that are exported by the library API. In contrast, the implementation configuration should be used to declare dependencies that are internal to the component.

dependencies {
    api 'org.apache.httpcomponents:httpclient:4.5.7'
    implementation 'org.apache.commons:commons-lang3:3.5'
}

Dependencies appearing in the “api” configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers. Dependencies found in the “implementation” configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumer’s compile classpath.

What Are Limitations Of Groovy?

Groovy has some limitations. They are described below:

  • It can be slower than the other object-oriented programming languages.
  • It might need more memory than that required by other languages.
  • The start-up time of groovy requires improvement. It is not that frequent.
  • For using groovy, you need to have enough knowledge of Java. Knowledge of Java is important because half of groovy is based on Java.
  • It might take you some time to get used to the usual syntax and default typing.
  • It consists of thin documentation.

Create a Gradle task and explain how to execute it.

The Gradle build file defines the project and its tasks. The below list a simple task to print “Hello world!”.

task hello {
    doLast {
        println 'Hello World!'
    }
}

Run gradle hello on the command line in the directory of the build file to execute hello task. If the Gradle output should be suppressed, use the -q (quiet) parameter.

How to create a gradle project in current directory?

The command gradle init will create the gradle project with the following files:

  • settings.gradle file
  • gradle wrapper files
  • build.gradle file

What is the layout for a repository in Gradle?

Specifies how the items of the repository are organized.

repositories {
    ivy {
        url "http://repo.codingjump.com/repo"
        patternLayout {
            artifact "[module]/[revision]/[type]/[artifact].[ext]"
        }
    }
}

What is a repository in Gradle?

The location for storing modules is called a repository. By specifying the repositories for a project, Gradle can find and retrieve modules. Repositories can be in different forms, such as a local directory or a remote repository.

What are the supported repositories in Gradle?

  1. Flat directory repository
  2. Maven repositories
  3. Ivy repositories
  4. Local repositories
  5. A wide variety of remote protocols such as HTTPS, SFTP, AWS S3 and Google Cloud Storage

What is the difference between jar and aar file format?

If you are just exporting a simple class library then use the *.jar file; if you are exporting a UI library, including some of your own control layout files and fonts and other resource files then You can only use *.aar files.

What is the latest version of Gradle?

Latest version of Gradle is 7.0, nowadays Gradle launches a new major version every year.