Skip to content

Commit 65b74f8

Browse files
Edson Moraes MenegattiDagger Team
authored andcommitted
Skip view injection when in edit mode
Closes #4305 RELNOTES=Skip view injection when in edit mode for previews. PiperOrigin-RevId: 642038084
1 parent 8b3b370 commit 65b74f8

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

java/dagger/hilt/android/processor/internal/androidentrypoint/ViewGenerator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public void generate() {
8989
XFiler.Mode.Isolating);
9090
}
9191

92-
9392
/**
9493
* Returns a pass-through constructor matching the base class's provided constructorElement. The
9594
* generated constructor simply calls super(), then inject().
@@ -99,7 +98,9 @@ public void generate() {
9998
* <pre>
10099
* Hilt_$CLASS(Context context, ...) {
101100
* super(context, ...);
102-
* inject();
101+
* if (!isInEditMode()) {
102+
* inject();
103+
* }
103104
* }
104105
* </pre>
105106
*/
@@ -117,7 +118,9 @@ private MethodSpec constructorMethod(XConstructorElement constructor) {
117118
AnnotationSpec.builder(AndroidClassNames.TARGET_API).addMember("value", "21").build());
118119
}
119120

120-
builder.addStatement("inject()");
121+
builder.beginControlFlow("if(!isInEditMode())")
122+
.addStatement("inject()")
123+
.endControlFlow();
121124

122125
return builder.build();
123126
}

javatests/dagger/hilt/android/processor/internal/GeneratorsTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,18 @@ public void copyConstructorParametersConvertsAndroidInternalNullableToExternal()
123123
JOINER.join(
124124
" Hilt_MyView(Context p0, @Nullable AttributeSet p1) {",
125125
" super(p0, p1);",
126-
" inject();",
126+
" if(!isInEditMode()) {",
127+
" inject();",
128+
" }",
127129
" }"));
128130
} else {
129131
stringSubject.contains(
130132
JOINER.join(
131133
" Hilt_MyView(Context context, @Nullable AttributeSet attrs) {",
132134
" super(context, attrs);",
133-
" inject();",
135+
" if(!isInEditMode()) {",
136+
" inject();",
137+
" }",
134138
" }"));
135139
}
136140
});

0 commit comments

Comments
 (0)