Adding Password Protected Maven Repository URL in Gradle in Android Studio

Image result for android gradle

This article is part of Today I Learned series and was originally posted at my TIL Github Repository and my website at wajahatkarim.com

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 file, I was getting this exception on syncing the project.

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 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 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.

Solution

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.

Then, you need to add your private maven URL in root file like this:

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

Wajahat Karim is a graduate from NUST, Islamabad, an experienced mobile developer, an active open source contributor, and co-author of two books Learning Android Intents and Mastering Android Game Development with Unity. In his spare time, he likes to spend time with his family, do experiments on coding, loves to write about lots of things (mostly on blog and medium) and is passionate contributor to open source. In June 2018, one of his library became #1 on Github Trending. His libraries have about 2000 stars on Github and are being used in various apps by the developers all around the globe. Follow him on Twitter and Medium to get more updates about his work in Writing, Android and Open Source.

Also, if you have any questions you’d like him to answer, contact him through his website at wajahatkarim.com with DEAR WAJAHAT in the subject line.

🔥 Google Dev Expert (GDE) in Android . 📱 Android Dev . 💻FOSS Contributor . 📝Writer . 🎤 Speaker . 👶 Developer Dad — 🌐Subscribe at https://wajahatkarim.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store