Gradle 知识汇总

gradle基本属性分析

Gradle基础介绍

Gradle常见插件开发方式

Gradle自定义插件开发

自定义gradle插件实现一键使用360加固

插件是一种遵循一定规范的应用程序接口编写出来的程序。其只能运行在程序规定的系统平台下(可能同时支持多个平台),而不能脱离指定的平台单独运行。Gradle就是一个Java程序,Gradle的插件可以在这个Java程序执行过程中执行额外的代码
 
 
 
 
 

 

一、作为独立项目,发布到仓库

新建java library:

创建plugin插件类,在引入插件的时候获取应用内app信息,并存入task和bean

package com.enjoy.plugin

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.api.BaseVariantOutput
import com.android.builder.model.SigningConfig
import org.gradle.api.Plugin
import org.gradle.api.Project

class JiaguPlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        Jiagu jiagu = project.extensions.create("jiagu", Jiagu)
        //回调, 在gradle配置完成之后回调,在解析完build.gradle之后回调
        project.afterEvaluate {
            AppExtension android = project.extensions.android
            android.applicationVariants.all {
                ApplicationVariant variant ->
                    //对应变体(debug/release)的签名配置
                    SigningConfig signingConfig = variant.signingConfig
                    variant.outputs.all {
                        BaseVariantOutput output->
                            //输出的apk文件
                            File apk = output.outputFile
                            //创建加固任务 定义加固的插件名字
                            JiaguTask jiaguTask = project.tasks.create("jiagu${variant.baseName.capitalize()}", JiaguTask)
                            jiaguTask.jiagu = jiagu
                            jiaguTask.signingConfig = signingConfig
                            jiaguTask.apk = apk
                    }
            }
        }
    }
}

创建插件bean

package com.enjoy.plugin;

class Jiagu {
    String userName;
    String password;
    String jiaguTools

    String getUserName() {
        return userName
    }

    void setUserName(String userName) {
        this.userName = userName
    }

    String getPassword() {
        return password
    }

    void setPassword(String password) {
        this.password = password
    }

    String getJiaguTools() {
        return jiaguTools
    }

    void setJiaguTools(String jiaguTools) {
        this.jiaguTools = jiaguTools
    }
}

创建任务,实际调用360加固包的命令行

package com.enjoy.plugin

import com.android.builder.model.SigningConfig
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction;

class JiaguTask extends DefaultTask {

    Jiagu jiagu
    SigningConfig signingConfig
    File apk

    //定义插件运行的位置,方便在gradle里面找
    JiaguTask() {
        group = "jiagu"
    }

    @TaskAction
    def run() {
        //调用命令行工具
        project.exec {
            // java -jar jiagu.jar -login user password
            it.commandLine("java", "-jar", jiagu.jiaguTools, "-login", jiagu.userName, jiagu.password)
        }

        if (signingConfig) {
            project.exec {
                // java -jar jiagu.jar -importsign  xxxx
                it.commandLine("java", "-jar", jiagu.jiaguTools, "-importsign", signingConfig.storeFile.absolutePath,
                        signingConfig.storePassword, signingConfig.keyAlias, signingConfig.keyPassword)
            }
        }
        project.exec {
            // java -jar jiagu.jar -jiagu  xxxx
            it.commandLine("java", "-jar", jiagu.jiaguTools, "-jiagu", apk.absolutePath,
                    apk.parent, "-autosign")
        }


    }
}
在插件gradle设置,定义上传发布的参数
 
apply plugin: 'groovy'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.tools.build:gradle:3.5.2'
    implementation gradleApi()
}

sourceCompatibility = "7"
targetCompatibility = "7"

//发布的marven仓库

apply plugin:'maven-publish'

publishing{
    publications{
        Jiagu(MavenPublication){
            from components.java  //要把源码生成的jar包上传
            groupId 'com.enjoy'
            artifactId "jiagu"
            version "1.0"
        }
    }
}

项目gradle引入,在整个项目里引入

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'

        classpath 'com.enjoy:jiagu:1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app.gradle的处理


apply plugin: 'com.android.application'

//添加引用
apply plugin:'com.enjoy.jiagu'

//配置基础信息,可以在自定义插件获取信息,配置
jiagu{
    userName '360U3190525523'
    password 'aaaa'
    jiaguTools 'F:\\gradle\\360jiagubao_windows_64\\jiagu\\jiagu.jar'
}


android {

    signingConfigs {
        release {
            storeFile file('enjoy.keystore')
            storePassword '123456'
            keyAlias 'enjoy'
            keyPassword '123456'
        }
    }


    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.enjoy.jiagu"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}



这样。当我们在gradle列表找到

jiaguRelease或者jiaguDebug就可以自动化产生打包了
 
 
 

脚本方式

直接在build.grade里面写,但是只能在本模块使用

 

BuildSrc方式

Gradle_使用buildSrc方式自定义gradle插件

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值