From 0386f5dcf9d0f472f243506be3c40f5cf46287a2 Mon Sep 17 00:00:00 2001 From: Raphael Moll Date: Fri, 14 Sep 2012 13:53:51 -0700 Subject: ManifestMerger: fix handling of codenames in min/targetSdkVersion. The change is that the manifest merger will only accept a codename if it's invoked in a context that can resolve that codename to an API level. This allows to produce an error on bogus codenames and to properly check that the API levels match appropriately. Change-Id: Ic70c0c3690b13d94dba81bb78cc09386016b2ef1 --- .../src/com/android/manifmerger/ICallback.java | 37 +++++++++++++ manifmerger/src/com/android/manifmerger/Main.java | 2 +- .../com/android/manifmerger/ManifestMerger.java | 64 +++++++++++++++++----- 3 files changed, 89 insertions(+), 14 deletions(-) create mode 100755 manifmerger/src/com/android/manifmerger/ICallback.java (limited to 'manifmerger/src') diff --git a/manifmerger/src/com/android/manifmerger/ICallback.java b/manifmerger/src/com/android/manifmerger/ICallback.java new file mode 100755 index 0000000..26ae40d --- /dev/null +++ b/manifmerger/src/com/android/manifmerger/ICallback.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.manifmerger; + +import com.android.annotations.NonNull; + +/** + * Callback used by the ManifestMerger to query the caller. + */ +public interface ICallback { + + public static final int UNKNOWN_CODENAME = 0; + + /** + * Queries the caller to find the API level for a given provisional API codename, + * as used in the <uses-sdk> {@code minSdkVersion} field. + * + * @param codename A non-null codename string. + * @return The integer API > 0 for the given codename, or {@link #UNKNOWN_CODENAME}. + */ + public int queryCodenameApiLevel(@NonNull String codename); + +} diff --git a/manifmerger/src/com/android/manifmerger/Main.java b/manifmerger/src/com/android/manifmerger/Main.java index 78da1a3..c48033f 100644 --- a/manifmerger/src/com/android/manifmerger/Main.java +++ b/manifmerger/src/com/android/manifmerger/Main.java @@ -57,7 +57,7 @@ public class Main { // Create a new ManifestMerger and call its process method. // It will take care of validating its own arguments. - ManifestMerger mm = new ManifestMerger(MergerLog.wrapSdkLog(mSdkLog)); + ManifestMerger mm = new ManifestMerger(MergerLog.wrapSdkLog(mSdkLog), null); String[] libPaths = mArgvParser.getParamLibs(); File[] libFiles = new File[libPaths.length]; diff --git a/manifmerger/src/com/android/manifmerger/ManifestMerger.java b/manifmerger/src/com/android/manifmerger/ManifestMerger.java index 8a61e67..2d5d3ec 100755 --- a/manifmerger/src/com/android/manifmerger/ManifestMerger.java +++ b/manifmerger/src/com/android/manifmerger/ManifestMerger.java @@ -45,7 +45,7 @@ import javax.xml.xpath.XPathExpressionException; /** * Merges a library manifest into a main application manifest. *

