后端:php
前端:android studio
背景:
目前是手动打包,打包完成之后打开apk所在文件夹,手动打开xftp,上传apk,手动修改php文件,通知其他人员更新内部软件。
需求:每次打包完成之后自动更新,通知在微信群@相关人员
打开build.gradle文件修改如下:
android {·······}代码块外新增
apply from: "${rootProject.rootDir}/upload.gradle"
def getVersionName() {
def date = new Date();
def versionName = date.format('yyyyMMddHHmm');
return versionName
}
android {·······}代码块内部新增
def currentVersionName = getVersionName()
defaultConfig {
applicationId "com.yuxuaner.yeji"
minSdk 21
targetSdk 28
versionCode 1
versionName currentVersionName //这里是引用当前时间为版本名称,这样不用每次更新版本名称
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
// 设置支持的SO库架构
abiFilters 'armeabi-v7a' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
}
android.applicationVariants.all { variant ->
String taskSuffix = variant.name.capitalize()
if (taskSuffix.contains("Debug")) {
println "Apk Build Start:"
task("uploadBuild${taskSuffix}") {
dependsOn ":app:assemble${taskSuffix}"
group 'build'
description 'Custom task for gradle'
doLast {
variant.outputs.all { output ->
uploadApk(output.outputFile,currentVersionName)//task传入apk文件和版本名称
}
}
}
}
}
新增upload.gradle 编译脚本
在应用根目录新建 upload.gradle 文件
ext.uploadApk = this.&uploadApk
/**
* 上传 apk 到 局域网服务器
*/
def uploadApk(File apk,String version) {
println "Apk "+version+" Build Start:"
if (apk == null) {
throw new RuntimeException("apk file not exists!")
}
println "*********************************** start upload file ******************************************"
def twoHyphens = "--"
def boundary = "*********"
def end = "\r\n"
def conn = new URL("http://192.168.0.243:7779/upload").openConnection()//服务器接收apk接口
conn.setRequestMethod("POST")
conn.setRequestProperty("Connection", "Keep-Alive")
conn.setRequestProperty("Charset", "UTF-8")
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary)
conn.setDoInput(true)
conn.setDoOutput(true)
def sb = new StringBuilder()
//添加参数:包名
sb.append(twoHyphens).append(boundary).append(end)
sb.append("Content-Disposition: form-data; name=package")
sb.append(end).append(end)
sb.append("com.*****.***").append(end)
//添加参数:版本名称
sb.append(twoHyphens).append(boundary).append(end)
sb.append("Content-Disposition: form-data; name=version")
sb.append(end).append(end)
sb.append(version).append(end)
//添加参数file: 需要上传的apk文件
sb.append(twoHyphens).append(boundary).append(end)
sb.append("Content-Disposition: form-data; name=file;filename=").append(apk.getName())
sb.append(end).append(end)
def dos = new DataOutputStream(conn.getOutputStream())
dos.writeBytes(sb.toString())
dos.flush()
sb.delete(0, sb.length())
def fis = new FileInputStream(apk)
byte[] bf = new byte[8192]
int len
while ((len = fis.read(bf)) != -1) {
dos.write(bf, 0, len)
}
sb.append(end)
sb.append(twoHyphens).append(boundary).append(end)
dos.writeBytes(sb.toString())
dos.flush()
fis.close()
dos.close()
conn.connect()
def text = conn.getContent().text
println text
println "*************** upload finish ***************"
}
php后台相关代码
/***
* 上传应用
* @param $package
* @param $version
* @return \think\response\Json
*/
public function upload($package,$version){
$apk = Db::name('apk_config')->where('package', $package)->find();
if($apk){
$temp_file = $_FILES['file']['tmp_name'];
$outPath = public_path();
$fp = fopen($temp_file, "r");
$file = fread($fp, $_FILES["file"]["size"]); //二进制数据流
$filename = '/'.$version.'.apk'; //新图片名称
$newFile = fopen($outPath.$filename,"w"); //打开文件准备写入
fwrite($newFile,$file); //写入二进制流到文件
fclose($newFile); //关闭文件
$upload = [
'version'=>$version,
'url'=>'http://192.168.0.243:7779/'.$version.'.apk',
'content'=>'自动打包更新'
];
Db::name('apk_config')->where('package',$package)->update($upload);
//微信hook框架哈
$api = new Api();
$api->sendText('19507255515@chatroom',' 应用包:'.$package.' 版本:'.$version.'版本已更新!!',['wxid_z8j7tpsmpo3o22']);
return $package." upload ok!!!!!!!!!!!";
}else{
return $package."not not exists";
}
}
android studio 设置
准备打包哈
> Configure project :app
Apk Build Start:
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :opencvsdk:preBuild UP-TO-DATE
> Task :opencvsdk:preDebugBuild UP-TO-DATE
> Task :opencvsdk:compileDebugAidl UP-TO-DATE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:mergeLibDexDebug UP-TO-DATE
> Task :opencvsdk:buildCMakeDebug[opencv_jni_shared]
> Task :opencvsdk:externalNativeBuildDebug
> Task :opencvsdk:mergeDebugJniLibFolders UP-TO-DATE
> Task :opencvsdk:mergeDebugNativeLibs UP-TO-DATE
> Task :opencvsdk:stripDebugDebugSymbols UP-TO-DATE
> Task :opencvsdk:copyDebugJniLibsProjectOnly UP-TO-DATE
> Task :app:mergeDebugNativeLibs UP-TO-DATE
> Task :app:stripDebugDebugSymbols UP-TO-DATE
> Task :app:validateSigningDebug UP-TO-DATE
> Task :app:writeDebugAppMetadata UP-TO-DATE
> Task :app:writeDebugSigningConfigVersions UP-TO-DATE
> Task :app:processDebugManifestForPackage
> Task :app:processDebugResources
省略若干·····················
> Task :app:compileDebugJavaWithJavac
> Task :app:compileDebugSources
> Task :app:dexBuilderDebug
> Task :app:mergeProjectDexDebug
> Task :app:packageDebug
> Task :app:assembleDebug
> Task :app:uploadBuildDebug
Apk 202302252139 Build Start:
*********************************** start upload file ******************************************
com.****.**** upload ok!!!!!!!!!!!
*************** upload finish ***************
BUILD SUCCESSFUL in 25s
53 actionable tasks: 14 executed, 39 up-to-date
服务器apk文件信息
相关微信群
Tips:
php框架是Thinkphp
微信hook框架是千寻微信框架
1326

被折叠的 条评论
为什么被折叠?



