'~/.gradle/gradle.properties file not being read

There is a similar question here: Gradle properties not being read from ~/.gradle/gradle.properties but it does not solve my problem.

It seems to me that gradle is NOT reading my ~/.gradle/gradle.properties file.

I have a gradle.properties file in ~/.gradle, and it has properties needed to sign artifacts before uploading to maven central. It looks like this:

signing.keyId=12345678
signing.password=myPassword
signing.secretKeyRingFile=/home/me/.gnupg/secring.gpg

sonatypeUsername=me
sonatypePassword=myOtherPassword

When I try to build my project, it complains that there's no sonatypeUsername property, thus:

> Could not find property 'sonatypeUsername' on root project 'yourProject'.

Here's the relevant portion of my project's build.gradle:

uploadArchives {
    repositories {
        mavenDeployer {

            // lots of non-interesting things here

            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                authentication(userName: project.property("sonatypeUsername"), password: project.property("sonatypePassword"))
            }
        }
    }
}

When I try to build the project with debugging, here's what I see regarding properties:

$ ./gradlew --stacktrace --debug build

[INFO] [o.g.BuildLogger] Starting Build
[DEBUG] [o.g.BuildLogger] Gradle user home: /home/me
[DEBUG] [o.g.BuildLogger] Current dir: /home/me/dev/yourProject
[DEBUG] [o.g.BuildLogger] Settings file: null
[DEBUG] [o.g.BuildLogger] Build file: null
[DEBUG] [o.g.i.b.BuildSourceBuilder] Starting to build the build sources.
[DEBUG] [o.g.i.b.BuildSourceBuilder] Gradle source dir does not exist. We leave.
[DEBUG] [o.g.i.DefaultGradlePropertiesLoader] Found env project properties: []
[DEBUG] [o.g.i.DefaultGradlePropertiesLoader] Found system project properties: []
[DEBUG] [o.g.a.i.a.m.DefaultLocalMavenRepositoryLocator] No local repository in Settings file defined. Using default path: /home/me/.m2/repository
[DEBUG] [o.g.i.ScriptEvaluatingSettingsProcessor] Timing: Processing settings took: 0.286 secs
[INFO] [o.g.BuildLogger] Settings evaluated using empty settings script.
[DEBUG] [o.g.i.ProjectPropertySettingBuildLoader] Looking for project properties from: /home/me/dev/yourProject/gradle.properties
[DEBUG] [o.g.i.ProjectPropertySettingBuildLoader] project property file does not exists. We continue!
[INFO] [o.g.BuildLogger] Projects loaded. Root project using build file '/home/me/dev/yourProject/build.gradle'.


Solution 1:[1]

The problem was that I made an assumption that wasn't true. If you look at section 14.2 of the gradle documentation, it says:

You can place a gradle.properties file in the Gradle user home directory (defined by the “GRADLE_USER_HOME” environment variable, which if not set defaults to USER_HOME/.gradle) or in your project directory.

My incorrect assumption was that USER_HOME just defaulted to the standard linux HOME environment variable. This is not true.

As soon as I export USER_HOME=$HOME in my ~/.bashrc everything works

Solution 2:[2]

By default, without setting GRADLE_USER_HOME, it should work. I tested it in v3.5.

But make sure if your are running it as the right user. For ex, if you do your ./gradlew build using sudo, then gradle.properties in your home folder will not be picked up.

To make sure the default gradle user home, you can run gradle with the --debug option and look out for the below line,

[DEBUG] [org.gradle.internal.buildevents.BuildLogger] Gradle user home:

Solution 3:[3]

A quick and dirty solution is just to simply put the gradle.properties right next to your build.gradle, that will guarantee it will be read. I realize this doesn't solve a single centralized source of common properties, but at least it's something.

Solution 4:[4]

I am using Windows and I was experiencing the same issue. I noticed that my ~/.gradle/gradle.properties file had a byte-order-marker (BOM) at the beginning and I think it had windows line endings (\r\n). I recreated my ~/.gradle/gradle.properties file by deleting it and then making a new one with vim with unix line endings (\n) with the same content as before and then it started working. I don't know if it was the BOM, line-endings, or the combination of the two that was causing the issue, but I thought I should share this in case someone else has the same cause for this issue.

Solution 5:[5]

I did check Proxy Settings in Android Studio and i had No Proxy selected.

I did Invalidate Cache and Restart with no luck.

I added an exception in Firewall, still no luck.

In project gradle.properties i have no proxy configurations. Its empty, only comments.

In a previous version of Android Studio i had enabled Global Proxy Setting and it was a file stored in my user profile %userprofile%\.gradle\gradle.properties

Removing proxy lines in this file, solved my issue. I had to restart Android Studio to make it work.

:)

Solution 6:[6]

I myself stumbled upon this question when I was struggling with apk building via gradlew. I tried setting Gradle_user_home earlier for the mentioned error in question, but it was of no use, below solution worked for me.

I have below configuration for gradle-wrapper.properties :

*distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip*

gradlew itself at run time downloads the correct distribution of gradle from above config (distributionUrl). Nothing else is required.

Solution 7:[7]

I faced the same issue and solved it by using gradle binary, to build, instead of wrapper that comes with android project. You can install gradle from here: https://docs.gradle.org/current/userguide/installation.html#installation

Then used system-wide gradle to build, while on project root, instead of wrapper, like ex:

$ gradle installDebug

Solution 8:[8]

I made a different mistake. Why did i set up this field in "intelligent idea"? :) However when i wrote correct value in this field I get correct reading gradle.properties from .gradle
some setting in Idea

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Kannan Ramamoorthy
Solution 3 WillBD
Solution 4 Ricky Heywood
Solution 5 Community
Solution 6 Surabhi Pandey
Solution 7
Solution 8 Chehov_n