Unable to publish android library to packages

The example does not work:

publications {
        gpr(MavenPublication) {
            from(components.java)
        }
    }

Error: Could not get unknown property ‘java’ for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer.

If I comment out “from(components.java)” I am getting
Error: Invalid publication ‘github’: groupId [Android Studio Project Name] because the name contains spaces. Why is groupId not documented and missing in the example??

If I use groupId = “gprRepoOwner”

The error is 404 due to wrong URL:
'https://maven.pkg.github.com/[gprRepoOwner]/[gprRepoOwner]/unspecified

How to configure build.gradle to get the correct URL for the repository?