Skip to content

Commit bf2f8fa

Browse files
nbradburyclaude
andcommitted
Add checkstyle rule to prevent string resource access via Application context
AGP 8.8+ generateLocaleConfig causes incorrect locale-based resource resolution when using Application context. This rule flags WordPress.getContext().getString() and directs developers to use Activity/Fragment context or injected ResourceProvider instead. Also removes the one existing usage in ReaderPostTable by hardcoding the string, since it was embedded in HTML stored in SQLite and would never update on locale change anyway. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3fc041e commit bf2f8fa

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

WordPress/src/main/java/org/wordpress/android/datasets/ReaderPostTable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import androidx.annotation.Nullable;
1212

1313
import org.greenrobot.eventbus.EventBus;
14-
import org.wordpress.android.R;
15-
import org.wordpress.android.WordPress;
1614
import org.wordpress.android.models.ReaderCardType;
1715
import org.wordpress.android.models.ReaderPost;
1816
import org.wordpress.android.models.ReaderPostList;
@@ -844,7 +842,7 @@ private static String maxText(final ReaderPost post) {
844842
return "<p>" + post.getExcerpt() + "</p>"
845843
+ String.format("<p style='text-align:center'><a href='%s'>%s</a></p>",
846844
post.getUrl(),
847-
WordPress.getContext().getString(R.string.reader_label_view_original));
845+
"View original article");
848846
} else {
849847
AppLog.w(AppLog.T.READER, "reader post table > max text exceeded, storing truncated text");
850848
return post.getText().substring(0, MAX_TEXT_LEN);

config/checkstyle.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@
119119
<property name="severity" value="error"/>
120120
</module>
121121

122+
<!-- Specific to WordPress -->
123+
<!-- WordPress.getContext() must not be used for string resource access. -->
124+
<!-- AGP 8.8+ generateLocaleConfig causes incorrect resource resolution -->
125+
<!-- with Application context. Use Activity/Fragment context, or -->
126+
<!-- injected ResourceProvider instead. -->
127+
<module name="RegexpSingleline">
128+
<property name="format"
129+
value="WordPress\.getContext\(\)\.getString\((?!.*checkstyle ignore)"/>
130+
<property name="message"
131+
value="Do not use WordPress.getContext().getString(). Use Activity/Fragment context or injected ResourceProvider instead."/>
132+
<property name="severity" value="error"/>
133+
</module>
134+
122135
<module name="LineLength">
123136
<property name="fileExtensions" value="java"/>
124137
<!-- what is a good max value? -->

0 commit comments

Comments
 (0)