您的位置:首页 > 移动开发 > Android开发

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

2017-04-27 11:10 651 查看

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
}


说明:

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)
}
}
}


说明 :

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

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


buildProguardJar <= buildJar <=  assembleRelease


混淆后文件: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
}
}


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

(这是一行命令)

gradle :core:buildJar
:core:generatePomFileForMavenJavaPublication
:core:artifactoryPublish


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

gradle :core:buildProguardJar
:core:generatePomFileForMavenJavaPublication
:core:artifactoryPublish


附上粗糙的执行运行图:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android gradle jar 混淆