需求
在web端集成命令来实现在线自定义Android项目的包名以及APP名称,并在线一键打包生成正式包
开始表演
环境(版本自己定,以下是我环境):
- 系统:Windows10
- jdk1.8
- gradle6.5:下载gradle并配置环境变量,cmd下"gradle -v"来验证是否配置成功
- android sdk:参考文章直接去看怎么配置:blog.csdn.net/mityuxuan/a…
Android测试项目一份
- 之前运行成功的Android代码一份,需要修改project目录下gradle.properties,内容如下:
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
android.injected.testOnly=false
android.buildCacheDir=D\:\\gradle\\android-build-cache\\cache
# 包名,这里定义的是APP目录下的build.gradle中的applicationId,并不是修改代码包名,基于此实现后以此类推即可
pkgName=com.xxx.xxx
# App名称
#CUR_PROJECT=xxx
APP_NAME=xxx
# App logo图标
#APP_ICON=@drawable/xxx_logo
# 版本号
#VERSION_CODE=2
# 版本名称
#VERSION_NAME=1.0.2
# 调试开关
#DEBUG_TAG=false
- 接下来修改app目录下的build.gradle,注意defaultConfig部分对应上一步
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
aaptOptions.useNewCruncher = false
aaptOptions.cruncherEnabled = false
signingConfigs {
release {
storeFile file("../xxx.jks")
keyAlias 'xxx'
keyPassword '123456'
storePassword '123456'
v1SigningEnabled true
v2SigningEnabled true
}
}
defaultConfig {
applicationId project.pkgName
//java代码中引用的定义形式
buildConfigField "String", "APP_NAME", "\"${APP_NAME}\""
manifestPlaceholders = [
// CUR_PROJECT : CUR_PROJECT,
APP_NAME : URLDecoder.decode(APP_NAME.toString(), "UTF-8"),
// VERSION_CODE: VERSION_CODE,
// VERSION_NAME: VERSION_NAME,
// APP_ICON : APP_ICON
]
// buildConfigField project.requestParam
// buildConfigField "String", "requestParam", "$requestParam"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
//是否混淆
minifyEnabled false
//混淆的配置文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
//修改apk 的 文件名,这里是文件名
applicationVariants.all { variant ->
variant.outputs.all { output ->
def date = new Date().format("yyyy_MM_dd_hh_mm", TimeZone.getTimeZone("GMT+08"))
if (variant.buildType.name == "debug") {
output.outputFileName = "${date}_arbitrator_v${android.defaultConfig.versionName}_debug_${android.defaultConfig.versionCode}.apk"
} else if (variant.buildType.name == "release") {
output.outputFileName = "${date}_arbitrator_v${android.defaultConfig.versionName}_release_${android.defaultConfig.versionCode}.apk"
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'pub.devrel:easypermissions:3.0.0'
}
- 编写shell脚本,内容如下,写好后放在Android项目根目录下:
#!/usr/bin/env bash
# 进入项目工程
#cd $projectPath
# 复制icon图标到资源目录(绝对路径 -> 项目相对路径),这里可以延伸为自定义logo,开屏图之类的功能,
# 比如icon在项目根目录,后台可以编写接口来上传logo之类的图片,执行shell脚本打包apk的时候就可以实现修改logo等图片
cp icon/ic_launcher.png app/src/main/res/mipmap-hdpi/
cp icon/ic_launcher.png app/src/main/res/mipmap-mdpi/
cp icon/ic_launcher.png app/src/main/res/mipmap-xhdpi/
cp icon/ic_launcher.png app/src/main/res/mipmap-xxhdpi/
cp icon/ic_launcher.png app/src/main/res/mipmap-xxxhdpi/
#cp /xxx/xxhdpi/ic_launcher.png app/src/main/res/mipmap-xxhdpi/
#... (全部icon图标的路径)
# 复制启动图到资源目录
#cp /icon/img_welcome.png app/src/main/res/mipmap-xxhdpi/
# 执行打包命令,-P用于拼接参数
./gradlew assembleRelease -PpkgName=com.xxx.xxx -PAPP_NAME=xxx
运行
运行方式有多种:
- 直接cmd到Android项目根目录,xxx.sh运行即可
- 直接在Android项目根目录双击xxx.sh即可
- 运行结束后打包的apk在这个目录下:app\build\outputs\apk\release\
web端在线自定义包名打包apk
- Android项目代码编写完毕后上传至托管平台
- clone至本地
- web端表单提交自定义包名字符串、APP名称字符串、logo、开屏图接口上传至服务端
- 服务端接收信息后拼接为shell脚本文件
- 运行该脚本,打包结束后路径是固定的,web端可以展示url或二维码提供下载安装
思考拓展
- 项目本身放置到服务器,在线打包对用户开放使用的话,Android代码需要放到服务器,有一定的泄露风险,目前还没想好怎么处理,或者其他方式去实现此功能
参考
- www.zhihu.com/question/30…
- blog.csdn.net/menglong032…
- blog.csdn.net/menglong032…