- * To use, create with {@link ManifestMerger#ManifestMerger(IMergerLog)} then + * To use, create with {@link ManifestMerger#ManifestMerger(IMergerLog, ICallback)} then * call {@link ManifestMerger#process(File, File, File[])}. *

*

 Merge operations:
@@ -69,6 +69,7 @@ import javax.xml.xpath.XPathExpressionException;
  *      => Add. OK if already defined.
  * E- uses-sdk:
  *      {@code @minSdkVersion}: error if dest<lib. Never automatically change dest minsdk.
+ *                              Codenames are accepted if we can resolve their API level.
  *      {@code @targetSdkVersion}: warning if dest<lib.
  *                                 Never automatically change dest targetsdk.
  *      {@code @maxSdkVersion}: obsolete, ignored. Not used in comparisons and not merged.
@@ -117,7 +118,9 @@ import javax.xml.xpath.XPathExpressionException;
 public class ManifestMerger {
 
     /** Logger object. Never null. */
-    private IMergerLog mLog;
+    private final IMergerLog mLog;
+    /** An optional callback that the merger can use to query the calling SDK. */
+    private final ICallback mCallback;
     private XPath mXPath;
     private Document mMainDoc;
 
@@ -125,8 +128,15 @@ public class ManifestMerger {
     private String NS_PREFIX = AndroidXPathFactory.DEFAULT_NS_PREFIX;
     private int destMinSdk;
 
-    public ManifestMerger(IMergerLog log) {
+    /**
+     * Creates a new {@link ManifestMerger}.
+     *
+     * @param log A non-null merger log to capture all warnings, errors and their location.
+     * @param callback An optional callback that the merger can use to query the calling SDK.
+     */
+    public ManifestMerger(@NonNull IMergerLog log, @Nullable ICallback callback) {
         mLog = log;
+        mCallback = callback;
     }
 
     /**
@@ -726,11 +736,12 @@ public class ManifestMerger {
     }
 
     /**
-     * Checks (but does not merge) uses-sdk attribues using the following rules:
+     * Checks (but does not merge) uses-sdk attributes using the following rules:
      * 
      * - {@code @minSdkVersion}: error if dest<lib. Never automatically change dest minsdk.
      * - {@code @targetSdkVersion}: warning if dest<lib. Never automatically change destination.
      * - {@code @maxSdkVersion}: obsolete, ignored. Not used in comparisons and not merged.
+     * - The API level can be a codename if we have a callback that can convert it to an integer.
      * 
* @param libDoc The library document to merge from. Must not be null. * @return True on success, false if any error occurred (printed to the {@link IMergerLog}). @@ -838,6 +849,7 @@ public class ManifestMerger { String s = destUsesSdk == null ? "" //$NON-NLS-1$ : destUsesSdk.getAttributeNS(NS_URI, attr + "SdkVersion"); //$NON-NLS-1$ + boolean result = true; assert s != null; s = s.trim(); try { @@ -846,14 +858,27 @@ public class ManifestMerger { destImplied.set(false); } } catch (NumberFormatException e) { - // Note: NumberFormatException.toString() has no interesting information - // so we don't output it. - mLog.error(Severity.ERROR, + boolean error = true; + if (mCallback != null) { + // Versions can contain codenames such as "JellyBean". + // We'll accept it only if have a callback that can give us the API level for it. + int apiLevel = mCallback.queryCodenameApiLevel(s); + if (apiLevel > ICallback.UNKNOWN_CODENAME) { + destValue.set(apiLevel); + destImplied.set(false); + error = false; + } + } + if (error) { + // Note: NumberFormatException.toString() has no interesting information + // so we don't output it. + mLog.error(Severity.ERROR, xmlFileAndLine(destUsesSdk == null ? mMainDoc : destUsesSdk), - "Failed to parse : must be an integer number.", + "Failed to parse : must be an integer number or codename.", attr, s); - return false; + result = false; + } } s = srcUsesSdk == null ? "" //$NON-NLS-1$ @@ -866,15 +891,28 @@ public class ManifestMerger { srcImplied.set(false); } } catch (NumberFormatException e) { - mLog.error(Severity.ERROR, + boolean error = true; + if (mCallback != null) { + // Versions can contain codenames such as "JellyBean". + // We'll accept it only if have a callback that can give us the API level for it. + int apiLevel = mCallback.queryCodenameApiLevel(s); + if (apiLevel > ICallback.UNKNOWN_CODENAME) { + srcValue.set(apiLevel); + srcImplied.set(false); + error = false; + } + } + if (error) { + mLog.error(Severity.ERROR, xmlFileAndLine(srcUsesSdk == null ? libDoc : srcUsesSdk), - "Failed to parse : must be an integer number.", + "Failed to parse : must be an integer number or codename.", attr, s); - return false; + result = false; + } } - return true; + return result; } -- cgit v1.1