Android 编译脚本
本篇文章主要简单记录下平时开发用到的一些脚本,用于编译,构建,打包等.
1: Gradle 编译命令(基础)
1.1: 清理项目
./gradlew clean
1.2: 查看所有任务
./gradlew tasks
这个可以看下输出:
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------
Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for the base and test modules
sourceSets - Prints out all the source sets defined in this project.
Build tasks
-----------
assemble - Assemble main outputs for all the variants.
assembleAndroidTest - Assembles all the Test applications.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
bundle - Assemble bundles for all the variants.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Cleanup tasks
-------------
lintFix - Runs lint on all variants and applies any safe suggestions to the source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'ArgbTest'.
components - Displays the components produced by root project 'ArgbTest'. [incubating]
dependencies - Displays all dependencies declared in root project 'ArgbTest'.
dependencyInsight - Displays the insight into a specific dependency in root project 'ArgbTest'.
dependentComponents - Displays the dependent components of components in root project 'ArgbTest'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'ArgbTest'. [incubating]
projects - Displays the sub-projects of root project 'ArgbTest'.
properties - Displays the properties of root project 'ArgbTest'.
tasks - Displays the tasks runnable from root project 'ArgbTest' (some of the displayed tasks may belong to subprojects).
Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.
Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
lintVitalRelease - Runs lint on just the fatal issues in the release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testReleaseUnitTest - Run unit tests for the release build.
To see all tasks and more detail, run gradlew tasks --all
To see more detail about a task, run gradlew help --task <task>
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
1.3: 编译所有 debug 包
./gradlew assembleDebug
1.4: 编译所有 release 包
./gradlew assembleRelease
1.5: 编译指定模块的 debug 包
./gradlew :app:assembleDebug
1.6: 安装 App
./gradlew installDebug
2: 简单的脚本案例
2.1: 安装启动apk
添加脚本build_install.sh 脚本内容如下:
#!/bin/bash
# 构建并安装 Debug 包
./gradlew clean
./gradlew assembleDebug
./gradlew installDebug
# 启动主 Activity
adb shell am start -n com.zh.argbtest/.MainActivity
执行脚本如下:
chmod +x build_install.sh
./build_install.sh
2.2: 指定apk输出名称
修改build.gralde :
android {
...
applicationVariants.all { variant ->
def formattedDate = new Date().format("yyyyMMdd_HHmmss")
variant.outputs.all { output ->
def newName = "app-${variant.versionName}-${formattedDate}.apk"
outputFileName = newName
}
}
}
编译后可以看到之前build/outputs/apk/app-debug.apk 换成了app-1.0-20250606_121147.apk
当然也可以修改build_install.sh脚本, 拷贝app-debug.apk从而生成新的文件.
#!/bin/bash
# 构建并安装 Debug 包
./gradlew clean
./gradlew assembleDebug
# 构建输出目录(以 debug 为例)
APK_DIR="app/build/outputs/apk/debug"
ORIGINAL_APK="${APK_DIR}/app-debug.apk"
# 检查 APK 是否存在
if [ ! -f "$ORIGINAL_APK" ]; then
echo "Error: APK file not found at $ORIGINAL_APK"
exit 1
fi
# 定义APK名称和包名
APK_NAME="custom_named_app"
# 复制并重命名 APK 到当前目录
cp "$ORIGINAL_APK" "${APK_DIR}/${APK_NAME}.apk"
# 安装 APK
#./gradlew installDebug
adb install -r "${APK_NAME}.apk"
# 启动主 Activity
adb shell am start -n com.zh.argbtest/.MainActivity