package com.ruoyi.common.utils;

import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ReadAPPUtil {
    /**
     * 读取apk
     *
     * @param apkUrl
     * @return
     */
    public static Map<String, Object> readAPK(File apkUrl) {
        Map<String, Object> resMap = new HashMap<String, Object>();
        try (ApkFile apkFile = new ApkFile(apkUrl)) {
            ApkMeta apkMeta = apkFile.getApkMeta();
            //app名称
            resMap.put("name", apkMeta.getName());
            //包名
            resMap.put("packageName", apkMeta.getPackageName());
            //版本号
            resMap.put("versionCode", apkMeta.getVersionCode());
            //版本名
            resMap.put("versionName", apkMeta.getVersionName());
            resMap.put("code", "0");
            resMap.put("error", "ok");
        } catch (IOException e) {
            e.printStackTrace();
            resMap.put("code", "-1");
            resMap.put("error", "读取apk文件失败");
        }
        return resMap;

    }

    public static void main(String[] args) {
        System.out.println("======apk=========");
        String apkUrl = "E:/AndroidStudioProjects/misi/app/release/app-kks.apk";
        File f = new File(apkUrl);
        Map<String, Object> mapApk = ReadAPPUtil.readAPK(f);
        for (String key : mapApk.keySet()) {
            System.out.println(key + ":" + mapApk.get(key));
        }
    }

}
