@@ -673,24 +673,40 @@ static bool applyFileOverlay(Bundle *bundle,
673673 return true ;
674674}
675675
676- void addTagAttribute (const sp<XMLNode>& node, const char * ns8,
677- const char * attr8, const char * value)
676+ /*
677+ * Inserts an attribute in a given node, only if the attribute does not
678+ * exist.
679+ * If errorOnFailedInsert is true, and the attribute already exists, returns false.
680+ * Returns true otherwise, even if the attribute already exists.
681+ */
682+ bool addTagAttribute (const sp<XMLNode>& node, const char * ns8,
683+ const char * attr8, const char * value, bool errorOnFailedInsert)
678684{
679685 if (value == NULL ) {
680- return ;
686+ return true ;
681687 }
682-
688+
683689 const String16 ns (ns8);
684690 const String16 attr (attr8);
685-
691+
686692 if (node->getAttribute (ns, attr) != NULL ) {
693+ if (errorOnFailedInsert) {
694+ fprintf (stderr, " Error: AndroidManifest.xml already defines %s (in %s);"
695+ " cannot insert new value %s.\n " ,
696+ String8 (attr).string (), String8 (ns).string (), value);
697+ return false ;
698+ }
699+
687700 fprintf (stderr, " Warning: AndroidManifest.xml already defines %s (in %s);"
688701 " using existing value in manifest.\n " ,
689702 String8 (attr).string (), String8 (ns).string ());
690- return ;
703+
704+ // don't stop the build.
705+ return true ;
691706 }
692707
693708 node->addAttribute (ns, attr, String16 (value));
709+ return true ;
694710}
695711
696712static void fullyQualifyClassName (const String8& package, sp<XMLNode> node,
@@ -728,11 +744,17 @@ status_t massageManifest(Bundle* bundle, sp<XMLNode> root)
728744 fprintf (stderr, " No <manifest> tag.\n " );
729745 return UNKNOWN_ERROR;
730746 }
731-
732- addTagAttribute (root, RESOURCES_ANDROID_NAMESPACE, " versionCode" ,
733- bundle->getVersionCode ());
734- addTagAttribute (root, RESOURCES_ANDROID_NAMESPACE, " versionName" ,
735- bundle->getVersionName ());
747+
748+ bool errorOnFailedInsert = bundle->getErrorOnFailedInsert ();
749+
750+ if (!addTagAttribute (root, RESOURCES_ANDROID_NAMESPACE, " versionCode" ,
751+ bundle->getVersionCode (), errorOnFailedInsert)) {
752+ return UNKNOWN_ERROR;
753+ }
754+ if (!addTagAttribute (root, RESOURCES_ANDROID_NAMESPACE, " versionName" ,
755+ bundle->getVersionName (), errorOnFailedInsert)) {
756+ return UNKNOWN_ERROR;
757+ }
736758
737759 if (bundle->getMinSdkVersion () != NULL
738760 || bundle->getTargetSdkVersion () != NULL
@@ -743,18 +765,27 @@ status_t massageManifest(Bundle* bundle, sp<XMLNode> root)
743765 root->insertChildAt (vers, 0 );
744766 }
745767
746- addTagAttribute (vers, RESOURCES_ANDROID_NAMESPACE, " minSdkVersion" ,
747- bundle->getMinSdkVersion ());
748- addTagAttribute (vers, RESOURCES_ANDROID_NAMESPACE, " targetSdkVersion" ,
749- bundle->getTargetSdkVersion ());
750- addTagAttribute (vers, RESOURCES_ANDROID_NAMESPACE, " maxSdkVersion" ,
751- bundle->getMaxSdkVersion ());
768+ if (!addTagAttribute (vers, RESOURCES_ANDROID_NAMESPACE, " minSdkVersion" ,
769+ bundle->getMinSdkVersion (), errorOnFailedInsert)) {
770+ return UNKNOWN_ERROR;
771+ }
772+ if (!addTagAttribute (vers, RESOURCES_ANDROID_NAMESPACE, " targetSdkVersion" ,
773+ bundle->getTargetSdkVersion (), errorOnFailedInsert)) {
774+ return UNKNOWN_ERROR;
775+ }
776+ if (!addTagAttribute (vers, RESOURCES_ANDROID_NAMESPACE, " maxSdkVersion" ,
777+ bundle->getMaxSdkVersion (), errorOnFailedInsert)) {
778+ return UNKNOWN_ERROR;
779+ }
752780 }
753781
754782 if (bundle->getDebugMode ()) {
755783 sp<XMLNode> application = root->getChildElement (String16 (), String16 (" application" ));
756784 if (application != NULL ) {
757- addTagAttribute (application, RESOURCES_ANDROID_NAMESPACE, " debuggable" , " true" );
785+ if (!addTagAttribute (application, RESOURCES_ANDROID_NAMESPACE, " debuggable" , " true" ,
786+ errorOnFailedInsert)) {
787+ return UNKNOWN_ERROR;
788+ }
758789 }
759790 }
760791
0 commit comments