これで、バイナリXMLデータからAPKファイルのバージョンを正常に取得できるようになりました。私は私の答えへの鍵を持ってどこ
このトピックでは、(私もリボのコードの私のバージョンを追加しました)です: How to parse the AndroidManifest.xml file inside an .apk package
はさらに、ここで私が書いたXML解析コードは、バージョンを取得するために具体的には、です:
このコードは、次のトピックからインスピレーションを得ている:(前のスニペットでSystemPackageTools.compareVersionsとして見られる)
/**
* Verifies at Conductor APK path if package version if newer
*
* @return True if package found is newer, false otherwise
*/
public static boolean checkIsNewVersion(String conductorApkPath) {
boolean newVersionExists = false;
// Decompress found APK's Manifest XML
// Source: https://stackoverflow.com/questions/2097813/how-to-parse-the-androidmanifest-xml-file-inside-an-apk-package/4761689#4761689
try {
if ((new File(conductorApkPath).exists())) {
JarFile jf = new JarFile(conductorApkPath);
InputStream is = jf.getInputStream(jf.getEntry("AndroidManifest.xml"));
byte[] xml = new byte[is.available()];
int br = is.read(xml);
//Tree tr = TrunkFactory.newTree();
String xmlResult = SystemPackageTools.decompressXML(xml);
//prt("XML\n"+tr.list());
if (!xmlResult.isEmpty()) {
InputStream in = new ByteArrayInputStream(xmlResult.getBytes());
// Source: http://developer.android.com/training/basics/network-ops/xml.html
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
String name = parser.getName();
if (name.equalsIgnoreCase("Manifest")) {
String pakVersion = parser.getAttributeValue(null, "versionName");
//NOTE: This is specific to my project. Replace with whatever is relevant on your side to fetch your project's version
String curVersion = SharedData.getPlayerVersion();
int isNewer = SystemPackageTools.compareVersions(pakVersion, curVersion);
newVersionExists = (isNewer == 1);
}
}
}
} catch (Exception ex) {
android.util.Log.e(TAG, "getIntents, ex: "+ex);
ex.printStackTrace();
}
return newVersionExists;
}
バージョン比較 NOTEの解析
XML Efficient way to compare version strings in Java
/**
* Compare 2 version strings and tell if the first is higher, equal or lower
* Source: https://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java
*
* @param ver1 Reference version
* @param ver2 Comparison version
*
* @return 1 if ver1 is higher, 0 if equal, -1 if ver1 is lower
*/
public static final int compareVersions(String ver1, String ver2) {
String[] vals1 = ver1.split("\\.");
String[] vals2 = ver2.split("\\.");
int i=0;
while(i<vals1.length&&i<vals2.length&&vals1[i].equals(vals2[i])) {
i++;
}
if (i<vals1.length&&i<vals2.length) {
int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
return diff<0?-1:diff==0?0:1;
}
return vals1.length<vals2.length?-1:vals1.length==vals2.length?0:1;
}
これが役立ちます。
これを確認してください。http://stackoverflow.com/a/4761689/1109425 – nandeesh
結局のところ何か分かりましたか?私は同じ問題に直面しています。ありがとう! – Mathieu
ところで、誰も良い解決策はファイルをダウンロードして、それが必要かどうかをチェックすることだと誰も考えないことを願っています。代わりに、あなたのサーバー*上で実行されるコードを*必要とし、使用可能なバージョン番号で応答すると考えられます。 – ToolmakerSteve