Android - Gradle 使用干货 之 生成 jar 包 和 生成混淆的 jar 包 和 上传
程序员文章站
2024-02-03 18:17:04
...
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
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
附上粗糙的执行运行图: