preface

Gradle is an automated build tool that automatically downloads your configured dependencies from remote services such as

Implementation ‘com. Squareup. Okhttp3: okhttp: 3.10.0’

A simple sentence can also use OKhttp3.10.0 in the project, isn’t it convenient? But how exactly?

The principle of

First gradle checks to see if it has been downloaded locally.

If you have downloaded it, you can use it directly. If you have not downloaded it, you can download it. The default source of the download project is:

   repositories {
        google()
        jcenter()
    }
Copy the code

The Google ()

    public MavenArtifactRepository google() {
        return addRepository(repositoryFactory.createGoogleRepository(), GOOGLE_REPO_NAME);
    }
    
    public MavenArtifactRepository createGoogleRepository() {
        MavenArtifactRepository mavenRepository = createMavenRepository(new NamedMavenRepositoryDescriber(RepositoryHandler.GOOGLE_URL));
        mavenRepository.setUrl(RepositoryHandler.GOOGLE_URL);
        return mavenRepository;
    }
    
    String GOOGLE_URL = "https://dl.google.com/dl/android/maven2/"
Copy the code

jcenter()

    public MavenArtifactRepository jcenter() {
        return addRepository(repositoryFactory.createJCenterRepository(), DEFAULT_BINTRAY_JCENTER_REPO_NAME);
    }
    public MavenArtifactRepository createJCenterRepository() {
        MavenArtifactRepository mavenRepository = createMavenRepository(new NamedMavenRepositoryDescriber(DefaultRepositoryHandler.BINTRAY_JCENTER_URL));
        mavenRepository.setUrl(DefaultRepositoryHandler.BINTRAY_JCENTER_URL);
        return mavenRepository;
    }
	String BINTRAY_JCENTER_URL = "https://jcenter.bintray.com/"
Copy the code

Warehouse:

Dl.google.com/dl/android/… jcenter.bintray.com/

Finally in the above two warehouses to download resources, obviously because of the reason of the domestic wall, the speed is not ideal or simply access.

Introduction of domestic warehouses

In order to solve the problem, China has built a lot of synchronous warehouses for people to use, such as Ali Cloud

HTTP: / / https://maven.aliyun.com/repository/google / / the company warehouse Google https://maven.aliyun.com/repository/jcenter offers / / JFrog companies provide Jcenter HTTP: / / https://maven.aliyun.com/repository/central / / Sonatype central provided by the company HTTP: / / https://maven.aliyun.com/nexus/content/repositories/central / / Sonatype central provided by the company HTTP: / / https://maven.aliyun.com/repository/google / / Google Google provided by warehouse HTTP: / / https://maven.aliyun.com/nexus/content/repositories/google / / the company provide warehouse Google HTTP: / / https://jitpack.io / / JitPack warehouse HTTP: / / https://maven.aliyun.com/nexus/content/groups/public / / jcenter and mavenCentral polymerization warehouse https://maven.aliyun.com/repository/public https://maven.aliyun.com/repository/gradle-plugin / / jcenter and mavenCentral aggregation warehouse / / Gradle warehouse/https://maven.aliyun.com/nexus/content/repositories/gradle-plugin/Gradle plugin repositoryCopy the code

We can change the dependency repository to the above repository and basically solve all open source library download problems, such as adding the above repository under the root directory build.gradle

Buildscript {repositories {/ / to multiple warehouse address maven (' https://maven.aliyun.com/repository/google ')} {url uri Google jcenter () () Allprojects}} {repositories {/ / to multiple warehouse address maven (' https://maven.aliyun.com/repository/google ')} {url uri Google () jcenter() } }Copy the code

Introducing private libraries

Although it can be solved in this way, why do we need to build our own? At present, my reasons are as follows: 1, Shared within the company development module, convenient internal dependence in distributed network, and to avoid internal code 2, due to reasons known to all, to the network server to download abroad rely on slowly, intranets to the related file cache on the one hand, accelerated the export bandwidth depend on the speed of installation and to reduce the pressure more than 3, don’t need to join the warehouse address, Join a self-built private agent repository

I chose to build the Maven repository using ArtiFactory. As for why I choose Artifacory, it is because ArtiFactory supports Gradle and Android more smoothly, and the interface looks better and more modern. Or you can go with the old Nexus

Install Artifactory

How to install ArtiFactory, here is a complete tutorial, I Windows, MAC system have been verified, follow the steps to go, not to do too much explanation, also with the Pro version oh.

  • Click me to download (access code: 9sho)
  • Click me to enter the installation tutorial

installedartifactoryThe gradle file only needs to import the virtual repository. You don’t need to add more configurations. After you have configured the virtual repository, you can build againartifactoryYou keep adding and adding until you stop, and your project builds. The next time or someone else builds it, it changes very quickly

Sync it to your buddy and let him lead you into your private warehouse. In the local area network environment is not the first time to load, naturally very fast, for the new partner or join a new project is a very good help.

Publish library files to the Artifactory private repository

After solving problems 2 and 3, we are naturally faced with how to publish libraries to the repository. The solution is simple, and an example is provided here. For details, see -ArtiFactoryExample

For example, build. Gradle in The Module-library project of AndroidStudio adds publishing and artifactoryPublish nodes

Library 'maven-publish' // Apply plugin: 'com.android.library' apply plugin: 'maven-publish' 'com.jfrog.artifactory' // must be added to this plugin android {...... } publishing {publications {aar_publish(MavenPublication) { Need to use the groupId = under artifactoryPublish rootProject. Ext maven. GroupId/artifactId/company = rootProject. Ext maven. ArtifactId / / project version = rootProject. Ext. Maven. Version version / / / / aar file location / / module after packaged under the path to the module module build/outputs/aar, generated aar name for: Module name -release.aar artifact("$buildDir/outputs/aar/" + getReleaseName())}} ContextUrl = rootProject. Ext. Maven. Local publications (' aar_publish ') / / attention aar_publish used here is defined above ClientConfig. Publisher. RepoKey = rootProject. Ext. Maven. RepoKey / / uploaded to warehouse address clientConfig publisher. The username = RootProject. Ext. Maven. Username/login/artifactory username clientConfig. Publisher. Password = rootProject. Ext maven. Password //artifactory login password}Copy the code

Note that the current project needs to be cleaned before each release, otherwise it will not be uploaded.

The procedure is executionclean,assembleAfter generating the AAR file, clickpublish - > artifactoryPublish, publish to a private repository, and check whether the upload is successful based on the repoKey you publish

Import the private libraries you publish

$groupId:$artifactId:$version. For example, in your app project, reference the AAR in the app directory build.gradle

plugins { id 'com.android.application' } android { .... } dependencies {implementation "com. Squareup. Okhttp3: okhttp: 3.10.0". / / the groupId = com squareup okhttp3, ArtifactId = okhttp, version = 3.10.0"}Copy the code

So, problem 1 is solved. Let your friends use it quickly.