Skip to content

Commit 09e7e0a

Browse files
committed
build: 修复IDE无法直接Run项目的问题
经过反复分析现有代码,无法在不改动SDK层代码的前提下修复这个问题。 主要是因为Shadow的Gradle plugin有硬编码outputs目录的情况。 并且这个Plugin还假设了Gradle配置阶段就能确定apk的最终路径, 这与现状不符。目前是配置完成后,IDE还能决定将apk的输出路径改到intermediates中。 所以很难简单的修复这个假设的错误。 考虑到主要要解决的问题还是Shdaow自身项目希望能在IDE中直接run起来。 决定还是将intermediates中的apk直接复制到原本的路径。这样其他代码都不用改动。 fix #1263
1 parent 04094e0 commit 09e7e0a

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ buildscript {
3131
apply from: 'buildScripts/gradle/common.gradle'
3232

3333
apply from: "buildScripts/gradle/maven.gradle"
34+
35+
apply from: "buildScripts/gradle/fix_issue_1263.gradle"
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)