项目中有个需求想本地编译完成自动上传release 的apk到服务器,因为功能比较简单,不想借助第三方来实现,这是个我自定义gradle task来上传apk的例子。
自定义task
task apkUpload(type: Exec, dependsOn: ['assembleRelease']) {
def version = project.android.defaultConfig.versionName
def apkPath = "$buildDir/outputs/apk/release/app-release.apk"
commandLine "curl", "-F", "apk=@$apkPath", "上传地址"
}
创建了一个Exec
类型的task, 依赖assembleRelease
任务, 最后执行shell curl -f apk=@{文件路径} {上传路径}
来完成上传
服务器示例 (PHP Codeigniter)
/**
* 上传apk
* @param $version
*/
public function upload($version)
{
$config['upload_path'] = FCPATH . 'apks/';
$config['allowed_types'] = '*';
$config['file_name'] = "beauty_$version.apk";
$config['max_size'] = 1024 * 10;
$config['overwrite'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('apk')) {
file_put_contents(FCPATH . 'apks/version', $version);
echo "upload success!!";
} else {
echo $this->upload->display_errors();
echo "upload failure!!";
}
}