打Jar包的几种方式:
def SDK_BASENAME = "Sdk";//打包后名字
def SDK_VERSION = "_V1.0";//${defaultConfig.versionName}
def sdkDestinationPath = "build";//保存目录
def zipFile = file('build/intermediates/bundles/release/classes.jar')
task deleteBuild(type: Delete) {//删除jar包
delete sdkDestinationPath + "/" + SDK_BASENAME + SDK_VERSION + ".jar"
}
task makeJar(type: Jar) {//生成jar
from zipTree(zipFile)
from fileTree(dir: 'src/main', includes: ['assets/**'])
baseName = SDK_BASENAME + SDK_VERSION
destinationDir = file(sdkDestinationPath)//目的地
}
makeJar.dependsOn(deleteBuild, build)
//在终端执行生成JAR包
// gradlew makeJar
def SDK_BASENAME = "Sdk";//打包后名字
def SDK_VERSION = "_V1.0";//${defaultConfig.versionName}
def SDK_NAME = SDK_BASENAME + SDK_VERSION + ".jar";
//Copy类型
task makeJar(type: Copy) {
//删除存在的
delete 'build/libs/' + SDK_NAME
//设置拷贝的文件
from('build/intermediates/bundles/release/')
//打进jar包后的文件目录
into('build/libs/')
//将classes.jar放入build/libs/目录下
//include ,exclude参数来设置过滤
//(我们只关心classes.jar这个文件)
include('classes.jar')
//重命名
rename('classes.jar', SDK_NAME)
}
makeJar.dependsOn(build)
//在终端执行生成JAR包
//gradlew makeJar
task clearJar(type: Delete) {
delete 'libs/sdk.jar'
}
task makeJar(type: org.gradle.api.tasks.bundling.Jar) {
//指定生成的jar名
baseName 'sdk'
//从哪里打包class文件
from('build/intermediates/classes/debug/org/cmdmac/cloud/pluginsdk/')
//打包到jar后的目录结构
into('build/libs/')
//去掉不需要打包的目录和文件
exclude('test/', 'BuildConfig.class', 'R.class')
//去掉R$开头的文件
exclude { it.name.startsWith('R$'); }
}
makeJar.dependsOn(clearJar, build)
//在终端执行生成JAR包
//gradlew makeJar
def SDK_BASENAME = "Sdk";//打包后名字
def SDK_VERSION = "_V1.0";//${defaultConfig.versionName}
def SDK_NAME = SDK_BASENAME + SDK_VERSION + ".jar";
def zipFile = file('build/intermediates/bundles/release/classes.jar')
def sdkDestinationPath = "build/libs/";//保存目录
task makeJar(type: Jar) {//生成jar
delete 'build/libs/' + SDK_NAME
from zipTree(zipFile)
from fileTree(dir: 'src/main', includes: ['assets/**'])
from(project.zipTree('libs/xutils-3.3.4.jar'))
from(project.zipTree('libs/glide-3.7.0.jar'))
from(project.zipTree('libs/xutils-3.3.4.jar'))
baseName = SDK_BASENAME + SDK_VERSION
destinationDir = file(sdkDestinationPath)//目的地
}
makeJar.dependsOn(build)
选择以上任意一段代码,然后使用以下任意一种方法打Jar包。
方式一:
方式二:
Jar包混淆:
由于这个项目使用了NDK,所以以下NDK配置请略过。
android {
compileSdkVersion versions.compileSdk
buildToolsVersion versions.buildTools
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
versionName project.version
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
consumerProguardFiles 'proguard-rules.pro'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
externalNativeBuild {
ndkBuild {
arguments "NDK_DEBUG=${isDebugBuild() ? '1' : '0'}"
}
}
}
externalNativeBuild {
ndkBuild {
path 'src/main/c/Android.mk'
}
}
sourceSets {
main {
//你的源码目录
jniLibs.srcDirs 'src/main/libs'
}
}
repositories {
flatDir {
dirs 'src/main/libs'
}
}
buildTypes {
release {
minifyEnabled false//混淆打包
shrinkResources true//去掉不用资源
// signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
混淆配置需要根据自己项目情况酌情配置,配置文件可以去SDK目录拷贝。