Beispiel #1
0
  private int getTargetSDKVersion(String apkFile, String platformJARs) {
    // get AndroidManifest
    InputStream manifestIS = null;
    ZipFile archive = null;
    try {
      try {
        archive = new ZipFile(apkFile);
        for (Enumeration<? extends ZipEntry> entries = archive.entries();
            entries.hasMoreElements(); ) {
          ZipEntry entry = entries.nextElement();
          String entryName = entry.getName();
          // We are dealing with the Android manifest
          if (entryName.equals("AndroidManifest.xml")) {
            manifestIS = archive.getInputStream(entry);
            break;
          }
        }
      } catch (Exception e) {
        throw new RuntimeException("Error when looking for manifest in apk: " + e);
      }

      if (manifestIS == null) {
        G.v()
            .out
            .println(
                "Could not find sdk version in Android manifest! Using default: "
                    + defaultSdkVersion);
        return defaultSdkVersion;
      }

      // process AndroidManifest.xml
      int maxAPI = getMaxAPIAvailable(platformJARs);
      int sdkTargetVersion = -1;
      int minSdkVersion = -1;
      try {
        AXmlResourceParser parser = new AXmlResourceParser();
        parser.open(manifestIS);
        int depth = 0;
        loop:
        while (true) {
          int type = parser.next();
          switch (type) {
            case XmlPullParser.START_DOCUMENT:
              {
                break;
              }
            case XmlPullParser.END_DOCUMENT:
              break loop;
            case XmlPullParser.START_TAG:
              {
                depth++;
                String tagName = parser.getName();
                if (depth == 2 && tagName.equals("uses-sdk")) {
                  for (int i = 0; i != parser.getAttributeCount(); ++i) {
                    String attributeName = parser.getAttributeName(i);
                    String attributeValue = AXMLPrinter.getAttributeValue(parser, i);
                    if (attributeName.equals("targetSdkVersion")) {
                      sdkTargetVersion = Integer.parseInt(attributeValue);
                    } else if (attributeName.equals("minSdkVersion")) {
                      minSdkVersion = Integer.parseInt(attributeValue);
                    }
                  }
                }
                break;
              }
            case XmlPullParser.END_TAG:
              depth--;
              break;
            case XmlPullParser.TEXT:
              break;
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }

      int APIVersion = -1;
      if (sdkTargetVersion != -1) {
        if (sdkTargetVersion > maxAPI && minSdkVersion != -1 && minSdkVersion <= maxAPI) {
          G.v()
              .out
              .println(
                  "warning: Android API version '"
                      + sdkTargetVersion
                      + "' not available, using minApkVersion '"
                      + minSdkVersion
                      + "' instead");
          APIVersion = minSdkVersion;
        } else {
          APIVersion = sdkTargetVersion;
        }
      } else if (minSdkVersion != -1) {
        APIVersion = minSdkVersion;
      } else {
        G.v()
            .out
            .println(
                "Could not find sdk version in Android manifest! Using default: "
                    + defaultSdkVersion);
        APIVersion = defaultSdkVersion;
      }

      if (APIVersion <= 2) APIVersion = 3;
      return APIVersion;
    } finally {
      if (archive != null)
        try {
          archive.close();
        } catch (IOException e) {
          throw new RuntimeException("Error when looking for manifest in apk: " + e);
        }
    }
  }