我不太會 groovy 所以使用java? 來制作 插件
1;插件一個(gè)java library
2:在java library 中的? 添加 gradle? 依賴 和 安卓插件的依賴
dependencies{
? implementation gradleApi();
? implementation'com.android.tools.build:gradle:4.0.1'
}
4:創(chuàng)建一個(gè)實(shí)體類 來定義我們在插件 需要? 數(shù)據(jù)
====================================
public class xxxEntity {
? public Stringname;
? public? Stringpwd;
}
=========================
5:創(chuàng)建一個(gè)task (最自己的業(yè)務(wù))
========================
public class xxxTask? extends DefaultTask{
xxxEntity? ?entity;
@Inject(構(gòu)造函數(shù) 一定要加)
? ?public PGYUploadTask(xxxEntity? info) {
? ? ? this.entity= info;
? ? setGroup("taskGroupName");? ?// 設(shè)置task 的 組名
}
? ? @TaskAction
? ? public void dowrk() {
? ? ? ?// 做自己的業(yè)務(wù)
? ?例如: 上傳apk? ? 正常寫java的 網(wǎng)絡(luò)請求就行
? ? }
}
========================
5:插件一個(gè) java 類 實(shí)現(xiàn)?implements Plugin<Project>? 并實(shí)現(xiàn)?apply 方法
========================================
public class DingDingPluginimplements Plugin {
@Override
? ? public void apply(Project project) {
//?"taskName",? ?1: 是在 需要使用 插件的 build.gradle 中需要使用的 配置的名稱
//?xxxEntity? ??
? xxxEntity? ?xxEntity= project.getExtensions().create("xxxConfig",? ? xxxEntity? ?.class);
? ?project.afterEvaluate(new Action() {? ? // 在項(xiàng)目配置完 后執(zhí)行一個(gè)動作
@Override
? ? ? ? ? ? public void execute(final Project project) {
? ?//Project? ?可以獲取項(xiàng)目的信息?
? ? ? ? ? ? ?創(chuàng)建task 并執(zhí)行
//?xxxTask" “”? ?task 的名稱? ??xxxTask?.class: 要執(zhí)行的 task? ??xxEntity:? xxxTask 構(gòu)造函數(shù)的參數(shù)??
...? 可以傳遞多個(gè)參數(shù)
? ? ? ? ? ? ? ? xxxTask? ?sendTxt = project.getTasks().create("xxxTask", xxxTask?.class,xxEntity蒸矛,....);
//? 如果是需要多個(gè) task 執(zhí)行不同的任務(wù)? 創(chuàng)建taks的 歷程和上面一樣?
// 如果 多個(gè)?task 有關(guān)聯(lián)? 比如? A task? 之執(zhí)行完 在執(zhí)行 B? ?可以使用?task 的?dependsOn();
? ? ? bTask.dependsOn(A task);? // dependsOn表示在自己之前先執(zhí)行這個(gè)方法
======================? 如你想在 app build 完 在執(zhí)行task
AppExtension appExtension = project.getExtensions().getByType(AppExtension.class);
//? ? ? ? 得到一個(gè)集合? 默認(rèn)獲取? debug? 和release
? ? ? ? ? ? ? ? appExtension.getApplicationVariants().all(new Action<ApplicationVariant>() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void execute(ApplicationVariant applicationVariant) {
//? 獲取apk build? 完 之后的信息
? ? ? ? ? ? ? ? ? ? ? ? applicationVariant.getOutputs().all(new Action<BaseVariantOutput>() { /
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void execute(BaseVariantOutput baseVariantOutput) {
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 需要加固的? 文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? File outputFile = baseVariantOutput.getOutputFile();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String name = baseVariantOutput.getName();
.//? 執(zhí)行 task?
?xxxTask? ?sendTxt = project.getTasks().create("xxxTask",?xxxTask?.class,xxEntity,..outputFile?.);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
======================
}
========================================
6:配置 插件
在xx.properties 文件中配置 插件類的 全類型名
implementation-class=com.xxx.xxx.xxPlugin
7 上傳插件
在? library 中的? 添加 build.gradle 編寫
111111=========上傳本的倉庫
applyplugin:'maven-publish'
publishing{
//? ? Publication
? ? publications{
? ? ? ? xxPlugin(MavenPublication){
? ? ? ? ? ? fromcomponents.java
? ? ? ? ? ? groupId"com.xxx"? ?//? 組織名
? ? ? ? ? ? artifactId"xxx"? ? //?項(xiàng)目的唯一的標(biāo)識符驯击, 一般是項(xiàng)目名
? ? ? ? ? ? version"1.0.0"??
? ? ? ? }
? ? }
}
此時(shí)同步 一下 gradel? ?
======================= ?需要 指定倉庫 地址可以添加?
repositories{
? ? maven{
? ? ? ? //指定要上傳的maven私服倉庫1
? ? ? ? url="../repo"
? ? ? ? /*? url = "http://jenkins.maxrocky.com:8080/maven/content/repositories/thirdparty/"
? ? ? ? ? //認(rèn)證用戶和密碼credentials {
username 'admin'
password 'maxrocky5721'
}*/
? ? }
}
然后 同步? 上傳
=============================