1 package com.simpligility.maven.plugins.android.common;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.maven.plugin.logging.Log;
5
6 import java.io.File;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11
12
13
14
15
16
17
18
19 public class AaptCommandBuilder
20 {
21 protected final List<String> commands;
22 protected final Log log;
23
24 protected AaptCommandBuilder( Log log )
25 {
26 this.log = log;
27 this.commands = new ArrayList<String>();
28 }
29
30
31
32
33
34
35 public static AaptPackageCommandBuilder packageResources( Log log )
36 {
37 return new AaptPackageCommandBuilder( log );
38 }
39
40
41
42
43
44
45 public static final AaptDumpCommandBuilder dump( Log log )
46 {
47 return new AaptDumpCommandBuilder( log );
48 }
49
50
51
52
53 public static final class AaptPackageCommandBuilder extends AaptCommandBuilder
54 {
55 public AaptPackageCommandBuilder( Log log )
56 {
57 super( log );
58 commands.add( "package" );
59 }
60
61
62
63
64
65
66
67
68
69
70 public AaptPackageCommandBuilder makeResourcesNonConstant()
71 {
72 return makeResourcesNonConstant( true );
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public AaptPackageCommandBuilder makeResourcesNonConstant( boolean make )
86 {
87 if ( make )
88 {
89 log.debug( "Adding non-constant-id" );
90 commands.add( "--non-constant-id" );
91 }
92 return this;
93 }
94
95
96
97
98
99
100 public AaptPackageCommandBuilder makePackageDirectories()
101 {
102 commands.add( "-m" );
103 return this;
104 }
105
106
107
108
109
110
111
112 public AaptPackageCommandBuilder setResourceConstantsFolder( File path )
113 {
114 commands.add( "-J" );
115 commands.add( path.getAbsolutePath() );
116 return this;
117 }
118
119
120
121
122
123
124
125 public AaptPackageCommandBuilder generateRIntoPackage( String packageName )
126 {
127 if ( StringUtils.isNotBlank( packageName ) )
128 {
129 commands.add( "--custom-package" );
130 commands.add( packageName );
131 }
132 return this;
133 }
134
135
136
137
138
139
140
141 public AaptPackageCommandBuilder setPathToAndroidManifest( File path )
142 {
143 commands.add( "-M" );
144 commands.add( path.getAbsolutePath() );
145 return this;
146 }
147
148
149
150
151
152
153
154
155
156 public AaptPackageCommandBuilder addResourceDirectoryIfExists( File resourceDirectory )
157 {
158 if ( resourceDirectory != null && resourceDirectory.exists() )
159 {
160 commands.add( "-S" );
161 commands.add( resourceDirectory.getAbsolutePath() );
162 }
163 return this;
164 }
165
166
167
168
169
170
171
172
173
174 public AaptPackageCommandBuilder addResourceDirectoriesIfExists( List<File> resourceDirectories )
175 {
176 if ( resourceDirectories != null )
177 {
178 for ( File resourceDirectory : resourceDirectories )
179 {
180 addResourceDirectoryIfExists( resourceDirectory );
181 }
182 }
183 return this;
184 }
185
186
187
188
189
190
191
192
193
194 public AaptPackageCommandBuilder addResourceDirectoriesIfExists( File[] resourceDirectories )
195 {
196 if ( resourceDirectories != null )
197 {
198 for ( File resourceDirectory : resourceDirectories )
199 {
200 addResourceDirectoryIfExists( resourceDirectory );
201 }
202 }
203 return this;
204 }
205
206
207
208
209
210
211 public AaptPackageCommandBuilder autoAddOverlay()
212 {
213 commands.add( "--auto-add-overlay" );
214 return this;
215 }
216
217
218
219
220
221
222
223 public AaptPackageCommandBuilder addRawAssetsDirectoryIfExists( File assetsFolder )
224 {
225 if ( assetsFolder != null && assetsFolder.exists() )
226 {
227 log.debug( "Adding assets folder : " + assetsFolder );
228 commands.add( "-A" );
229 commands.add( assetsFolder.getAbsolutePath() );
230 }
231 return this;
232 }
233
234
235
236
237
238
239
240 public AaptPackageCommandBuilder addExistingPackageToBaseIncludeSet( File path )
241 {
242 commands.add( "-I" );
243 commands.add( path.getAbsolutePath() );
244 return this;
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 public AaptPackageCommandBuilder addConfigurations( String configurations )
270 {
271 if ( StringUtils.isNotBlank( configurations ) )
272 {
273 commands.add( "-c" );
274 commands.add( configurations );
275 }
276 return this;
277 }
278
279
280
281
282
283
284
285
286 public AaptPackageCommandBuilder addExtraArguments( String[] extraArguments )
287 {
288 if ( extraArguments != null )
289 {
290 commands.addAll( Arrays.asList( extraArguments ) );
291 }
292 return this;
293 }
294
295
296
297
298
299
300
301 public AaptPackageCommandBuilder setVerbose( boolean isVerbose )
302 {
303 if ( isVerbose )
304 {
305 commands.add( "-v" );
306 }
307 return this;
308 }
309
310
311
312
313
314
315
316
317 public AaptPackageCommandBuilder generateRTextFile( File folderForR )
318 {
319 commands.add( "--output-text-symbols" );
320 commands.add( folderForR.getAbsolutePath() );
321 return this;
322 }
323
324
325
326
327
328
329 public AaptPackageCommandBuilder forceOverwriteExistingFiles()
330 {
331 commands.add( "-f" );
332 return this;
333 }
334
335
336
337
338
339
340 public AaptPackageCommandBuilder disablePngCrunching()
341 {
342 commands.add( "--no-crunch" );
343 return this;
344 }
345
346
347
348
349
350
351 public AaptPackageCommandBuilder setOutputApkFile( File outputFile )
352 {
353 commands.add( "-F" );
354 commands.add( outputFile.getAbsolutePath() );
355 return this;
356 }
357
358
359
360
361
362
363 public AaptPackageCommandBuilder setProguardOptionsOutputFile( File outputFile )
364 {
365 if ( outputFile != null )
366 {
367 final File parentFolder = outputFile.getParentFile();
368 if ( parentFolder != null )
369 {
370 parentFolder.mkdirs();
371 }
372 log.debug( "Adding proguard file : " + outputFile );
373 commands.add( "-G" );
374 commands.add( outputFile.getAbsolutePath() );
375 }
376 return this;
377 }
378
379
380
381
382
383
384
385
386
387 public AaptPackageCommandBuilder renameManifestPackage( String manifestPackage )
388 {
389 if ( StringUtils.isNotBlank( manifestPackage ) )
390 {
391 commands.add( "--rename-manifest-package" );
392 commands.add( manifestPackage );
393 }
394 return this;
395 }
396
397
398
399
400
401
402
403
404
405 public AaptPackageCommandBuilder renameInstrumentationTargetPackage( String instrumentationPackage )
406 {
407 if ( StringUtils.isNotBlank( instrumentationPackage ) )
408 {
409 commands.add( "--rename-instrumentation-target-package" );
410 commands.add( instrumentationPackage );
411 }
412 return this;
413 }
414
415
416
417
418
419
420
421 public AaptPackageCommandBuilder setDebugMode( boolean isDebugMode )
422 {
423 if ( isDebugMode )
424 {
425 log.info( "Generating debug apk." );
426 commands.add( "--debug-mode" );
427 }
428 else
429 {
430 log.info( "Generating release apk." );
431 }
432 return this;
433 }
434 }
435
436
437
438
439 public static final class AaptDumpCommandBuilder extends AaptCommandBuilder
440 {
441 public AaptDumpCommandBuilder( Log log )
442 {
443 super( log );
444 commands.add( "dump" );
445 }
446
447
448
449
450
451 public AaptDumpCommandBuilder xmlTree()
452 {
453 commands.add( "xmltree" );
454 return this;
455 }
456
457
458
459
460
461
462
463 public AaptDumpCommandBuilder setPathToApk( String pathToApk )
464 {
465 commands.add( pathToApk );
466 return this;
467 }
468
469
470
471
472
473
474 public AaptDumpCommandBuilder addAssetFile( String assetFile )
475 {
476 commands.add( assetFile );
477 return this;
478 }
479 }
480
481 @Override
482 public String toString()
483 {
484 return commands.toString();
485 }
486
487
488
489
490
491
492 public List<String> build()
493 {
494 return Collections.unmodifiableList( commands );
495 }
496 }