Article directory
Gradle plug-in development development learning, publishing repo local warehouse
1. Gradle plug-in
Provide specific build functions
Improve code reusability
binary plugin
Script plugin
In the build.gradle file under the project
binary plugin
// Top-level build file where you can add configuration options common to all sub-projects/modules.
//声明插件ID与版本号
plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
id 'org.jetbrains.kotlin.jvm' version '1.8.20' apply false
}
Apply plugin
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
Script plug-in, create a new gradle script file
Add the association of this plug-in in the sub-process
2. Gradle plug-in development, page routing
Mark pages, collect pages, generate documents, register mappings, open pages
1.buildSrc
Configuration
//引用groovy插件,编译插件工程中的代码
apply plugin: 'groovy'
//声明仓库的地址
repositories {
mavenCentral()
}
//声明依赖的包
dependencies{
implementation gradleApi()
implementation localGroovy()
}
2. Plug-in operation
Create this directory manually
Configuration
Test it in the app module
Correct output
3. Parameter configuration
Similar to the following are parameter configurations
android {
namespace 'com.qfh.common'
compileSdk cfg.android.compileSdk
defaultConfig {
applicationId cfg.applicationId.app
minSdk cfg.android.minSdk
targetSdk cfg.android.targetSdk
versionCode cfg.android.versionCode
versionName cfg.android.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}
Define Extension, register Extension, use Extension, obtain Extension
Define a new class
Register Extension, callback.
Set our parameters, and the parameter values set by our user will be obtained in the above callback.
3. Release the simple version of Gradle plug-in
3.1 Publish to local warehouse
//调用maven插件,用于发布
apply plugin: 'maven-publish'
publishing {
repositories {
maven {
//设置发布路径为工程根目录下面的 repo文件夹
url = uri('../repo')
}
}
publications {
maven(MavenPublication) {
//设置groupId,通常为包名
groupId = 'com.qfh.route'
//设置artifactId,为当前插件的名称
artifactId = 'router-gradle-plugin'
version = '1.0.0'
}
}
}
Copy a copy of buildSrc and rename it to the name of the plug-in router-gradle-plugin. You cannot publish the plug-in directly in buildSrc.
Register this copied subproject in the root directory of the project
Double-click publish to publish to the local warehouse
Finally, there is one more folder directory