1

I have an Android library that I want to publish it as dependecy to be used privately by different collegues in our company.

I amm not sure but Github Packages will make it public and any one can use it.

Is there any way to do that using Gitlab or any tool ?

Oussèma Hassine
  • 741
  • 1
  • 7
  • 18

2 Answers2

2

It is possible to publish to the Gitlab package registry as well. It got even simpler with the Android Gradle plugin 7.1 since it builds the POM, sources, and documentation automatically for you. In the past, all of that had to be written manually. Here is an example written in Gradle Kotlin DSL:

android {
    publishing {
        singleVariant("debug") { // or use multipleVariants
            // if you don't prefer the options below, you can remove them
            withSourcesJar()
            withJavadocJar()
        }
    }
}

val gitLabPersonalAccessToken: String by project

publishing {
    val mavenArtifactId = project.name

    repositories {
        maven {
            name = "gitlab-maven"
            url = uri("https://gitlab.bla.io/api/v4/projects/666/packages/maven")
            credentials(HttpHeaderCredentials::class) {
                name = "Private-Token"
                value = gitLabPersonalAccessToken
            }
            authentication {
                create<HttpHeaderAuthentication>("header")
            }
        }
    }

    publications {
        register("debug", MavenPublication::class) {
            groupId = "com.foo.bar"
            artifactId = mavenArtifactId
            version = "1.0"
            afterEvaluate {
                from(components["debug"])
            }
        }
    }
}

The authentication part is done by using the Gitlab personal access token. In Gitlab in the upper right corner click on your account -> Preferences -> Access Tokens:

  • In the Token Name enter gitLabPersonalAccessToken and give it api permission
  • Click on Create personal access token
  • Copy-paste it to the global Gradle properties (Mac: /Users/username/.gradle/gradle.properties) like that: gitLabPersonalAccessToken=copiedPersonalAccessToken

Once that is done, the personal access token will be assigned to the val gitLabPersonalAccessToken: String by project defined in the Gradle example above. Click here for more details about the delegated properties.

You can also find more info in the following links:

flyingAssistant
  • 872
  • 7
  • 19
1

I have done this using github packages, i was not able to access it until i provide a token, so i think this makes it private.

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/USERNAME/REPOSITORY")
            credentials {
                username = "USERNAME"
                password = "TOKEN"
            }
        }
    }


    publications {
        aar(MavenPublication) {
            groupId 'com' 
            artifactId 'test'
            version '1.0.0'
            artifact("set your aar file location")
        }
    }
}

USERNAME = your user name for github account

REPOSITORY = name of you repo

TOKEN = this token is generated, you can do this from your account

aar file= this file appears when you build your project under the library build files.

then you can run publish either by a command line $gradle publish

or do it from the android studio interface as explained in this image.

------ Adding the dependency to a project

into you project gradle add this

 maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/USERNAME/REPOSITORY")
            credentials {
                /** Create github.properties in root project folder file with
                 ** gpr.usr=GITHUB_USER_ID & gpr.key=PERSONAL_ACCESS_TOKEN
                 ** Or set env variable GPR_USER & GPR_API_KEY if not adding a properties file**/

                username = "USERNAME"
                password = "TOEKN"
            }
        }

into you module gradle add this

implementation com:test:1.0.0

هيثم
  • 791
  • 8
  • 11