Skip to content

Commit 94118a2

Browse files
bcorsoDagger Team
authored andcommitted
Shade androidx.room.compiler dependencies in Dagger.
This CL also adds a python script that will validate each jar deployed through deploy-library.sh to prevent unexpected classes from leaking into our deployed jars in the future. RELNOTES=N/A PiperOrigin-RevId: 488388271
1 parent 633059f commit 94118a2

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

util/deploy-dagger.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ _deploy \
5454
# This artifact uses the shaded classes from dagger-spi, so we use the same
5555
# shading rules so that our references to those classes are shaded the same way.
5656
_deploy \
57-
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler.processing,dagger.spi.shaded.androidx.room.compiler.processing" \
57+
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler,dagger.spi.shaded.androidx.room.compiler" \
5858
java/dagger/internal/codegen/artifact.jar \
5959
java/dagger/internal/codegen/pom.xml \
6060
java/dagger/internal/codegen/artifact-src.jar \
@@ -70,7 +70,7 @@ _deploy \
7070
""
7171

7272
_deploy \
73-
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler.processing,dagger.spi.shaded.androidx.room.compiler.processing" \
73+
"com.google.auto.common,dagger.spi.shaded.auto.common;androidx.room.compiler,dagger.spi.shaded.androidx.room.compiler" \
7474
java/dagger/spi/artifact.jar \
7575
java/dagger/spi/pom.xml \
7676
java/dagger/spi/artifact-src.jar \

util/deploy-library.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ deploy_library() {
3333
library="${library%.*}-shaded.${library##*.}"
3434
fi
3535

36+
# Validate that the classes in the library jar begin with expected prefixes.
37+
validate_jar $(bazel_output_file $library)
38+
3639
# TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro, this
3740
# requires having the version checked-in for the build system.
3841
add_tracking_version \
@@ -98,6 +101,20 @@ add_tracking_version() {
98101
fi
99102
}
100103

104+
validate_jar() {
105+
local library=$1
106+
if [[ $library == */gwt/libgwt.jar ]]; then
107+
python $(dirname $0)/validate-jar-entry-prefixes.py \
108+
$library "dagger/,META-INF/,javax/inject/"
109+
elif [[ $library == */java/dagger/hilt/android/artifact.aar ]]; then
110+
python $(dirname $0)/validate-jar-entry-prefixes.py \
111+
$library "dagger/,META-INF/,hilt_aggregated_deps/"
112+
else
113+
python $(dirname $0)/validate-jar-entry-prefixes.py \
114+
$library "dagger/,META-INF/"
115+
fi
116+
}
117+
101118
add_automatic_module_name_manifest_entry() {
102119
local library=$1
103120
local module_name=$2

util/validate-jar-entry-prefixes.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Validates classes in the deployed jar are all within the expected packages.
2+
3+
Usage:
4+
python validate-jar-entry-prefixes.py <jar-file> <comma-separated-prefixes>
5+
"""
6+
import re
7+
import shutil
8+
import sys
9+
import tempfile
10+
import zipfile
11+
12+
13+
def main(argv):
14+
if len(argv) > 3:
15+
raise Exception('Expected only two arguments but got {0}'.format(len(argv)))
16+
17+
jar_file, prefixes = argv[-2:]
18+
prefixes_pattern = re.compile('|'.join(prefixes.split(',')))
19+
20+
invalid_entries = []
21+
if jar_file.endswith('.jar'):
22+
invalid_entries = _invalid_entries(jar_file, prefixes_pattern)
23+
elif jar_file.endswith('.aar'):
24+
dirpath = tempfile.mkdtemp()
25+
with zipfile.ZipFile(jar_file, 'r') as zip_file:
26+
class_file = zip_file.extract('classes.jar', dirpath)
27+
invalid_entries = _invalid_entries(class_file, prefixes_pattern)
28+
shutil.rmtree(dirpath)
29+
else:
30+
raise Exception('Invalid jar file: {0}'.format(jar_file))
31+
32+
if invalid_entries:
33+
raise Exception(
34+
'Found invalid entries in {0} that do not match one of the allowed prefixes ({1}):\n {2}'
35+
.format(
36+
jar_file,
37+
', '.join(['"{0}"'.format(p) for p in prefixes.split(',')]),
38+
'\n '.join(invalid_entries))
39+
)
40+
41+
42+
def _invalid_entries(jar_file, prefixes_pattern):
43+
invalid_entries = []
44+
with zipfile.ZipFile(jar_file, 'r') as zip_file:
45+
for info in zip_file.infolist():
46+
if not info.is_dir():
47+
if not prefixes_pattern.match(info.filename):
48+
invalid_entries.append(info.filename)
49+
return invalid_entries
50+
51+
52+
if __name__ == '__main__':
53+
main(sys.argv)

0 commit comments

Comments
 (0)