背景:因为某些需求,需要将android appA打包为aar供其他工程使用,但是有些class有冲突,只能将appA中的类排除。
具体做法:
在build.gradle中排除指定的类,
// build.gradle顶部引入import java.text.SimpleDateFormat// 创建一个时间格式化器
def dateFormat = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')afterEvaluate {// 找到负责打包 AAR 的任务tasks.matching { it.name == 'compileReleaseJavaWithJavac' }.configureEach { bundleAarTask ->def arrFile = 'build/outputs/aar/sdk_module_a-release.aar'if (!file(arrFile).exists()) {println "${dateFormat.format(new Date())} - 12file of sdk_module_a does not exist."} else {println "${dateFormat.format(new Date())} - 12file of sdk_module_a does exist."}// 在打包 AAR 前执行删除类文件的任务println "${dateFormat.format(new Date())} - 18compileReleaseJavaWithJavac delete class."// 打印日志,表明即将开始打包 AARbundleAarTask.doFirst {println "${dateFormat.format(new Date())} - 18Starting to compileReleaseJavaWithJavac..."}bundleAarTask.doLast {println "${dateFormat.format(new Date())} - 18last to compileReleaseJavaWithJavac..."def baseDir = 'build/intermediates/javac/release/classes/com/x/y/navi'def classesToDelete = ["$baseDir/NaviCsv.class","$baseDir/NaviCsv\$1.class","$baseDir/NaviCsv\$Zxc.class"]classesToDelete.each { classFile ->if (file(classFile).exists()) {println "${dateFormat.format(new Date())} - 18-02 ${classFile} exist"} else {println "${dateFormat.format(new Date())} - 18-02 ${classFile} file not exist"}}// 删除操作delete classesToDeleteclassesToDelete.each { classFile ->if (file(classFile).exists()) {println "${dateFormat.format(new Date())} - 18-03Failed to delete ${classFile}"} else {println "${dateFormat.format(new Date())} - 18-03delete ${classFile} file ok"}}}}
}
执行对应的构建命令
./gradlew :sdk_module_a:assembleRelease