One, foreword

Whether at the front end, back end or APP end, with the iteration of functional requirements and the expansion and flow of staff team, a project becomes more and more bloated, the performance of IDE also decreases geometrically, the code coupling is serious, the maintainability becomes worse, and the compilation is more time-consuming. At this time, we will think of code abstraction and separation, modularization, componentization, microservitization and other directions of evolution; Once unassembled, they wanted to make it available to users as Library rather than source code, so package management tools like Maven/JCetner were born.

Two, package management warehouse

Android projects rely on third-party libraries in three ways:

  • Module dependency (implemention project(‘: XXXX ‘))
  • Local LIBS dependencies: placed under “./Module/libs/”;
  • Remote repository dependencies: based on third-party central repositories;

Android has three central repositories available:

  • Maven
  • JCenter
  • Google

These are public network warehouses that can be used globally, but for the consideration of some companies, they will also set up private servers inside the enterprise, as shown below:

Gradle universal publishing scripts based on Maven

3.1 Basic knowledge of Maven

Let’s start by looking at the contents of a package’s POM file

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" XMLNS = "http://maven.apache.org/POM/4.0.0" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Chris. Aop < / groupId > < artifactId > aop - asm - gradle - plugin < / artifactId > <version>1.0.0</version> <dependencies> <groupId>org.ow2.asm</groupId> <artifactId> <version>9.0</version> <scope> Runtime </scope> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId> ASm-Commons </artifactId> <version>9.0</version> <scope> Runtime </scope> </dependency> <dependency> < the groupId > com. Android. Tools. Build < / groupId > < artifactId > gradle < / artifactId > < version > 3.5.0 < / version > <scope>runtime</scope> </dependency> </dependencies> <name>com.chris.aop:aop-asm-gradle-plugin</name> <url>https://github.com/qingye/AndroidAOPDemo</url> <description>android aop</description> <licenses> <license> <name>The Apache Software License, Version 2.0 < / name > < url > http://www.apache.org/licenses/LICENSE-2.0.txt < / url > < / license > < / licenses > < developers > <developer> <name>qingye</name> </developer> </developers> <scm> <url>https://github.com/qingye/AndroidAOPDemo</url> </scm> </project>Copy the code

A POM file is actually an XML file, and the most core and basic content should include:

  • The project tag, along with the specified protocols: XMLNS and XSI;
  • XML document version;
  • GroupId, artifactId, version: these three can absolutely locate a package
    • A groupId can have more than one artifactId;
    • An artifactId can have multiple versions;
  • Dependencies: Information about dependencies needed by the publication package
  • Licenses: legal agreements that users must meet to use the package;
  • Developers: developers information;
  • SCM: source repository address;

Additional information may be available, but this information is required to publish a package.

Maven-publish plugin

This plugin allows you to package modules, generate POM files, and publish them to a local directory. After using this plugin, Gradle panel displays the following options:

In Gradle Tasks, there is a Publishing Task, and there are many files that we can package and publish, or generate Metadata/Pom.

3.3. Generic Maven scripts For Java

3.3.1. Define POM general configuration files

// Create a new: // project-mvn-config.gradle file under the root directory /gradle.  ext { GROUP = 'com.chris.aop' VERSION_NAME = "${VERSION}" PROJECT_URL = 'https://github.com/qingye/AndroidAOPDemo' ISSUES_URL = "${PROJECT_URL}/issues" bintray_license = [' apache-2.0 '] BINTRAY_ORGANIZATION = "qingye" LICENSE = [NAME: "The Apache Software License, Version 2.0," URL: DEVELOPER = "http://www.apache.org/licenses/LICENSE-2.0.txt"] [ID: "", NAME : "qingye", EMAIL: "[email protected]" ] SCM = [ URL : "${PROJECT_URL}", COMMIT: "" ] }Copy the code

3.3.2 Other global configurations (root gradle.properties)

  • Modify the JVM memory configuration when Gradle is running
  • All release modules have the same version number
Org. Gradle. Jvmargs = - Xmx2048m - XX: XX: MaxPermSize = 512 m - + HeapDumpOnOutOfMemoryError - Dfile. Encoding = utf-8 VERSION = 1.0.0Copy the code

