!DEPRECATED 由于FIR网站的更新,这篇文章的脚本已经失效了!新的API的利用脚本正在捣鼓中…

关于FIR.im就不做过多介绍了,本文主要是为了解决通过 Android Studio 或者说 Android Gradle 构建工具快速上传 APK 到 FIR 上。

FIR提供了上传APK的API,下面就是使用这个API的脚本:

*注意:这个脚本默认 Android 项目为 git 仓库,会将 git 最新 commit 的 hash 作为 FIR 的版本号,日志作为更新内容 *

fir_upload.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
# fork from https://gist.github.com/ggshily/8594b69a266a410d82a0
#
# Upload apk file to fir.im
#
# Syntax: upload_fir.sh <path/to/apk> <build type>
#
APK=$1
RELEASE_TYPE=$2
build() {
date +%m%d%H%M
}
if [ -z "$APK" ]
then
echo "Syntax: fir_upload.sh <path to apk> <build type>"
exit 1
fi
COMMIT_HASH=`git rev-parse --short HEAD`
VERSION="git-${COMMIT_HASH}"
BUILD=$(build)

USER_TOKEN="YOUR TOKEN XXX"
APP_ID="YOUR ID XX"
CHANGE_LOG=`git log -1 --pretty=%B`
echo "starting upload ${APK}"
echo "version: ${VERSION} build: ${BUILD}"
echo "getting token"
INFO=`curl "http://fir.im/api/v2/app/info/${APP_ID}?token=${USER_TOKEN}&type=android" 2>/dev/null`
KEY=$(echo ${INFO} | grep "pkg.*url" -o | grep "key.*$" -o | awk -F '"' '{print $3;}')
TOKEN=$(echo ${INFO} | grep "pkg.*url" -o | grep "token.*$" -o | awk -F '"' '{print $3;}')
echo "uploading"
APP_INFO=`curl -# -F file=@${APK} -F "key=${KEY}" -F "token=${TOKEN}" http://up.qiniu.com`

if [ $? != 0 ]
then
echo "upload error"
exit 1
fi

VERSIONOID=`echo ${APP_INFO} | grep "versionOid\".*$" -o | awk -F '"' '{print $3;}'`
curl -X PUT -d "changelog=${CHANGE_LOG}" -d "versionShort=${VERSION}" -d "version=${BUILD}" -d "release_type=${RELEASE_TYPE}" "http://fir.im/api/v2/appVersion/${VERSIONOID}/complete?token=${USER_TOKEN}&type=android"

将其中的USER_TOKENAPP_ID替换成你的即可,然后命名为fir_upload.sh
现在你可以用通过运行这个脚本上传APK了:

sh fir_upload.sh /path/to/apk debug

为了方便Android项目使用,可以保存至项目的根目录,即跟settings.gradle同目录

然后在你Android Application module 的build.gradle末尾添加下面几行即可:

1
2
3
4
5
6
7
8
9
10
11
afterEvaluate {
android.applicationVariants.all { variant ->
def output = variant.outputs.get(0).outputFile as File
def uploadTask = tasks.create(name: "fir${variant.name.capitalize()}",
description: "Upload ${output} to fir.im",
group: "Upload", type: Exec) {
commandLine 'sh', rootProject.file("fir_upload.sh").absolutePath, output.absolutePath, variant.name
}
uploadTask.dependsOn variant.assemble
}
}

重新 Sync Project 之后,可以在Android Studio 右侧的 Gradle任务栏里看到新增的 Upload任务组,运行firDebug或者firRelease即可。

当然也可以通过终端执行,如: ./gradlew firDebug

Enjoy it