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:
./gradlew build
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:
> Could not resolve all files for configuration ':compileClasspath'.
> Could not resolve com.example:my-lib:1.0.0.
Required by:
project :
> Could not resolve com.example:my-lib:1.0.0.
> Could not get resource 'https://maven.pkg.github.com/myAccountName/myRepo/com/example/my-lib/1.0.0/my-lib-1.0.0.pom'.
> Could not GET 'https://maven.pkg.github.com/myAccountName/myRepo/com/example/my-lib/1.0.0/my-lib-1.0.0.pom'. Received status code 401 from server: Unauthorized
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:
> Could not resolve all files for configuration ':compileClasspath'.
> Could not find com.example:oops-wrong:1.0.0.
Searched in the following locations:
- file:/home/chriki/.m2/repository/com/example/oops-wrong/1.0.0/oops-wrong-1.0.0.pom
- https://maven.pkg.github.com/myAccountName/myRepo/com/example/oops-wrong/1.0.0/oops-wrong-1.0.0.pom
Required by:
project :
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:
> Could not resolve all files for configuration ':compileClasspath'.
> Could not find com.example:my-lib:1.0.0.
Searched in the following locations:
- file:/home/chriki/.m2/repository/com/example/my-lib/1.0.0/my-lib-1.0.0.pom
- https://maven.pkg.github.com/oops-wrong/com/example/my-lib/1.0.0/my-lib-1.0.0.pom
Required by:
project :
Some more findings:
- For the credentials, it seems that GitHub only looks at the token. The user name doesn’t seem to matter.
- The concrete name of the repository in the URL seems to be irrelevant as long as it’s not empty.
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:
wget --user myAccountName --password "$GITHUB_PACKAGES_TOKEN" \
https://maven.pkg.github.com/myAccountName/myRepo/com/example/my-lib/1.0.0/my-lib-1.0.0.pom