How to read extra properties extensions defined within a submodule from the submodule's gradle file


(nmbiyũ) #1

I have two Android projects. Code that can be shared between these two projects has been put in a separate project that is included in each Android project as a sub module.

I recently tried to define my dependencies using extra properties extensions and had success doing this in the two root projects. Here is an overview of what I did.

  1. Defined an ext object in a dependencies.gradle file at the root of the project.
  2. Applied this file from within the root build.gradle
    apply from: 'dependencies.gradle'
  3. Used the properties I defined within the root project’s only module, e.g
def appDependencies = rootProject.ext.appDependencies

implementation appDependencies.androidSupportAppCompat
// more dependencies

However, I was unable to replicate the same in the sub module project. The sub module project also has its dependencies and should thus define its own dependencies.gradle file. I applied the dependencies.gradle file and tried to get the properties defined therein using rootProject.ext.appDependencies.

rootProject.ext.appDependencies reads the extra properties extension from the root project even when called from within the sub module’s build.gradle file. I thus have errors like these.

Cannot get property 'commonDependencies' on extra properties extension as it does not exist

This behavior seems logical to me. How do I read the extra properties extensions defined in the sub module from the sub module’s gradle file?

(nmbiyũ) #2

I figured it out. Calling project.ext from the sub module’s build.gradle file got me the extra properties extensions defined in the sub module’s dependencies.gradle file.