1 package com.simpligility.maven.plugins.android.standalonemojos;
2
3 import com.android.SdkConstants;
4 import com.android.builder.core.AndroidBuilder;
5 import com.android.builder.core.ErrorReporter;
6 import com.android.builder.dependency.level2.AndroidDependency;
7 import com.android.ide.common.process.DefaultProcessExecutor;
8 import com.android.manifmerger.ManifestMerger2;
9 import com.android.utils.ILogger;
10 import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
11 import com.simpligility.maven.plugins.android.DefaultJavaProcessExecutor;
12 import com.simpligility.maven.plugins.android.MavenErrorReporter;
13 import com.simpligility.maven.plugins.android.common.AndroidExtension;
14 import com.simpligility.maven.plugins.android.configuration.ManifestMerger;
15 import com.simpligility.maven.plugins.android.configuration.UsesSdk;
16 import com.simpligility.maven.plugins.android.configuration.VersionGenerator;
17 import com.simpligility.maven.plugins.android.phase01generatesources.MavenILogger;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugins.annotations.LifecyclePhase;
24 import org.apache.maven.plugins.annotations.Mojo;
25 import org.apache.maven.plugins.annotations.Parameter;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Set;
32
33
34
35
36
37
38
39 @Mojo( name = "manifest-merger", defaultPhase = LifecyclePhase.PROCESS_RESOURCES )
40 public class ManifestMergerMojo extends AbstractAndroidMojo
41 {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 @Parameter
90 private ManifestMerger manifestMerger;
91
92
93
94
95
96
97 @Parameter( property = "android.manifestMerger.versionName", defaultValue = "${project.version}" )
98 protected String manifestVersionName;
99
100
101
102
103
104 @Parameter( property = "android.manifestMerger.versionCode", defaultValue = "1" )
105 protected Integer manifestVersionCode;
106
107
108
109
110
111
112
113
114
115
116
117
118
119 @Parameter( property = "android.manifest.versionCodeUpdateFromVersion", defaultValue = "false" )
120 protected Boolean manifestVersionCodeUpdateFromVersion = false;
121
122
123
124
125
126
127
128
129
130
131
132
133 @Parameter( property = "android.manifestMerger.versionNamingPattern" )
134 protected String manifestVersionNamingPattern;
135
136
137
138
139
140
141 @Parameter( property = "android.manifestMerger.versionDigits", defaultValue = "4,3,3" )
142 protected String manifestVersionDigits;
143
144
145
146
147
148 @Parameter( property = "android.manifestMerger.mergeLibraries", defaultValue = "false" )
149 protected Boolean manifestMergeLibraries;
150
151
152
153
154
155 @Parameter( property = "android.manifestMerger.mergeReportFile" )
156 protected File manifestMergeReportFile;
157
158
159
160
161
162 protected UsesSdk manifestUsesSdk;
163 private Boolean parsedVersionCodeUpdateFromVersion;
164 private String parsedVersionNamingPattern;
165 private String parsedVersionDigits;
166 private Boolean parsedMergeLibraries;
167 private String parsedVersionName;
168 private Integer parsedVersionCode;
169 private UsesSdk parsedUsesSdk;
170 private File parsedMergeReportFile;
171
172
173
174
175
176 public void execute() throws MojoExecutionException, MojoFailureException
177 {
178 if ( ! AndroidExtension.isAndroidPackaging( project.getPackaging() ) )
179 {
180 return;
181 }
182
183 if ( androidManifestFile == null )
184 {
185 getLog().debug( "skip, no androidmanifest.xml defined (androidManifestFile rare case)" );
186 return;
187 }
188
189 parseConfiguration();
190
191 getLog().info( "Attempting to update manifest " + androidManifestFile );
192 getLog().debug( " usesSdk=" + parsedUsesSdk );
193 getLog().debug( " versionName=" + parsedVersionName );
194 getLog().debug( " versionCode=" + parsedVersionCode );
195 getLog().debug( " usesSdk=" + parsedUsesSdk );
196 getLog().debug( " versionCodeUpdateFromVersion=" + parsedVersionCodeUpdateFromVersion );
197 getLog().debug( " versionNamingPattern=" + parsedVersionNamingPattern );
198 getLog().debug( " versionDigits=" + parsedVersionDigits );
199 getLog().debug( " mergeLibraries=" + parsedMergeLibraries );
200 getLog().debug( " mergeReportFile=" + parsedMergeReportFile );
201
202 if ( ! androidManifestFile.exists() )
203 {
204 return;
205 }
206
207 getLog().debug( "Using manifest merger V2" );
208 manifestMergerV2();
209 }
210
211 private void parseConfiguration()
212 {
213
214 if ( manifestMerger != null )
215 {
216 if ( StringUtils.isNotEmpty( manifestMerger.getVersionName() ) )
217 {
218 parsedVersionName = manifestMerger.getVersionName();
219 }
220 else
221 {
222 parsedVersionName = manifestVersionName;
223 }
224 if ( manifestMerger.getVersionCode() != null )
225 {
226 parsedVersionCode = manifestMerger.getVersionCode();
227 }
228 else
229 {
230 parsedVersionCode = manifestVersionCode;
231 }
232 if ( manifestMerger.getVersionCodeUpdateFromVersion() != null )
233 {
234 parsedVersionCodeUpdateFromVersion = manifestMerger.getVersionCodeUpdateFromVersion();
235 }
236 else
237 {
238 parsedVersionCodeUpdateFromVersion = manifestVersionCodeUpdateFromVersion;
239 }
240 if ( manifestMerger.getVersionNamingPattern() != null )
241 {
242 parsedVersionNamingPattern = manifestMerger.getVersionNamingPattern();
243 }
244 else
245 {
246 parsedVersionNamingPattern = manifestVersionNamingPattern;
247 }
248 if ( manifestMerger.getVersionDigits() != null )
249 {
250 parsedVersionDigits = manifestMerger.getVersionDigits();
251 }
252 else
253 {
254 parsedVersionDigits = manifestVersionDigits;
255 }
256 if ( manifestMerger.getUsesSdk() != null )
257 {
258 parsedUsesSdk = manifestMerger.getUsesSdk();
259 }
260 else
261 {
262 parsedUsesSdk = manifestUsesSdk;
263 }
264 if ( manifestMerger.getMergeLibraries() != null )
265 {
266 parsedMergeLibraries = manifestMerger.getMergeLibraries();
267 }
268 else
269 {
270 parsedMergeLibraries = manifestMergeLibraries;
271 }
272 if ( manifestMerger.getMergeReportFile() != null )
273 {
274 parsedMergeReportFile = manifestMerger.getMergeReportFile();
275 }
276 else
277 {
278 parsedMergeReportFile = manifestMergeReportFile;
279 }
280 }
281 else
282 {
283 parsedVersionName = manifestVersionName;
284 parsedVersionCode = manifestVersionCode;
285 parsedUsesSdk = manifestUsesSdk;
286 parsedVersionCodeUpdateFromVersion = manifestVersionCodeUpdateFromVersion;
287 parsedVersionNamingPattern = manifestVersionNamingPattern;
288 parsedVersionDigits = manifestVersionDigits;
289 parsedMergeLibraries = manifestMergeLibraries;
290 parsedMergeReportFile = manifestMergeReportFile;
291 }
292 }
293
294
295 public void manifestMergerV2() throws MojoExecutionException, MojoFailureException
296 {
297 ILogger logger = new MavenILogger( getLog(), ( parsedMergeReportFile != null ) );
298 AndroidBuilder builder = new AndroidBuilder( project.toString(), "created by Android Maven Plugin",
299 new DefaultProcessExecutor( logger ),
300 new DefaultJavaProcessExecutor( logger ),
301 new MavenErrorReporter( logger, ErrorReporter.EvaluationMode.STANDARD ),
302 logger,
303 false );
304
305 String minSdkVersion = null;
306 String targetSdkVersion = null;
307 int versionCode;
308 if ( parsedUsesSdk != null )
309 {
310 minSdkVersion = parsedUsesSdk.getMinSdkVersion();
311 targetSdkVersion = parsedUsesSdk.getTargetSdkVersion();
312 }
313 if ( parsedVersionCodeUpdateFromVersion )
314 {
315 VersionGenerator gen = new VersionGenerator( parsedVersionDigits, parsedVersionNamingPattern );
316
317 versionCode = gen.generate( parsedVersionName );
318 }
319 else
320 {
321 versionCode = parsedVersionCode;
322 }
323 List<AndroidDependency> manifestDependencies = new ArrayList<>();
324
325 if ( parsedMergeLibraries )
326 {
327 final Set<Artifact> allArtifacts = project.getDependencyArtifacts();
328 Set<Artifact> dependencyArtifacts = getArtifactResolverHelper().getFilteredArtifacts( allArtifacts );
329
330 for ( Artifact dependency : dependencyArtifacts )
331 {
332 final File unpackedLibFolder = getUnpackedLibFolder( dependency );
333 final File manifestFile = new File( unpackedLibFolder, SdkConstants.FN_ANDROID_MANIFEST_XML );
334 if ( manifestFile.exists() )
335 {
336
337
338
339
340
341
342
343
344 manifestDependencies.add( AndroidDependency.createExplodedAarLibrary(
345 manifestFile,
346 null,
347 dependency.getArtifactId(),
348 unpackedLibFolder.getPath(),
349 unpackedLibFolder ) );
350 }
351 }
352 }
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 builder.mergeManifestsForApplication(
373 androidManifestFile,
374 new ArrayList<File>(),
375 manifestDependencies,
376 "",
377 versionCode,
378 parsedVersionName,
379 minSdkVersion,
380 targetSdkVersion,
381 null,
382 destinationManifestFile.getPath(),
383 null,
384 null,
385 ManifestMerger2.MergeType.APPLICATION,
386 new HashMap<String, Object>(),
387 new ArrayList<ManifestMerger2.Invoker.Feature>(),
388 parsedMergeReportFile
389 );
390 }
391 }