1, the plug-in

Plug-ins: The things that define and concretely execute these tasks.

1.1 The apply function

Apply is a function that loads packaged plug-ins or other Gradle files.

void apply(Closure closure);
void apply(Action<? super ObjectConfigurationAction> action);

/ / the commonly used
void apply(Map
       
         options)
       ,>;
Copy the code

The Apply function can contain three types of key-value pairs in the parameter map: plugin, FROM, and to

  • From: Applies a script to the local PC
  • Plugin: Apply a plugin
  • To: Applies to an object
  1. Java – related Gradle plugin
Apply the plugin:'java'
apply plugin: 'java-library'
Copy the code
  1. Android Gradle plugin
apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
apply plugin: 'com.android.test'
Copy the code
  1. other
apply plugin: 'com.android.dynamic-feature'
apply plugin: 'maven'
// Loads a script from the specified path
apply from: rootProject.getRootDir().path + "/xxx/xxx"
// Only?? A module loaded with the Apply plugin: 'com.android.library' inserts the module to be published and packaged as an AAR or JAR
Copy the code

2. Common plug-ins

2.1 Apply plugin: com.android. Application

// Android {} is the entry to the Android app configuration item in the project
android{
  compileSdkVersion 28
  buildToolsVersion '28.0.3'
  // One of the configuration blocks, responsible for default configuration, such as package name and version number
  defaultConfig {
      applicationId "com.example.myapp"
      minSdkVersion 23
      targetSdkVersion 29
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }
  buildTypes {
      release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
      }
  }
}

// Introduce dependencies
/ / TODO need to continue to research, refer to https://baijiahao.baidu.com/s?id=1628320215436928396&wfr=spider&for=pc
dependencies{}Copy the code

/ / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

2.2 Apply Plugin: ‘Maven’ plugin

  • UploadArchives: uploadArchives is a Task that publishes libraries to a central repository. We need to specify the local repository path for uploadArchives and some information about the library

The official documentation

  // Application plug-in
  apply plugin: 'maven'

  uploadArchives {
      repositories {
          mavenDeployer {
            // Configure the repository path, in the repository directory under the project root directory

            // It can be a local or remote repository
            // If the version suffix is' -snapshot ', the source code of the snapshotRepository TODO implementation is automatically used.
            repository(url: uri('.. /repository')){
              authentication(userName: 'username', password: 'pwd')
            }


            snapshotRepository(url:'http://xxxxxx.com'){
              authentication(userName: 'username', password: 'pwd')}// Using the Pom. project builder, you can add any Maven-related POM Reference elements to it
            // http://maven.apache.org/pom.html
              pom.project {
                  version rootProject.ext.sdk_version_name + '-SNAPSHOT'
                  artifactId 'common'
                  groupId 'com.example.mylib'
                  packaging 'aar'}}}}Copy the code

2.3 Apply plugin: ‘maven-publish

Maven-publish is a Gradle plug-in that publishes output artifacts to the Maven repository. Maven Publish Plugin

  1. Duplicate the Publishing node
  2. There are two configurations in this node: Publications and Repositories
// Application plug-in
apply plugin: 'maven-publish'

ext {
    DIR = "build/outputs/aar/"
}

publishing {
    repositories {
        maven {
            // url uri('.. /maven_repo')
            url = 'http://www.example.com/library'
            // The user name and password of the warehouse
            credentials {
              username myusername
              password mypwd
            }
        }  
    }

    publications {
        customName(MavenPublication) {
           // Unique identifier (usually the module package name, but can be arbitrary)
            groupId 'com.example.mylib'
            // Project name (usually the name of the class library module, but optionally)
            artifactId project.name
            / / version number
            / / version = "1.0.0"
            version '1.2.3.300'}}}Copy the code