37
37
38
38
String DEFAULT_MANIFEST_NAME = "AndroidManifest.xml" ;
39
39
Class <? extends Application > DEFAULT_APPLICATION = DefaultApplication .class ;
40
+ String DEFAULT_PACKAGE_NAME = "" ;
40
41
String DEFAULT_QUALIFIERS = "" ;
41
42
String DEFAULT_RES_FOLDER = "res" ;
42
43
String DEFAULT_ASSET_FOLDER = "assets" ;
87
88
Class <? extends Application > application () default
88
89
DefaultApplication .class ; // DEFAULT_APPLICATION
89
90
91
+ /**
92
+ * Java package name where the "R.class" file is located. This only needs to be specified if you
93
+ * define an {@code applicationId} associated with {@code productFlavors} or specify {@code
94
+ * applicationIdSuffix} in your build.gradle.
95
+ *
96
+ * <p>If not specified, Robolectric defaults to the {@code applicationId}.
97
+ *
98
+ * @return The java package name for R.class.
99
+ * @deprecated To change your package name please override the applicationId in your build system.
100
+ * Changing package name here is broken as the package name will no longer match the package
101
+ * name encoded in the arsc resources file. If you are looking to simulate another application
102
+ * you can create another applications Context using {@link
103
+ * android.content.Context#createPackageContext(String, int)}. Note that you must add this
104
+ * package to {@link
105
+ * org.robolectric.shadows.ShadowPackageManager#addPackage(android.content.pm.PackageInfo)}
106
+ * first.
107
+ */
108
+ @ Deprecated
109
+ String packageName () default DEFAULT_PACKAGE_NAME ;
110
+
90
111
/**
91
112
* Qualifiers specifying device configuration for this test, such as "fr-normal-port-hdpi".
92
113
*
@@ -158,6 +179,7 @@ class Implementation implements Config {
158
179
private final String qualifiers ;
159
180
private final String resourceDir ;
160
181
private final String assetDir ;
182
+ private final String packageName ;
161
183
private final Class <?>[] shadows ;
162
184
private final String [] instrumentedPackages ;
163
185
private final Class <? extends Application > application ;
@@ -172,6 +194,7 @@ public static Config fromProperties(Properties properties) {
172
194
properties .getProperty ("manifest" , DEFAULT_VALUE_STRING ),
173
195
properties .getProperty ("qualifiers" , DEFAULT_QUALIFIERS ),
174
196
Float .parseFloat (properties .getProperty ("fontScale" , "1.0f" )),
197
+ properties .getProperty ("packageName" , DEFAULT_PACKAGE_NAME ),
175
198
properties .getProperty ("resourceDir" , DEFAULT_RES_FOLDER ),
176
199
properties .getProperty ("assetDir" , DEFAULT_ASSET_FOLDER ),
177
200
parseClasses (properties .getProperty ("shadows" , "" )),
@@ -272,6 +295,7 @@ public Implementation(
272
295
String manifest ,
273
296
String qualifiers ,
274
297
float fontScale ,
298
+ String packageName ,
275
299
String resourceDir ,
276
300
String assetDir ,
277
301
Class <?>[] shadows ,
@@ -284,6 +308,7 @@ public Implementation(
284
308
this .manifest = manifest ;
285
309
this .qualifiers = qualifiers ;
286
310
this .fontScale = fontScale ;
311
+ this .packageName = packageName ;
287
312
this .resourceDir = resourceDir ;
288
313
this .assetDir = assetDir ;
289
314
this .shadows = shadows ;
@@ -329,6 +354,11 @@ public String qualifiers() {
329
354
return qualifiers ;
330
355
}
331
356
357
+ @ Override
358
+ public String packageName () {
359
+ return packageName ;
360
+ }
361
+
332
362
@ Override
333
363
public String resourceDir () {
334
364
return resourceDir ;
@@ -381,6 +411,9 @@ public String toString() {
381
411
+ ", assetDir='"
382
412
+ assetDir
383
413
+ '\''
414
+ + ", packageName='"
415
+ + packageName
416
+ + '\''
384
417
+ ", shadows="
385
418
+ Arrays .toString (shadows )
386
419
+ ", instrumentedPackages="
@@ -400,6 +433,7 @@ class Builder {
400
433
protected float fontScale = 1.0f ;
401
434
protected String manifest = Config .DEFAULT_VALUE_STRING ;
402
435
protected String qualifiers = Config .DEFAULT_QUALIFIERS ;
436
+ protected String packageName = Config .DEFAULT_PACKAGE_NAME ;
403
437
protected String resourceDir = Config .DEFAULT_RES_FOLDER ;
404
438
protected String assetDir = Config .DEFAULT_ASSET_FOLDER ;
405
439
protected Class <?>[] shadows = new Class [0 ];
@@ -416,6 +450,7 @@ public Builder(Config config) {
416
450
manifest = config .manifest ();
417
451
qualifiers = config .qualifiers ();
418
452
fontScale = config .fontScale ();
453
+ packageName = config .packageName ();
419
454
resourceDir = config .resourceDir ();
420
455
assetDir = config .assetDir ();
421
456
shadows = config .shadows ();
@@ -449,6 +484,11 @@ public Builder setQualifiers(String qualifiers) {
449
484
return this ;
450
485
}
451
486
487
+ public Builder setPackageName (String packageName ) {
488
+ this .packageName = packageName ;
489
+ return this ;
490
+ }
491
+
452
492
public Builder setResourceDir (String resourceDir ) {
453
493
this .resourceDir = resourceDir ;
454
494
return this ;
@@ -528,6 +568,7 @@ public Builder overlay(Config overlayConfig) {
528
568
}
529
569
}
530
570
571
+ this .packageName = pick (this .packageName , overlayConfig .packageName (), "" );
531
572
this .resourceDir =
532
573
pick (this .resourceDir , overlayConfig .resourceDir (), Config .DEFAULT_RES_FOLDER );
533
574
this .assetDir = pick (this .assetDir , overlayConfig .assetDir (), Config .DEFAULT_ASSET_FOLDER );
@@ -568,6 +609,7 @@ public Implementation build() {
568
609
manifest ,
569
610
qualifiers ,
570
611
fontScale ,
612
+ packageName ,
571
613
resourceDir ,
572
614
assetDir ,
573
615
shadows ,
0 commit comments