0%

构建编译ProcessResource

3.2.1版本正常主项目的编译流程

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
|------------------------------------------------------------------------
| processDebugResources , app|
|------------------------------------------------------------------------
| com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask_Decorated
|------------------------------------------------------------------------
| inputs file|
|-----------------------------------------------------------------
| /Users/juneleo/demo/CardControl/app/build/intermediates/aar_metadata_check/debug
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/compiled_local_resources/debug/out
| /Users/juneleo/.gradle/caches/transforms-2/files-2.1/0440b26303762a0d09f988d28effd92e/android.support.v7.recyclerview
| /Users/juneleo/.gradle/caches/transforms-2/files-2.1/b9be6620532dc927d1cf2fc250571de2/android.support.design-r.txt
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt
| /Users/juneleo/demo/CardControl/app/build/intermediates/res/merged/debug
| /Users/juneleo/demo/CardControl/app/build/intermediates/packaged_manifests/debug
| /Users/juneleo/demo/CardControl/app/build/intermediates/merged_manifests/debug
| /Users/juneleo/Library/Android/sdk/platforms/android-26/android.jar
|-----------------------------------------------------------------
| outputs file|
|-----------------------------------------------------------------
| /Users/juneleo/demo/CardControl/app/build/intermediates/compile_and_runtime_not_namespaced_r_class_jar/debug/R.jar
| /Users/juneleo/demo/CardControl/app/build/intermediates/incremental/processDebugResources
| /Users/juneleo/demo/CardControl/app/build/intermediates/processed_res/debug/out
| /Users/juneleo/demo/CardControl/app/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt
| /Users/juneleo/demo/CardControl/app/build/intermediates/runtime_symbol_list/debug/R.txt
|-----------------------------------------------------------------

Task

com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask

Intput

  • CardControl/app/build/intermediates/aar_metadata_check/debug
  • CardControl/poseidon/build/intermediates/compiled_local_resources/debug/out

poseidon项目aapt2 生成的flat资源

  • .gradle/caches/transforms-2/files-2.1/0440b26303762a0d09f988d28effd92e/android.support.v7.recyclerview
  • .gradle/caches/transforms-2/files-2.1/b9be6620532dc927d1cf2fc250571de2/android.support.design-r.txt

R文件中的资源Name,不包含资源id,类似package-aware-r.txt

  • CardControl/poseidon/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt

module项目生成的R.txt,包含资源Name,不包含资源id

  • CardControl/app/build/intermediates/res/merged/debug

merge之后的flat资源 MergeResources 生成的资源

  • CardControl/app/build/intermediates/packaged_manifests/debug

当前项目的manifest资源

  • CardControl/app/build/intermediates/merged_manifests/debug

merge之后的manifest资源

  • Android/sdk/platforms/android-26/android.jar

android sdk

Output

  • app/build/intermediates/compile_and_runtime_not_namespaced_r_class_jar/debug/R.jar

R文件的class资源

  • app/build/intermediates/incremental/processDebugResources

  • app/build/intermediates/processed_res/debug/out

.ap_文件, 作为packageDebug的input

  • app/build/intermediates/symbol_list_with_package_name/debug/package-aware-r.txt

和上面input中输入的文件一样

  • app/build/intermediates/runtime_symbol_list/debug/R.txt

包含 资源id的R文件

3.2.1版本 子项目的编译流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|------------------------------------------------------------------------
| generateDebugRFile , poseidon|
|------------------------------------------------------------------------
| com.android.build.gradle.internal.res.GenerateLibraryRFileTask_Decorated
|------------------------------------------------------------------------
| inputs file|
|-----------------------------------------------------------------
| /Users/juneleo/.gradle/caches/transforms-1/files-1.1/dab216d02caaf8b87f89dfa48ec24291/9d7210b16e6e5d1c07ca65c0dd771908/package-aware-r.txt
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/packaged_res/debug
| /Users/juneleo/.gradle/caches/transforms-1/files-1.1/android.jar/1369dac42fdce9bd069fe1c95652d143/R.txt
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/merged_manifests/debug/processDebugManifest/merged
|-----------------------------------------------------------------
| outputs file|
|-----------------------------------------------------------------
| /Users/juneleo/demo/CardControl/poseidon/build/generated/not_namespaced_r_class_sources/debug/generateDebugRFile/out
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/symbols/debug/R.txt
|-----------------------------------------------------------------

Task

com.android.build.gradle.internal.res.GenerateLibraryRFileTask

3.0.1版本 主项目的编译流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|------------------------------------------------------------------------
| processDebugResources , app|
|------------------------------------------------------------------------
| com.android.build.gradle.tasks.ProcessAndroidResources_Decorated
|------------------------------------------------------------------------
| inputs file|
|-----------------------------------------------------------------
| /Users/juneleo/demo/CardControl/app/build/intermediates/res/merged/debug
| /Users/juneleo/demo/CardControl/app/build/intermediates/manifests/full/debug
| /Users/juneleo/demo/CardControl/app/build/intermediates/splits-support/debug/split-list.gson
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/.gradle/caches/transforms-1/files-1.1/a0a34a73130b18f42cc41c8c497f3cc9/5c70425c182376359d3bb89e62192a04/package-aware-r.txt
| /Users/juneleo/.gradle/caches/transforms-1/files-1.1/693f279b1131862cdb8570ce26694084/068459beaea3a39d6e58ea1f7a070acb/package-aware-r.txt
| /Users/juneleo/demo/CardControl/card/cardadapter/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
|-----------------------------------------------------------------
| outputs file|
|-----------------------------------------------------------------
| /Users/juneleo/demo/CardControl/app/build/intermediates/incremental/processDebugResources
| /Users/juneleo/demo/CardControl/app/build/intermediates/res/debug
| /Users/juneleo/demo/CardControl/app/build/generated/source/r/debug
| /Users/juneleo/demo/CardControl/app/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/demo/CardControl/app/build/intermediates/symbols/debug/R.txt
|-----------------------------------------------------------------

Task

com.android.build.gradle.tasks.ProcessAndroidResources

3.0.1版本 子项目的编译流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|------------------------------------------------------------------------
| processDebugResources , poseidon|
|------------------------------------------------------------------------
| com.android.build.gradle.tasks.ProcessAndroidResources_Decorated
|------------------------------------------------------------------------
| inputs file|
|-----------------------------------------------------------------
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/bundles/debug/res
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/manifests/aapt/debug
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/attr/R.txt
| /Users/juneleo/.gradle/caches/transforms-1/files-1.1/f1a2d5111196fc2cf84b3f9e1edaf926/c58ce5742a53d2365a538840d4b5051f/package-aware-r.txt
|-----------------------------------------------------------------
| outputs file|
|-----------------------------------------------------------------
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/incremental/processDebugResources
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/res/debug
| /Users/juneleo/demo/CardControl/poseidon/build/generated/source/r/debug
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/demo/CardControl/poseidon/build/intermediates/bundles/debug/R.txt
|-----------------------------------------------------------------

task

com.android.build.gradle.tasks.ProcessAndroidResources

nebulaintegration-mpaas编译流程

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
|------------------------------------------------------------------------
| processDebugResources , nebulaintegration-build|
|------------------------------------------------------------------------
| com.alipay.android.tasks.ProcessAndroidResourcesExt_Decorated
|------------------------------------------------------------------------
| inputs file|
|-----------------------------------------------------------------
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/intermediates/res/merged/debug
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/intermediates/manifests/full/debug
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/intermediates/splits-support/debug/split-list.gson
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/testlibrary/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/resource-adapter/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/resource-core/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/api/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/multimedia-adapter/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/internal/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/resource-api/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
|-----------------------------------------------------------------
| outputs file|
|-----------------------------------------------------------------
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/intermediates/incremental/processDebugResources
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/intermediates/res/debug
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/generated/source/r/debug
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/build/build/intermediates/symbols/debug/R.txt
|-----------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|------------------------------------------------------------------------
| processDebugResources , mpaas|
|------------------------------------------------------------------------
| com.alipay.android.tasks.ProcessAndroidResourcesExt_Decorated
|------------------------------------------------------------------------
| inputs file|
|-----------------------------------------------------------------
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/bundles/debug/res
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/manifests/aapt/debug
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/attr/R.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/resource-adapter/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/resource-core/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/api/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/internal/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/resource-api/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
|-----------------------------------------------------------------
| outputs file|
|-----------------------------------------------------------------
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/incremental/processDebugResources
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/res/debug
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/generated/source/r/debug
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/res/symbol-table-with-package/debug/package-aware-r.txt
| /Users/juneleo/amap/amap_nebula/android-nebulaintegration/mpaas/build/intermediates/bundles/debug/R.txt
|-----------------------------------------------------------------

Task

com.alipay.android.tasks.ProcessAndroidResourcesExt
从上面可以看出,mpaas项目的processDebugResources任务实现被重写了,但是inputs和outputs貌似没有变化,不排除没有指定inputs和outputs的情况,有很大的可能进行了也生成了新的文件,但是没有指定为outputs。

分析

1.ProcessAndroidResourcesExt如何被替换?
在配置阶段执行完成之后(afterEvaluate),ReplaceProcessResourceTask 将ProcessAndroidResources替换为ProcessAndroidResourcesExt

2.ProcessAndroidResourcesExt做了什么?

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
public void newDoFullTaskAction() throws IOException, ExecutionException {
AaptInvoker aaptInvoker = aaptParam -> {
List<String> params = aaptParam.getAdditionalParameters();
List<String> origin = getAaptOptions().getAdditionalParameters();
if (origin == null) {
origin = new ArrayList<>();
}
addMoreArgs(origin); //未看出做了什么
addIgnoreAssets(origin);
for (String p : params) {
origin.add(p);
}
getAaptOptions().setAdditionalParameters(origin);
File inputResDir = aaptParam.getInputResDir();
if (inputResDir != null) {
this.inputResourcesDir = new SimpleFileCollection(new File[]{inputResDir});
}
superNewDoFullTaskAction();
AaptResult aaptResult = new AaptResult();
aaptResult.setSrcOutputDir(getSourceOutputDir());
aaptResult.setSymbolsWithPackageNameOutputFile(getSymbolslWithPackageNameOutputFile());
aaptResult.setTextSymbolOutputFile(getTextSymbolOutputFile());
aaptResult.setPackageOutput(findApkOutputs().get(0));
return aaptResult;
};
if (getProject().getPlugins().hasPlugin(PortalPlugin.class)) {
AlipayResGod.doPortalAapt(getProject(), aaptInvoker, getManifestFile(), getPackageForR(), getSourceOutputDir(), PortalHelpGroovyUtils.portalData((Task) this).getBaseResJar());
} else if (getProject().getPlugins().hasPlugin(BundlePlugin.class)) {
AlipayResGod.doBundleAapt(getProject(), aaptInvoker, BundleProperty.getInstance().packageId);
} else {
superNewDoFullTaskAction();
}
}
  • addMoreArgs 未看出做了什么,可能反编译后代码不全,大概在Plugin为BundlePlugin时给aapt命令指定一些args
  • addIgnoreAssets 指定了默认的ignoreAssets,如果外部通过aaptOption设置则覆盖默认
  • 指定aapt的输入输出目录等
  • 判定Plugin是否使用自定义的Task
    • PortalPlugin 使用
    • BundlePlugin 使用
    • 其他Plugin不使用
  • AlipayResGod 修改资源

结论

ProcessAndroidResourcesExt 会修改资源id ,将正常的0x7f 修改 为0x02-0x7f,前提是application项目
原理可以看https://github.com/seiginonakama/ResPkgRemaker
主要修改了R.java和ap_文件
R.java

1
2
3
4
5
6
7
8
9
10
11
12
13
public final class R {
public static final class anim {
public static final int ariver_fragment_translate_in_left=0x4c440000;
public static final int ariver_fragment_translate_in_right=0x4c440001;
public static final int ariver_fragment_translate_out_left=0x4c440002;
public static final int ariver_fragment_translate_out_right=0x4c440003;
public static final int tiny_menu_in=0x4c440004;
public static final int tiny_menu_in_landscape=0x4c440005;
public static final int tiny_menu_out=0x4c440006;
public static final int tiny_menu_out_landscape=0x4c440007;
}
...
}

_ap文件
1.resources.arsc
修改资源id
2.xml文件中的资源id
eg:layout中的string引用等

build.gradle 配置

1
2
3
4
5
6
bundle {
exportPackages ''
initLevel 10
packageName 'com.alipay.mobile.nebulaintegration'
packageId 278 //0x4f
}

nebulaintegration-build 中直接运行assembleDebug ,资源id会被替换为0x4f