简书链接:kts代替gradle升级指南高阶玩法 文章字数:132,阅读全文大约需要1分钟 1.整体拦截自定义版本号_渠道名_编译时间.apk 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 import com.android.build.gradle.LibraryExtension import java.text.SimpleDateFormat // Top-level build file where you can add configuration options common to all sub-projects/modules. //apply ( "config.gradle.kts") buildscript { // apply("versions.gradle.kts") repositories { maven("https://maven.aliyun.com/nexus/content/groups/public/") maven("https://maven.aliyun.com/repository/google") maven("https://maven.aliyun.com/repository/jcenter") maven("https://maven.aliyun.com/repository/gradle-plugin") mavenCentral() google() maven("https://jitpack.io") jcenter() maven("https://developer.huawei.com/repo/") // google() } //extra["versions"] = "1.4.30" //val versions: Any by extra dependencies { classpath("com.android.tools.build:gradle:7.4.0-alpha03") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21") // id 'org.jetbrains.kotlin.android' version '1.6.21' apply false // hms classpath("com.huawei.agconnect:agcp:1.6.2.300") subprojects { afterEvaluate { val isAndroidLibrary: Boolean = (project.pluginManager.hasPlugin("com.android.library")) print("是否是android库" + isAndroidLibrary) // if (isAndroidLibrary) { val ignoreEncrypt: Boolean; var moduleName = project.name; when (moduleName) { "base_interface", "base_extend", "jpush_aar", "writeexcel", "third_xc_chartlib", "third_print_sdk", "third_picture_library", "third_magicindicator", "third_form", "third_consecutivescroller" -> ignoreEncrypt = true; else -> ignoreEncrypt = false } if (ignoreEncrypt) { System.err.println("library-ignore-encrypt------" + project.name); } else { System.err.println("library-need-encrypt-str" + project.name); plugins.withId("com.android.application") { System.err.println("app字符串加密???" + project.name + "," + extensions.findByType(com.android.build.gradle.AppExtension::class)); extensions.findByType(com.android.build.gradle.AppExtension::class)?.run { // applicationVariants.forEach System.err.println("run exec AppExtension" + applicationVariants.size) applicationVariants.all(Action { java.lang.System.err.println(" applicationVariants.all-------" + applicationVariants.size) if (!this.buildType.isDebuggable) { var myBuildTime = SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis()) val outputFileName = "${productFlavors.first().name}channel_-${this.versionName}_${this.versionCode}-${myBuildTime}.apk" outputs.all { val output = this as? com.android.build.gradle.internal.api.BaseVariantOutputImpl output?.outputFileName = outputFileName } } this.javaCompileProvider.configure { doLast { System.err.println("module[${project.name}]Encrypt")// val provider = this@configure print("build dir ${provider.destinationDirectory.get()}\n"); if ("${provider.destinationDirectory.get()}".toLowerCase() .contains("release") ) { println("start module classes obfuscation " + "${provider.destinationDirectory.get()}") try { javaexec { defaultCharacterEncoding = "utf-8"//这里传错会导致解密出现问题, mainClass.set("-jar") args( "${rootDir.absolutePath}/obfuseStringGradle.jar", project.name, provider.destinationDirectory.get(), "../ignore_class.txt", MyConfig.ENCRYPT_CONFIG_APP_JSON ) } } catch (e: Throwable) { e.printStackTrace() System.err.println("exec encrypt fail.. " + "${e.message}") } } else { println("notReleaseApk,ignore class obfuscation " + "${provider.destinationDirectory.get()}") } } } }) } } plugins.withId("com.android.library") { System.err.println( "模块加密" + project.name + "," + extensions.findByType( LibraryExtension::class ) ); extensions.findByType(LibraryExtension::class)?.run { System.out.println("run exec LibraryExtension:" + libraryVariants.size) /* libraryVariants.forEach { variant -> libraryVariants.all { variant -> variant.javaCompileProvider.configure {*/ libraryVariants.all(Action { System.err.println("libraryVariants-------" + libraryVariants.size) this.javaCompileProvider.configure { doLast { System.err.println("module[${project.name}]Encrypt")// val provider = this@configure print("build dir ${provider.destinationDirectory.get()}\n"); if ("${provider.destinationDirectory.get()}".toLowerCase() .contains("release") ) { println("start module classes obfuscation " + "${provider.destinationDirectory.get()}") try { javaexec { defaultCharacterEncoding = "utf-8"//这里传错会导致解密出现问题, mainClass.set("-jar") args( "${rootDir.absolutePath}\\obnew.jar", project.name, provider.destinationDirectory.get(), "${rootDir.absolutePath}/ignore_class.txt", MyConfig.ENCRYPT_MODULE_JSON ) } } catch (e: Throwable) { e.printStackTrace() System.err.println("exec encrypt fail.. " + "${e.message}") } } else { println("notReleaseApk,ignore class obfuscation " + "${provider.destinationDirectory.get()}") } } } }) } } } dependencies { // android dependencies here // all subprojects dependencies here } } } allprojects { repositories { google() mavenCentral() maven("https://maven.aliyun.com/nexus/content/groups/public/") maven("https://maven.aliyun.com/repository/google") maven("https://maven.aliyun.com/repository/jcenter") maven("https://jitpack.io") maven("https://mvn.getui.com/nexus/content/repositories/releases/") } configurations.all { // exclude module: "spring-boot-devtools" } } /** * 不包括当前根工程,只包括子工程,添加公共配置 */ tasks.register("testXXXXX") { MyConfig.a() } tasks.register<Delete>("clean") { delete(rootProject.buildDir) }