Esempio n. 1
0
  /**
   * Computes the minimum SDK and target SDK versions for the project
   *
   * @param project the project to look up the versions for
   * @return a pair of (minimum SDK, target SDK) versions, never null
   */
  @NonNull
  public static Pair<Integer, Integer> computeSdkVersions(IProject project) {
    int mMinSdkVersion = 1;
    int mTargetSdkVersion = 1;

    IAbstractFile manifestFile = AndroidManifest.getManifest(new IFolderWrapper(project));
    if (manifestFile != null) {
      try {
        Object value = AndroidManifest.getMinSdkVersion(manifestFile);
        mMinSdkVersion = 1; // Default case if missing
        if (value instanceof Integer) {
          mMinSdkVersion = ((Integer) value).intValue();
        } else if (value instanceof String) {
          // handle codename, only if we can resolve it.
          if (Sdk.getCurrent() != null) {
            IAndroidTarget target =
                Sdk.getCurrent().getTargetFromHashString("android-" + value); // $NON-NLS-1$
            if (target != null) {
              // codename future API level is current api + 1
              mMinSdkVersion = target.getVersion().getApiLevel() + 1;
            }
          }
        }

        Integer i = AndroidManifest.getTargetSdkVersion(manifestFile);
        if (i == null) {
          mTargetSdkVersion = mMinSdkVersion;
        } else {
          mTargetSdkVersion = i.intValue();
        }
      } catch (XPathExpressionException e) {
        // do nothing we'll use 1 below.
      } catch (StreamException e) {
        // do nothing we'll use 1 below.
      }
    }

    return Pair.of(mMinSdkVersion, mTargetSdkVersion);
  }