I have set up a private project in Github that has some Maven packages. I can browse to them on the Github web site.
By following the directions here I have set up my build.gradle to declare the repository as follows:
repositories {
mavenLocal()
maven {
url = "https://maven.pkg.github.com/myAccountName/myRepo"
credentials {
username = System.getenv("GITHUB_USERNAME")
password = System.getenv("GITHUB_PACKAGES_TOKEN")
}
}
}
I've set up the system environment variables with the username and access token that I created in Github settings with read:packages
permission.
I declare the dependency the same way I was doing previously when I had it installed to mavenLocal()
. But I deleted it from the local Maven repo so I could test this.
The dependency cannot be resolved. But all the warning from Gradle says is:
Unable to resolve dependency for ':android@debug/compileClasspath': Could not resolve myGroup:myArtifact:1.0
There are no logged warnings about one of the Maven repos not being valid, but I think that is the likely problem, since this was working fine when the package was in the local Maven repo. Likely either I'm specifying it incorrectly or the credentials aren't working.
I also tried directly typing the string username and token instead of using environment variables. The token is brand new and not expired.
How can I determine what is going wrong with my connection to the Github Packages Maven repository? Is there a way I can get more useful logs? Or do you see something I'm doing wrong with how I'm authenticating?
From what I can tell from your question, your setup looks correct.
It seems that you’re only looking at the error provided by Android Studio. Gradle’s own error messages are usually more helpful, so I’d recommend to look at those. In other words, I’d recommend to try building the project from the command line, e.g. with:
Depending on what’s wrong, you’ll get different error messages. For example, if your password/token is wrong, then you’ll see something like this:
Note the
Received status code 401 from server: Unauthorized
at the end.If, however, your dependency declaration is wrong (any of group, module name or version), then you’ll get an error like this:
Of course, you’ll get the same error if you haven’t (correctly) uploaded/published the required package before. You can manually check if it exists at
https://maven.pkg.github.com/myAccountName/myRepo/packages
.There may be other error messages for other cases. I remember having seen a 403, for example. Unfortunately, the GitHub Packages registry doesn’t seem to provide enough details to Gradle so that it can provide other errors than the two above. Even if I use a wrong repository, I still get the second kind of error only:
Some more findings:
Getting back to your concrete issue: part of the Android Studio error message seems to still come from Gradle directly. It says “Could not resolve” rather than “Could not find”. So it’s likely a problem with the repository connection. If you haven’t used your token for anything else, then you can check at https://github.com/settings/tokens whether GitHub considers it to having been used, yet (e.g., “Last used within the last week”). If it says “Never used”, then you’ll know that Gradle has never succeeded to connect to GitHub, and you should double check the used token or try a new one. Alternatively, you could try your credentials with
wget
first: