Skip to content

Commit 5534da2

Browse files
committed
Merge proguard mapping files
1 parent b5d5811 commit 5534da2

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

examples/bundle/app/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ android_application(
88
},
99
feature_modules = ["//features/assets:feature_module"],
1010
manifest = "AndroidManifest.xml",
11+
proguard_specs = ["proguard-rules.pro"],
1112
deps = [":lib"],
1213
)
1314

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Proguard rules for base module
2+
-keepattributes SourceFile,LineNumberTable
3+
4+
# Ignore missing androidx annotations (they are compile-time only)
5+
-dontwarn androidx.annotation.**
6+
-dontwarn com.google.android.play.core.**

rules/android_application/android_application_rule.bzl

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ load(
5555
load(
5656
"//rules:utils.bzl",
5757
"ANDROID_SDK_TOOLCHAIN_TYPE",
58+
"ANDROID_TOOLCHAIN_TYPE",
5859
"get_android_sdk",
5960
"get_android_toolchain",
6061
_log = "log",
6162
)
63+
load("//rules:proguard.bzl", "proguard")
6264
load("//rules:visibility.bzl", "PROJECT_VISIBILITY")
6365
load("@rules_java//java/common:java_common.bzl", "java_common")
6466
load(":android_feature_module_rule.bzl", "get_feature_module_paths")
@@ -347,8 +349,14 @@ def _impl(ctx):
347349
get_android_toolchain(ctx).bundletool_module_builder.files_to_run,
348350
)
349351

352+
feature_module_proguard_mappings = []
353+
350354
# Convert each feature to module zip.
351355
for feature in ctx.attr.feature_modules:
356+
# Collect proguard mapping files from feature modules
357+
if ProguardMappingInfo in feature[AndroidFeatureModuleInfo].binary:
358+
feature_module_proguard_mappings.append(feature[AndroidFeatureModuleInfo].binary[ProguardMappingInfo].proguard_mapping)
359+
352360
proto_apk = ctx.actions.declare_file(
353361
"%s.proto-ap_" % feature.label.name,
354362
sibling = base_proto_apk,
@@ -375,9 +383,6 @@ def _impl(ctx):
375383
)
376384

377385
metadata = dict()
378-
if ProguardMappingInfo in ctx.attr.base_module:
379-
metadata["com.android.tools.build.obfuscation/proguard.map"] = ctx.attr.base_module[ProguardMappingInfo].proguard_mapping
380-
381386
if ctx.file.device_group_config:
382387
metadata["com.android.tools.build.bundletool/DeviceGroupConfig.json"] = ctx.file.device_group_config
383388

@@ -392,6 +397,29 @@ def _impl(ctx):
392397
metadata["com.android.tools.build.profiles/baseline.prof"] = base_art_profile_info.baseline_profile
393398
metadata["com.android.tools.build.profiles/baseline.profm"] = base_art_profile_info.baseline_profile_metadata
394399

400+
# Collect all proguard mapping files (base + feature modules)
401+
proguard_maps_to_merge = list(feature_module_proguard_mappings)
402+
if ProguardMappingInfo in ctx.attr.base_module:
403+
proguard_maps_to_merge.append(ctx.attr.base_module[ProguardMappingInfo].proguard_mapping)
404+
405+
if proguard_maps_to_merge:
406+
if len(proguard_maps_to_merge) == 1:
407+
# Only one mapping, no need to merge
408+
metadata["com.android.tools.build.obfuscation/proguard.map"] = proguard_maps_to_merge[0]
409+
else:
410+
# Multiple mappings, merge them
411+
merged_proguard_mapping = ctx.actions.declare_file(ctx.label.name + "_merged_proguard.map")
412+
413+
# TODO: this isn't working, because `proguard_maps_merger` doesn't exist in the toolchain.
414+
proguard.merge_proguard_maps(
415+
ctx,
416+
output = merged_proguard_mapping,
417+
inputs = proguard_maps_to_merge,
418+
proguard_maps_merger = get_android_toolchain(ctx).proguard_maps_merger.files_to_run,
419+
toolchain_type = ANDROID_TOOLCHAIN_TYPE,
420+
)
421+
metadata["com.android.tools.build.obfuscation/proguard.map"] = merged_proguard_mapping
422+
395423
# Create .aab
396424
_bundletool.build(
397425
ctx,

rules/android_binary/r8.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""R8 processor steps for android_binary."""
1515

16-
load("//providers:providers.bzl", "AndroidDexInfo", "AndroidPreDexJarInfo")
16+
load("//providers:providers.bzl", "AndroidDexInfo", "AndroidPreDexJarInfo", "ProguardMappingInfo")
1717
load("//rules:acls.bzl", "acls")
1818
load("//rules:android_neverlink_aspect.bzl", "StarlarkAndroidNeverlinkInfo")
1919
load("//rules:common.bzl", "common")
@@ -121,9 +121,11 @@ def process_r8(ctx, validation_ctx, jvm_ctx, packaged_resources_ctx, build_info_
121121
value = struct(
122122
final_classes_dex_zip = dexes_zip,
123123
dex_info = android_dex_info,
124+
proguard_output_map = proguard_mappings_output_file,
124125
providers = [
125126
android_dex_info,
126127
AndroidPreDexJarInfo(pre_dex_jar = deploy_jar),
128+
ProguardMappingInfo(proguard_mapping = proguard_mappings_output_file),
127129
],
128130
),
129131
)

0 commit comments

Comments
 (0)