"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.buildApk=buildApk,exports.generateCloudAndroidApk=generateCloudAndroidApk;const tslib_1=require("tslib"),request_1=require("../utils/request"),miniAppI18N_1=tslib_1.__importDefault(require("../utils/miniAppI18N")),checkBuildArgument_1=require("./miniapp/checkBuildArgument"),miniappJson_1=require("../utils/miniappJson"),tools_1=require("../utils/tools"),androidCloudBuild_1=require("./miniapp/androidCloudBuild"),log=tslib_1.__importStar(require("../utils/log")),tools_2=require("./utils/tools"),report_1=require("../utils/report");async function buildApk(e){const r=Date.now();let i;try{if(!e.project)throw new Error("缺少参数 project");const{project:r}=e;r.targetPlatform="mini-android",await r.miniappAttr();const{buildArchiveOpts:o,cloudBuildInfo:t}=(0,checkBuildArgument_1.transformAndroidBuildArgument)(e);e.disableCache&&((0,tools_2.clearAndroidCacheDir)(),log.info("清除 Apk 构建缓存成功")),i=await generateCloudAndroidApk(e,o,t)}catch(e){log.error("构建 Apk 失败：",e.message),i={success:!1,errmsg:"构建 Apk 失败："+e.message}}const o=Date.now()-r;return e.project&&(0,report_1.reportAction)("buildApk",i.success?0:-1,i.errmsg,o,e.project),i}async function generateCloudAndroidApk(e,r,i){var o;const{project:t,output:s}=e;let n=!1,d="failed";try{miniAppI18N_1.default.createI18NInfo(t.projectPath);const{useCloudUpload:u,useAab:a,packageVersion:c,packageDesc:l,certificateInfo:p,uploadAppInfo:A}=i,g=Object.assign({},r);u?(g.useCloudSync=!0,g.version=c,g.desc=l):(g.version=A?A.userVersion:"",g.desc=A?A.userDesc:"");const k=(0,miniappJson_1.tryGetAndroidMiniappJson)(t.projectPath);await checkAndroidSdkVersion(k);const m=Object.assign(Object.assign({},e),{resourceDir:(0,tools_2.getMiniappCacheDir)(),robot:e.robot||1});let _="";_=a?await(0,androidCloudBuild_1.getAndroidAabBuild)(m).build(g,s,p.androidCertificate):await(0,androidCloudBuild_1.getAndroidApkBuild)(m).build(g,s,p.androidCertificate),(null===(o=k.channel)||void 0===o?void 0:o.value)&&(_=`${_.slice(0,-4)}-${k.channel.value}${a?".aab":".Apk"}`),log.success("构建 Apk 成功"),n=!0,d="success! output file at "+_}catch(e){n=!1,d=e.message,log.error("构建 Apk 失败：",e.message)}return{success:n,errmsg:d}}async function checkAndroidSdkVersion(e){if(e.sdkVersion&&(null==e?void 0:e.usePluginSdk)){const r="http://dldir1.qq.com/WechatWebDev/donut/android-extended-sdk-version.json";let{body:i}=await(0,request_1.request)({url:r,method:"get"});try{i=JSON.parse(i)}catch(e){}const o=i;if(o){Object.keys(e.usePluginSdk).forEach(r=>{const i=e.usePluginSdk[r];if(!i)return;const t=o[r],s=t?t[i]:void 0;if(!s){const e=`当前使用的 ${r} 扩展SDK版本（${i}）未包含在官方支持的版本列表内，请检查`;return void log.warn(e)}const n=s.minMainSdk;if((0,tools_1.compareSemVer)(e.sdkVersion,n)<0)throw new Error("存在主模块 SDK 版本不满足拓展 SDK 使用最小版本情况，请检查")})}}}