Adding Password Protected Maven Repository URL in Gradle

In my current project at work, I had to setup a private maven repository hosted at Artifactory. I was assigned login credentials to access the libraries (or artifacts) from the repository. But, when I was fetching it from Android Studio project through build.gradle file, I was getting this exception on syncing the project.

org.gradle.internal.resource.transport.http.HttpRequestException: Could not GET 'MY_MAVEN_ARTIFACT_URL_GOES_HERE'.
Caused by: Read timed out
at Method)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(
at org.apache.http.protocol.HttpRequestExecutor.execute(
at org.apache.http.impl.execchain.MainClientExec.execute(
at org.apache.http.impl.execchain.ProtocolExec.execute(
at org.apache.http.impl.execchain.RetryExec.execute(
at org.apache.http.impl.execchain.RedirectExec.execute(
at org.apache.http.impl.client.InternalHttpClient.doExecute(
at org.apache.http.impl.client.CloseableHttpClient.execute(

As the exception says, I was confused and I thought that my private maven URL is not valid etc. And I tried following methods to solve this issue: * Invalidating cache and restart Android studio (several times) but didn’t worked for me. * Changing proxy settings from SOCKS to HTTPS to HTTP but didn’t worked for me. * White listing Android Studio in Firewall for Windows 10. Again, didn’t worked for me. * Deleting .gradle folder etc. * Tried to get username and password for maven repo from system variables, but didn’t work. * Tried to get username and password for maven repo from file but didn’t work. * Tried to put username and password for maven repo directly in the build.gradle file (which is highly not recommended) but still didn’t work.

After 2-3 hours of troubleshooting and debugging, I found out that problem is not with the maven URL. The URL works great in browsers, the problem was Android Studio was not properly authenticating maven repo in right way as I missed a thing to put.


So, here’s the solution which worked for me.

First, you need to add your login credentials in file. Please note that this is not recommended to share your credentials this way. I am looking for more optimal and secure solution. If you have any way, do let me know.

mavenUser = MY_USER_NAME
mavenPassword = MY_PASSWORD

THen, you need to add your private maven URL in root build.gradle file like this:

allprojects {
    repositories {
        // ...
        maven {
            credentials  {
                username = "${mavenUser}"
                password = "${mavenPassword}"
            url "http://my.private.artifactory/url/goes/here"
            authentication {                            // I was missing this authentication section and that's why read timeout

My mistake was I was missing authentication block and that’s why Android studio couldn’t get logged in private URL and after multiple failed retries, it was getting read timeout response. Adding this authentication block worked for me.

authentication {                            

If you liked this article, you can read my new articles below:

Wajahat Karim is Pakistan’s first Google Developer Expert in Android. As an experience Android developer, he deeply cares about it and keeps writing and speaking about it. He has written two worldwide 300+ pages books on Android development with more than 100 articles around the internet either on his website or his Medium publications. He is also a passionate contributor in open source and has created many Android libraries used by thousands of developers in their apps worldwide. As active public speaker, he spends lots of time giving talks in conferences and motivating people on Android development. You can find Wajahat most active on Twitter @WajahatKarim where he regularly shares all the good stuff about Android and community building.

Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan