This guide implies that you are using simple security model that is based on uniqueness of URLs. If you are using HTTP Basic Auth then please read a corresponding guide

Help / Android + URL Unique private maven repository

Intro

You can use any maven compatible repository (including myMavenRepo) to publish / consume your public / private aar files. There are a few different ways to publish your Android library to private maven repository. This article is based on the following script but you are free to use any other technology like android-maven-publish plugin.

Security

By default our service uses very simple security model - we create unique read/write URL for each user. But this approach has its limitations - anyone who knows your URL can access your data. Because of that putting those URLs in to your build.gradle file directly will be insecure. To solve this problem you can store repository URL in ~/.gradle/gradle.properties as we do in examples below.

Get more info: official documentation, stackoverflow question

Consuming artifacts

To consume artifacts from your private repository you can use read or write URLs.

  1. 1. Add myMavenRepoReadUrl=${repositoryURL} to file ~/.gradle/gradle.properties, replace ${repositoryURL} with read or write URL of your repository.
  2. 2. Modify /path/to/project/build.gradle file to consume artifacts:
  3. repositories {
        maven {
            url myMavenRepoReadUrl
        }
    }
    

Publishing artifacts

To publish data to repository you can use only write URL

  1. 1. Add myMavenRepoWriteUrl=${writeURL} to file ~/.gradle/gradle.properties, replace ${writeURL} with write URL of your repository
  2. 2. Modify /path/to/project/build.gradle file to configure publish task:
  3. apply plugin: 'maven-publish'
    
    publishing {
        repositories {
            maven {
                url myMavenRepoWriteUrl
            }
        }
    
        publications {
            maven(MavenPublication) {
                groupId 'com.my-project'
                artifactId 'custom-artifact'
                version = '0.1-SNAPSHOT'
    
                artifact bundleReleaseAar
                artifact androidJavadocsJar
                artifact androidSourcesJar
    
                pom.withXml {
                    final dependenciesNode = asNode().appendNode('dependencies')
    
                    ext.addDependency = { Dependency dep, String scope ->
                        if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                            return // ignore invalid dependencies
    
                        final dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dep.group)
                        dependencyNode.appendNode('artifactId', dep.name)
                        dependencyNode.appendNode('version', dep.version)
                        dependencyNode.appendNode('scope', scope)
    
                        if (!dep.transitive) {
                            // If this dependency is transitive, we should force exclude all its dependencies them from the POM
                            final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                            exclusionNode.appendNode('groupId', '*')
                            exclusionNode.appendNode('artifactId', '*')
                        } else if (!dep.properties.excludeRules.empty) {
                            // Otherwise add specified exclude rules
                            final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                            dep.properties.excludeRules.each { ExcludeRule rule ->
                                exclusionNode.appendNode('groupId', rule.group ?: '*')
                                exclusionNode.appendNode('artifactId', rule.module ?: '*')
                            }
                        }
                    }
    
                    // List all "compile" dependencies (for old Gradle)
                    configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
                    // List all "api" dependencies (for new Gradle) as "compile" dependencies
                    configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
                    // List all "implementation" dependencies (for new Gradle) as "runtime" dependencies
                    configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
                }
            }
        }
    }
    
    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
        android.libraryVariants.all { variant ->
            if (variant.name == 'release') {
                owner.classpath += variant.javaCompile.classpath
            }
        }
        exclude '**/R.html', '**/R.*.html', '**/index.html'
    }
    
    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = 'javadoc'
        from androidJavadocs.destinationDir
    }
    
    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
    }
    
  4. 3. Replace com.my-project, custom-artifact, 0.1-SNAPSHOT with an appropriate data
  5. 4. Use gradle clean publish command to upload artifacts.

Get more info: stackoverflow question