码迷,mamicode.com
首页 > 编程语言 > 详细

java自动识别上传的apk版本号

时间:2018-04-09 15:07:22      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:stream   UI   return   ons   http   name   pack   tostring   string   

 

 

 

import java.util.List;



public class ApkInfo {

    private String versionCode;
    private String versionName;
    private String apkPackage;
    private String minSdkVersion;
    private String apkName;
    @SuppressWarnings("rawtypes")
    private List uses_permission;

    public ApkInfo() {
        versionCode = null;
        versionName = null;
        apkPackage = null;
        minSdkVersion = null;
        apkName = null;
        uses_permission = null;
    }

    public String getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(String versionCode) {
        this.versionCode = versionCode;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getApkPackage() {
        return apkPackage;
    }

    public void setApkPackage(String apkPackage) {
        this.apkPackage = apkPackage;
    }

    public String getMinSdkVersion() {
        return minSdkVersion;
    }

    public void setMinSdkVersion(String minSdkVersion) {
        this.minSdkVersion = minSdkVersion;
    }

    public String getApkName() {
        return apkName;
    }

    public void setApkName(String apkName) {
        this.apkName = apkName;
    }

    @SuppressWarnings({ "rawtypes"})
    public List getUses_permission() {
        return uses_permission;
    }

    @SuppressWarnings({ "rawtypes"})
    public void setUses_permission(List uses_permission) {
        this.uses_permission = uses_permission;
    }

    public String toString() {
//        return (new StringBuilder("ApkInfo [versionCode=")).append(versionCode)
//                .append(", versionName=").append(versionName).append(
//                        ", apkPackage=").append(apkPackage).append(
//                        ", minSdkVersion=").append(minSdkVersion).append(
//                        ", apkName=").append(apkName).append(
//                        ", uses_permission=").append(uses_permission).append(
//                        "]").toString();
            return (new StringBuilder("&version="+versionName+"&versioncode="+versionCode)).toString();
    }
}

 

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import cn.zsmy.constant.Constant;

public class ApkUtil {
    
    private static final Namespace NS = Namespace.getNamespace("http://schemas.android.com/apk/res/android");
    
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static ApkInfo getApkInfo(String apkPath){
        ApkInfo apkInfo = new ApkInfo();
        SAXBuilder builder = new SAXBuilder();
        Document document = null;
        try{
            document = builder.build(getXmlInputStream(apkPath));
        }catch (Exception e) {
            e.printStackTrace();
        }
        Element root = document.getRootElement();//跟节点-->manifest
        apkInfo.setVersionCode(root.getAttributeValue("versionCode",NS));
        apkInfo.setVersionName(root.getAttributeValue("versionName", NS));
        apkInfo.setApkPackage(root.getAttributeValue("package"));
        Element elemUseSdk = root.getChild("uses-sdk");//子节点-->uses-sdk
        apkInfo.setMinSdkVersion(elemUseSdk.getAttributeValue("minSdkVersion", NS));
        List listPermission = root.getChildren("uses-permission");//子节点是个集合
        List permissions = new ArrayList();
        for(Object object : listPermission){
            String permission = ((Element)object).getAttributeValue("name", NS);
            permissions.add(permission);
        }
        apkInfo.setUses_permission(permissions);
        Constant.MY_LOG.debug("\n版本号:"+apkInfo.getVersionCode()+"\n版本名:"+apkInfo.getVersionName()+"\n包名:"+apkInfo.getApkPackage());
        //String str = "\n版本号:"+versionCode+"\n版本名:"+versionName+"\n包名:"+packageName;
        Constant.MY_LOG.debug(root.getAttributes().toString());
        return apkInfo;
//        String s = root.getAttributes().toString();
//        String c[] = s.split(",");
//        String versionCode = null;
//        String versionName = null;
//        String packageName = null;
//        for(String a: c){
//            if(a.contains("versionCode")){
//                versionCode = a.substring(a.indexOf("versionCode=\"")+13, a.lastIndexOf("\""));
//            }
//            if(a.contains("versionName")){
//                versionName = a.substring(a.indexOf("versionName=\"")+13, a.lastIndexOf("\""));
//            }
//            if(a.contains("package")){
//                packageName = a.substring(a.indexOf("package=\"")+9, a.lastIndexOf("\""));
//            }
//        }        
//        
//        Constant.MY_LOG.debug("\n版本号:"+versionCode+"\n版本名:"+versionName+"\n包名:"+packageName);
//        String str = "\n版本号:"+versionCode+"\n版本名:"+versionName+"\n包名:"+packageName;
////        return root.getAttributes().toString();
//        return str;
//        return "ss";
    }

    private static InputStream getXmlInputStream(String apkPath) {
        InputStream inputStream = null;
        InputStream xmlInputStream = null;
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(apkPath);
            ZipEntry zipEntry = new ZipEntry("AndroidManifest.xml");
            inputStream = zipFile.getInputStream(zipEntry);
            AXMLPrinter xmlPrinter = new AXMLPrinter();
            xmlPrinter.startPrinf(inputStream);
            xmlInputStream = new ByteArrayInputStream(xmlPrinter.getBuf().toString().getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
            try {
                inputStream.close();
                zipFile.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return xmlInputStream;
    }

}

 

java自动识别上传的apk版本号

标签:stream   UI   return   ons   http   name   pack   tostring   string   

原文地址:https://www.cnblogs.com/shihaiming/p/8759025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!