3.3.3 Reference configuration files in global build.gradle (Global effect)

3.3.4 Maven scripts for common Java libraries

// publish-java.gradle: // publish-java.gradle: 'maven - publish/pom/definition to the content of the def pomConfig = {licenses {license {name "${rootProject. Ext. License. The name}" url "${rootProject.ext.LICENSE.URL}" } } developers { developer { name "${rootProject.ext.DEVELOPER.NAME}" } } scm { url ${rootproject.ext.scm. URL}"}} task javadocJar(type: Jar, dependsOn:) ${rootproject.ext.scm. URL}"}} task javadocJar(type: Jar, dependsOn:) javadoc) { archiveClassifier = 'javadoc' from javadoc.destinationDir } task sourcesJar(type: Jar) {archiveClassifier = 'sources' from sourceSets. Main. Java srcDirs} / / Maven task/run/publishing, Publishing {publications {// Define a publication named Component, The type is MavenPublication // pom-default. XML file component(MavenPublication) {// Java Standard publishing artifacts provided by plug-ins, Java groupId = group artifactId = POM_ARTIFACT_ID version = Version artifact sourcesJar artifact javadocJar // append additional configurations pom.withXml { def root = asNode() root.appendNode('name', "${group}:${POM_ARTIFACT_ID}") root.appendNode('url', "${rootProject.ext.PROJECT_URL}") root.appendNode('description', 'Android aop') root.children().last() + pomConfig}}} // define maven repository name // 1. Public.repositories {maven {url ".. Public.repositories {maven {url ".. /repo" } }Copy the code

3.3.5 Module reference and use

  • Under the Java module you want to publish, create a new gradle.properties file for that module with the following contents:
POM_ARTIFACT_ID=aop-asm-gradle-plugin
POM_NAME=Android AOP ASM Gradle Plugin
POM_PACKAGING=jar
Copy the code
  • Modify build.gradle for the module
// Other plug-ins, required dependencies and other information...... // use the global groupId and version // artifactId for the group = "${rootproject.ext.group}" version = defined in gradle.properties "${rootProject.ext.VERSION_NAME}" /**************************************************************************** * Use command-line to build single module (at root project): * * $ ./gradlew :aop-asm-gradle-plugin:publish ****************************************************************************/ apply from: rootProject.file('gradle/publish-java.gradle')Copy the code

3.3.6. Release packaged

In the Terminal panel of Android Studio, enter the single module above to publish the command as shown below:

The JAR packages we need are generated.

Iv. Public Network Warehouse Publishing (JCenter)

If you can’t climb over the wall, just look at this part!

For Android developers, everyone is familiar with the repository, so I don’t need to introduce it. Today, I will show you how to register and use the repository.

4.1. Register an account and warehouse

  • JCenter address: bintray.com/

  • Sign Up for your Github account if you have one.

  • Click Sign Up with Github (this will require you to log into your Github account first)

  • Click on “Authorize bintray-bot”, and then need to complete other information (domestic mailboxes seem to be considered temporary mailboxes, preferably Gmail) :

  • Click “Complete Registration” and you will be automatically redirected to the home page

  • Create Maven Repository (after login, click “Add New Repository” on the home screen)

  • Click “Create” to Create the Maven repository as follows:

Let’s create our own Maven repository!

4.2 Add configuration and related dependencies to the project

4.2.1 Add JCenter account (root directory gradle.properties)

To obtain the Jcenter Key, click on the account -> Select “Edit” -> click on “API Key”, as shown below:

4.2.2 Build. Gradle configuration

Publish -java.gradle

4.2.4 Upload to JCenter

Once we’ve done the above configuration, there are two ways to package and upload it to JCenter’s Maven (the repository we created earlier, but didn’t actually publish to public view)

This example uses command line upload (with Unicom, mobile open hot spot upload fast, telecom… Take a chance) :

Bintray maven repository:

Note that there is a direct URL in the upper right corner, which we can configure in our root build.gradle:

Then Sync Now and view External Libraries:

It is available for download (not yet included in JCenter, but with this URL you can access the downloaded JAR package).

4.2.5 Requesting to Join the JCenter

Fill in your reasons and click “send”.

At this point, a complete process is OK!