|
| 1 | +/** |
| 2 | + * 这个脚本通过hook create*ApkListingFileRedirect任务, |
| 3 | + * 在它执行完成后,即生成了apk_ide_redirect_file,也应该生成了apk之后, |
| 4 | + * 补充一个复制build/intermediates/apk到build/outputs/apk的操作。 |
| 5 | + * |
| 6 | + * 采用这个修复方式是因为Shadow的打包代码设计不是很合理,难以通过少量改动, |
| 7 | + * 保证引用项目不引入任何兼容性问题。 |
| 8 | + * |
| 9 | + * 详见issue #1263 |
| 10 | + */ |
| 11 | +buildscript { |
| 12 | + dependencies { |
| 13 | + classpath files(rootProject.buildscript.configurations.classpath) |
| 14 | + } |
| 15 | +} |
| 16 | +def taskList = [ |
| 17 | + ":sample-loader:createDebugApkListingFileRedirect", |
| 18 | + ":sample-loader:createReleaseApkListingFileRedirect", |
| 19 | + ":sample-runtime:createDebugApkListingFileRedirect", |
| 20 | + ":sample-runtime:createReleaseApkListingFileRedirect", |
| 21 | + ":sample-manager:createDebugApkListingFileRedirect", |
| 22 | + ":sample-manager:createReleaseApkListingFileRedirect", |
| 23 | + ":sample-app:createPluginDebugApkListingFileRedirect", |
| 24 | + ":sample-app:createPluginReleaseApkListingFileRedirect", |
| 25 | + ":sample-base:createPluginDebugApkListingFileRedirect", |
| 26 | + ":sample-base:createPluginReleaseApkListingFileRedirect", |
| 27 | + |
| 28 | + ":test-dynamic-loader:createDebugApkListingFileRedirect", |
| 29 | + ":test-dynamic-loader:createReleaseApkListingFileRedirect", |
| 30 | + ":test-dynamic-runtime:createDebugApkListingFileRedirect", |
| 31 | + ":test-dynamic-runtime:createReleaseApkListingFileRedirect", |
| 32 | + ":test-dynamic-manager:createDebugApkListingFileRedirect", |
| 33 | + ":test-dynamic-manager:createReleaseApkListingFileRedirect", |
| 34 | + ":plugin-service-for-host:createPluginDebugApkListingFileRedirect", |
| 35 | + ":plugin-service-for-host:createPluginReleaseApkListingFileRedirect", |
| 36 | + ":test-plugin-androidx-cases:createPluginDebugApkListingFileRedirect", |
| 37 | + ":test-plugin-androidx-cases:createPluginReleaseApkListingFileRedirect", |
| 38 | + ":test-plugin-general-cases:createPluginDebugApkListingFileRedirect", |
| 39 | + ":test-plugin-general-cases:createPluginReleaseApkListingFileRedirect", |
| 40 | + |
| 41 | + ":sample-hello-apk:createDebugApkListingFileRedirect", |
| 42 | + ":sample-hello-apk:createReleaseApkListingFileRedirect", |
| 43 | +] |
| 44 | + |
| 45 | +afterEvaluate { |
| 46 | + taskList.forEach { |
| 47 | + def t = tasks.findByPath(it) |
| 48 | + copyApkAfterTask(t) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +def copyApkAfterTask(t) { |
| 53 | + t.doLast { |
| 54 | + def redirectFile = t.getOutputs().getFiles().singleFile |
| 55 | + def listingFile = redirectFile.readLines().get(1).replaceFirst("listingFile=", "") |
| 56 | + def metadataFile = new File(redirectFile.parentFile, listingFile) |
| 57 | + def metadata = new org.json.JSONObject(metadataFile.text) |
| 58 | + def outputFile = metadata.getJSONArray("elements").getJSONObject(0).getString("outputFile") |
| 59 | + def apkFile = new File(metadataFile.parentFile, outputFile) |
| 60 | + def testRelativePath = redirectFile.relativePath(apkFile) |
| 61 | + def needCopy = !testRelativePath.matches("^(\\.\\.${File.separatorChar})+outputs${File.separatorChar}.+") |
| 62 | + if (needCopy) { |
| 63 | + def matchPath = new File("/build/intermediates").toPath().toString() |
| 64 | + def intermediatesDir = new File(apkFile.toPath().normalize().toString().find("^.+?$matchPath")) |
| 65 | + def outputsDir = new File(intermediatesDir.parentFile, "outputs") |
| 66 | + def r = copy { |
| 67 | + from intermediatesDir |
| 68 | + into outputsDir |
| 69 | + include 'apk/**' |
| 70 | + } |
| 71 | + if (r.didWork) { |
| 72 | + getLogger().info("copy apk from ${intermediatesDir.path} to ${outputsDir.path}") |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments