android gradle 脚本如何在构建后自动将 APK 放入资产中
How to automatically put the APK into the asset after the build by android gradle script
我正在使用 DexClassLoader 进行动态加载 class,现在,我每次都需要将 APK 文件复制到资产文件夹中。有什么方法可以自动添加到 android gradle 脚本
谢谢
您可以使用 python 来完成此操作,同时确保您在执行此操作之前已经安装了 python。
在您的根项目中创建一个 python 文件。
build.py
import subprocess
import shutil
import os
import sys
# Call this function to build the apk via gradle.
def build_cmd():
# This will build your debug apk.
buildProject = "gradlew.bat clean --profile --recompile-scripts --offline --rerun-tasks assembleDebug";
# If you want to build a release apk use this instead.
# buildProject = "gradlew.bat clean --profile --recompile-scripts --offline --rerun-tasks assembleRelease -Pandroid.injected.signing.store.file=" + os.path.abspath("store_key.jks") + " -Pandroid.injected.signing.store.password=pass123 -Pandroid.injected.signing.key.alias=key_alias -Pandroid.injected.signing.key.password=pass123"
# As of Python 3 use run() instead of call() function
# https://docs.python.org/3/library/subprocess.html#older-high-level-api
subprocess.run(buildProject, shell=True)
# Call this function to move the builded apk to your desire directory.
def moveDir():
# Get the generated apk
apk_path = "./app/build/outputs/apk/debug/app-debug.apk"
# apk will rename and move to asset directory
shutil.move(apk_path, "./app/src/main/assets/app-debug.apk")
if __name__ == '__main__':
build_cmd()
moveDir()
然后运行终端build.py
python build.py
并等待 gradle 任务完成。
编辑:
因为一些成员在这个项目上没有python环境而不是使用python中的脚本尝试添加一些task 在你的 app/build.gradle
android {
// Some stuffs of yours....
task copyAPKtoAsset(type: Copy) {
from "/build/outputs/apk/debug/app-debug.apk", "/build/outputs/apk/release/app-release.apk"
into "/src/main/assets/"
}
afterEvaluate {
packageDebug.finalizedBy(copyAPKtoAsset)
}
}
构建您的项目并在之后检查您的 assets 目录。
我正在使用 DexClassLoader 进行动态加载 class,现在,我每次都需要将 APK 文件复制到资产文件夹中。有什么方法可以自动添加到 android gradle 脚本 谢谢
您可以使用 python 来完成此操作,同时确保您在执行此操作之前已经安装了 python。
在您的根项目中创建一个 python 文件。
build.py
import subprocess
import shutil
import os
import sys
# Call this function to build the apk via gradle.
def build_cmd():
# This will build your debug apk.
buildProject = "gradlew.bat clean --profile --recompile-scripts --offline --rerun-tasks assembleDebug";
# If you want to build a release apk use this instead.
# buildProject = "gradlew.bat clean --profile --recompile-scripts --offline --rerun-tasks assembleRelease -Pandroid.injected.signing.store.file=" + os.path.abspath("store_key.jks") + " -Pandroid.injected.signing.store.password=pass123 -Pandroid.injected.signing.key.alias=key_alias -Pandroid.injected.signing.key.password=pass123"
# As of Python 3 use run() instead of call() function
# https://docs.python.org/3/library/subprocess.html#older-high-level-api
subprocess.run(buildProject, shell=True)
# Call this function to move the builded apk to your desire directory.
def moveDir():
# Get the generated apk
apk_path = "./app/build/outputs/apk/debug/app-debug.apk"
# apk will rename and move to asset directory
shutil.move(apk_path, "./app/src/main/assets/app-debug.apk")
if __name__ == '__main__':
build_cmd()
moveDir()
然后运行终端build.py
python build.py
并等待 gradle 任务完成。
编辑:
因为一些成员在这个项目上没有python环境而不是使用python中的脚本尝试添加一些task 在你的 app/build.gradle
android {
// Some stuffs of yours....
task copyAPKtoAsset(type: Copy) {
from "/build/outputs/apk/debug/app-debug.apk", "/build/outputs/apk/release/app-release.apk"
into "/src/main/assets/"
}
afterEvaluate {
packageDebug.finalizedBy(copyAPKtoAsset)
}
}
构建您的项目并在之后检查您的 assets 目录。