Android - Gradle 使用干货 之 生成 jar 包 和 生成混淆的 jar 包 和 上传

 

Android - Gradle 使用干货 之 生成 jar 包 和 生成混淆的 jar 包 和 上传 artifactory

标签: androidgradlejar混淆
  113人阅读  评论(0)  收藏  举报
  分类:
 

目录(?)[+]

1. 前言

Android - Gradle 使用干货 之 artifactory仓库上传arr 配置

前面已经说了上传本地仓库的使用方法,这篇将生成 jar 包 和 混淆 jar 包 并上传到 本地仓库;

2. 生成 jar 包

使用 gradle 的 自定义任务 生成 jar 包,比如:

task buildJar(dependsOn: ['assembleRelease'], type: Jar) {
    //后缀名
    extension = "jar"
    //最终的 Jar 包名
    archiveName = "${project.getName()}-release.jar"
    //需打包的资源所在的路径集
    def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"];
    //初始化资源路径集
    from srcClassDir
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

说明:

buildJar 任务 依赖于 assembleRelease task , 在 执行该任务的时候,会先执行 asembleRelease 任务,后再将 release 的内容 build 成jar

生成文件在 : module_name/build/libs/xxx-release.jar


3. 混淆 jar 包

想要混淆 jar 包 ,需要先生成 jar 包 ,后再混淆,新建混淆任务:

task buildProguardJar(dependsOn: ['buildJar'], type: ProGuardTask) {
    //Android 默认的 proguard 文件, 默认的混淆文件有时候出错
    //configuration android.getDefaultProguardFile('proguard-android.txt')
    //会根据该文件对 Jar 进行混淆,注意:需要在 manifest 注册的组件也要加入该文件中
    configuration 'proguard-rules.pro'
    String inJar = buildJar.archivePath.getAbsolutePath()
    //输入 jar
    injars inJar
    //输出 jar
    outjars inJar.substring(0, inJar.indexOf(".")) + "_proguard.jar"
    //设置不删除未引用的资源(类,方法等)
    dontshrink
    AppPlugin appPlugin = getPlugins().findPlugin(AppPlugin)
    if (appPlugin != null) {
        List<String> runtimeJarList
        if (appPlugin.getMetaClass().getMetaMethod("getRuntimeJarList")) {
            runtimeJarList = appPlugin.getRuntimeJarList()
        } else if (android.getMetaClass().getMetaMethod("getBootClasspath")) {
            runtimeJarList = android.getBootClasspath()
        } else {
            runtimeJarList = appPlugin.getBootClasspath()
        }

        for (String runtimeJar : runtimeJarList) {
            //给 proguard 添加 runtime
            libraryjars(runtimeJar)
        }
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

说明 :

执行该任务时,依次会执行的任务:

先进行 release 编译,其次 生成 jar 包 ,最后 混淆 jar 包

buildProguardJar <= buildJar <=  assembleRelease
   
   
  • 1
  • 1

混淆后文件:module_name/build/libs/xxx-release_proguard.jar


4. build.gradle

import com.android.build.gradle.AppPlugin
import proguard.gradle.ProGuardTask

apply plugin: 'com.android.library'
apply from: "$rootDir/config.gradle"
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'

def MAVEN_LOCAL_PATH = urls.localUploadPath
def ARTIFACT_ID = artifactIds.core
def VERSION_NAME = versions.core
def GROUP_ID = groupIds.core

// 编译类型:1 不混淆,2 混淆 ,无需修改,执行不同命令生成不同的jar文件
def build_type = 1

/**
 * 执行命令:
 * (这是一条命令)
 * gradle :core:buildJar 
 * :core:generatePomFileForMavenJavaPublication 
 * :core:artifactoryPublish
 */

android {
    compileSdkVersion versions.complieSdk
    buildToolsVersion versions.buildTools

    defaultConfig {
        minSdkVersion versions.minSdk
        targetSdkVersion versions.targetSdk
        versionCode versions.code
        versionName versions.name
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile libs.gson
    //ok http
    compile libs.okhttpLib
    compile libs.okhttpInterceptor
    //retrofit2
    compile libs.retrifit2Lib
    compile libs.retrifit2Gson
    compile libs.retrifit2Rxjava2Adapter
    //rx java2
    compile libs.rxJava2
    compile libs.rxAndroid
    //glide
    compile libs.glide
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId GROUP_ID
            version = VERSION_NAME
            artifactId ARTIFACT_ID
            // Tell maven to prepare the generated "*.jar" file for publishing
            println(build_type)
            def artifactPath = "$buildDir/libs/${project.getName()}-release.jar"
            if (build_type == 2) {
                artifactPath = "$buildDir/libs/${project.getName()}-release_proguard.jar"
            }
            println(artifactPath)
            artifact(artifactPath)
        }
    }
}

artifactory {
    contextUrl = MAVEN_LOCAL_PATH
    publish {
        repository {
            // The Artifactory repository key to publish to
            repoKey = localRespos.repoName
            username = localRespos.userName
            password = localRespos.password
            maven = true
        }
        defaults {
            publications('mavenJava')
            publishArtifacts = true
            publishPom = true
        }
    }
    resolve {
        repoKey = 'jcenter'
    }
}

task buildJar(dependsOn: ['assembleRelease'], type: Jar) {
    build_type = 1
    //后缀名
    extension = "jar"
    //最终的 Jar 包名
    archiveName = "${project.getName()}-release.jar"
    //需打包的资源所在的路径集
    def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"];
    //初始化资源路径集
    from srcClassDir
}

task buildProguardJar(dependsOn: ['buildJar'], type: ProGuardTask) {
    //Android 默认的 proguard 文件
    //configuration android.getDefaultProguardFile('proguard-android.txt')
    //会根据该文件对 Jar 进行混淆,注意:需要在 manifest 注册的组件也要加入该文件中
    configuration 'proguard-rules.pro'
    String inJar = buildJar.archivePath.getAbsolutePath()
    //输入 jar
    injars inJar
    //输出 jar
    outjars inJar.substring(0, inJar.indexOf(".")) + "_proguard.jar"
    //设置不删除未引用的资源(类,方法等)
    dontshrink
    AppPlugin appPlugin = getPlugins().findPlugin(AppPlugin)
    if (appPlugin != null) {
        List<String> runtimeJarList
        if (appPlugin.getMetaClass().getMetaMethod("getRuntimeJarList")) {
            runtimeJarList = appPlugin.getRuntimeJarList()
        } else if (android.getMetaClass().getMetaMethod("getBootClasspath")) {
            runtimeJarList = android.getBootClasspath()
        } else {
            runtimeJarList = appPlugin.getBootClasspath()
        }

        for (String runtimeJar : runtimeJarList) {
            //给 proguard 添加 runtime
            libraryjars(runtimeJar)
        }
        build_type = 2
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141

生成不混淆的 jar 包 并上传到 本地仓库: 
(这是一行命令)

gradle :core:buildJar 
:core:generatePomFileForMavenJavaPublication 
:core:artifactoryPublish
   
   
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

生成混淆的 jar 包 并上传到 本地仓库:

gradle :core:buildProguardJar
:core:generatePomFileForMavenJavaPublication 
:core:artifactoryPublish
   
   
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

附上粗糙的执行运行图:

这里写图片描述


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值