The foreword 0.

We will use Eclipse and IDEA to create Gradle projects and add Grale support to existing projects. For more on the advanced uses of Gradle, see the following blog post.

Before using gradle, you are advised to download the latest gradle components and save them in a local directory so that you can quickly create new projects.

1. idea

Gradle was first introduced to Android development. AndroidStudio is a custom Android application development IDE based on IntelliJ IDEA, using Gradle to build projects. I’ve been building Android projects for fools, not realizing that Gradle is a powerful tool for building projects. In the previous article, building Android projects from scratch based on Google packaged components to build Android projects, please taste the original taste of gradle.

1.1 Creating a Gradle project

Select create gradle project.

Specify gradle configuration.

Configure the version information.

Specify the project directory.

Final Gradle project structure.

Is it similar to the Android project at first glance?

1.2 Add Gradle support

To add gradle support to an existing IDEA project, add settings.gradle and build.gradle files to your project.

The following is a common Java project.

After adding the above two files and adding the configuration statement, open IDEA again, and the following prompt appears in the lower right corner:

Click “Import Gradle Projet” to display the following configuration page. It is recommended to specify Gradle version as marked.

Settings. gradle file edit content:

rootProject.name = 'demo'
Copy the code

Build. Gradle file

apply plugin: 'java'
Copy the code

It is important to note that normal Java engineering structures are different from Gradle engineering structures. For example, the former source directory hierarchy is SRC /package/class. The latter source directory hierarchy is SRC/main/Java/package/class.

Therefore, you need to configure the following code in the build.gradle file:

sourceSets {
    main{
        java{
            srcDir 'src'}}}Copy the code

After configuration, click the refresh button on the left to rebuild the project.

Or click the Settings button on the right and select Automatic import.

Notice how the project catalog changes at this point.

2. eclipse

Using Ecilpse requires the gradle plugin to be installed separately. Some versions do not.

Open the Eclipse Marketplace.

Search gradle for the latest version 3.0.

Eclipse needs to be restarted after installation.

2.1 Creating a Gradle project

To create a new Project, select Gradle Project.

The welcome page in the middle can be selected not to show next time.

Specify the project name and storage path.

Determine gradle parameters.

Confirm engineering parameters are correct.

The final project catalog is as follows.

2.2 Adding Gradle support

After testing, this solution is not feasible to add Gradle support to existing Eclipse projects. If anyone has tried and succeeded, please leave a message at [email protected].

It is recommended that eclipse projects add Gradle support and open it with IDEA. The specific method is the same as that of adding Gradle to IDEA project.

3 Multi-project association

Eclipse and IDEA have a slightly different concept of engineering architecture.

  1. Eclipse opens up a workspace, and each workspace can contain multiple projects. Each project can be run separately or in association.

  2. Idea opens a project, and each project can contain multiple modules. The association between modules is equivalent to the multi-project association in Eclipse. A project can have multiple main blocks, equivalent to multiple independent projects in Eclipse.

Gradle project has a settings. Gradle file and 1 + N build. Gradle files. Settings. gradle declares projects and modules. Each project and module contains a build.gradle file, which is used to configure project parameters such as dependencies and compile scripts.

3.1 the idea of engineering

Create a lib Module based on the previous demo.

Settings. gradle automatically generates configuration containing lib.

rootProject.name = 'demo'
include 'lib'
Copy the code

Build. gradle has two files, one in the lib directory for lib configuration and one in the project directory for main Module and project configuration. In the build.gradle project directory, add the association to lib.

dependencies{
    implementation project(':lib')}Copy the code

If you extract the source code from the main Module and place it in a separate folder.

At this point, the build.gradle file for the project is separated from the build.gradle file for the main Module. You need to add a lib dependency to the build.gradle file in your app.

3.2 the eclipse project

In Eclipse, each project is independent, so you can’t add dependent modules as you can in IDEA. However, you can rely on other projects as library projects by referring to external directories.

In the figure, demo and test are separate projects, so they both have settings.gradle files and build.gradle files. To make the demo depend on Test, add the following configuration to settings.gradle file in the demo.

include 'Gradle-test'
project(':Gradle-test').projectDir = new File(settingsDir, ".. /Gradle-test")
Copy the code

Add dependencies to the build.gradle file in the demo.

dependencies {
    implementation project(':Gradle-test')}Copy the code

Think it works? I’ll give you a bonus.To play to admire