安装 Apk
在 Android 沙箱中安装 Apk 文件的步骤
由于lybic 的下载API和执行命令API存在超时限制, 你可能无法通过这些API安装较大的 Apk 文件。
不过,你可以大致通过以下伪代码流程来实现 Apk 文件的安装:
- 批量下载和安装脚本
#!/system/bin/sh
# 提供了 URL
curl 下载一个或多个apk -> apk 临时存储目录
pm install 安装下载的这些apk
rm 删除这些apk文件
# 提供了安卓上的安卓目录
pm install 安装这些apk但不删除apk文件- 使用nohup运行
nohup 可以让命令在后台运行,避免超时问题。你可以通过以下命令格式来运行脚本:
sh -c nohup sh -c {shell_scripts} >/dev/null 2>&1 &
参照 执行命令API 的 API,
curl -X POST "https://api.Lybic.cn/api/orgs/{orgId}/sandboxes/{sandboxId}/process" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"executable": "sh",
"args": ["-c", "nohup sh -c '{script_content}' >/dev/null 2>&1 &"]
}'import asyncio
import base64
from Lybic import LybicClient, LybicAuth
async def main():
async with LybicClient(
LybicAuth(
org_id="ORG-xxxx",
api_key="lysk-xxxxxxxxxxx",
endpoint="https://api.Lybic.cn/"
)
) as client:
await client.sandbox.execute_process(
"SBX-xxxx",
executable="sh",
args=["-c", f"nohup sh -c '{script_content}' >/dev/null 2>&1 &"]
)
if __name__ == "__main__":
asyncio.run(main())import { LybicClient } from '@Lybic/core'
const Lybic = new LybicClient({
baseUrl: 'https://api.Lybic.cn',
orgId: 'ORG-xxxx',
apiKey: 'lysk-your-api-key-here',
})
const result = await Lybic.execSandboxProcess('SBX-xxxx', {
executable: 'sh',
args: ["-c", "nohup sh -c '{script_content}' >/dev/null 2>&1 &"]
})package main
import (
"context"
"encoding/base64"
"fmt"
"github.com/Lybic/Lybic-sdk-go"
)
func main() {
ctx := context.Background()
client, _ := Lybic.NewClient(nil)
processDto := Lybic.SandboxProcessRequestDto{
Executable: "sh",
Args: []string{"-c", "nohup sh -c '{script_content}' >/dev/null 2>&1 &"},
}
result, err := client.ExecSandboxProcess(ctx, "SBX-xxxx", processDto)
if err != nil {
fmt.Println("执行进程时出错:", err)
return
}
}如果你使用 Python SDK,我们对此外提供了一个便捷的方法 install_apk 来安装 Apk 文件:
import asyncio
from lybic import LybicClient, LybicAuth, dto
async def install_apk_example():
async with LybicClient(
LybicAuth(
org_id="ORG-xxxx",
api_key="lysk-xxxxxxxxxxx",
endpoint="https://api.lybic.cn/"
)
) as client:
# Example 1: Install APK from URL
await client.tools.mobile_use.install_apk(
sandbox_id="SBX-xxxx",
app_sources=[
dto.HttpRemote(url_path="https://example.com/app.apk")
]
)
print("APK installation started (running in background)")
# Example 2: Install multiple APKs from URLs with custom headers
await client.tools.mobile_use.install_apk(
sandbox_id="SBX-xxxx",
app_sources=[
dto.HttpRemote(
url_path="https://example.com/app1.apk",
headers={"Authorization": "Bearer token"}
),
dto.HttpRemote(
url_path="https://example.com/app2.apk",
headers={"User-Agent": "CustomAgent/1.0"}
)
]
)
print("Multiple APKs installation started")
# Example 3: Install APK from local path
await client.tools.mobile_use.install_apk(
sandbox_id="SBX-xxxx",
app_sources=[
dto.AndroidLocal(apk_path="/data/local/tmp/app.apk")
]
)
print("Local APK installation started")
# Example 4: Mix local and remote APKs
await client.tools.mobile_use.install_apk(
sandbox_id="SBX-xxxx",
app_sources=[
dto.HttpRemote(url_path="https://example.com/app.apk"),
dto.AndroidLocal(apk_path="/sdcard/Download/local.apk")
]
)
print("Mixed APK installation started")
if __name__ == '__main__':
asyncio.run(install_apk_example())