DEV Community 👩‍💻👨‍💻

Cover image for How to access projects in multimodule Gradle project?
Aldo Wachyudi
Aldo Wachyudi

Posted on

How to access projects in multimodule Gradle project?

In a multimodule Gradle project, you're likely to have.. many modules. Each module can have other modules as dependencies.

dependencies {
    implementation(project(":feature:feature-payment"))
    implementation(project(":lib:crypto"))
    implementation(project(":android-lib:navigation"))  
}
Enter fullscreen mode Exit fullscreen mode

How would you remember the module name, if you have 100 modules?

An example of multimodule project called nowinandroid

Sure, you can type the module name manually, like in the above code snippet.
But is there a safer and faster way to access the project?
Yes, there is!

By using Gradle's TYPESAFE_PROJECT_ACCESSORS, we can access the projects in our project in a typesafe way.

If your project exists, the IDE will give autocomplete.

Autocomplete

If your project doesn't exist, the IDE will give you an error.

Setup

  1. Make sure you're using Gradle 7.0 or higher
  2. In your root project build.gradle file, add the following line of code.
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
Enter fullscreen mode Exit fullscreen mode

Result

Open your build.gradle or build.gradle.kts, the dependencies above will look like this.

dependencies {
    implementation(projects.feature.featurePayment)
    implementation(projects.lib.crypto)
    implementation(projects.androidLib.navigation)  
}
Enter fullscreen mode Exit fullscreen mode

Notice that Gradle converts the module with kebab-case or snake_case to camelCase. From feature-payment to featurePayment.

Summary

By using typesafe project accessors you can access modules in your project in a typesafe manner. Currently, Gradle still marks this feature as experimental. But you can use it in your project by enabling the TYPESAFE_PROJECT_ACCESSORS in the build.gradle file.

References:

Top comments (0)

Stop sifting through your feed.

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.