我想使用Gradle创建带有文件名时间戳的apk,并在页面上使用此信息。
摇篮: (下面)在创建输出文件时工作正常,如DSapp-1.2.0-debug_date_time.apk
applicationVariants.all { variant ->
variant.outputs.each { output ->
def project = "DSapp"
def sep = "_"
def version = variant.versionName
def newApkName = project + sep + version + sep + formattedDateD + "_" + formattedDateT + ".apk"
output.outputFile = new File(output.outputFile.parent, newApkName)
}
}
当我尝试使用buildConfigField
时
android {
compileSdkVersion 21
buildToolsVersion "22.0.1"
def date = new Date();
def formattedDateD = date.format('ddMMyy');
def formattedDateT = date.format('HHmm');
defaultConfig {
applicationId "com.app_name"
minSdkVersion 18
targetSdkVersion 20
versionCode 1
versionName "1.2.0"
buildConfigField "String", "STRING2", formattedDateD
buildConfigField "String", "STRING3", formattedDateT
}
About.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
version_number = (TextView)findViewById(R.id.version_number);
version_number.setText(mVersionName);
mVersionBuild= "( " + BuildConfig.STRING2 +"_"+BuildConfig.STRING3+ " )";
version_build = (TextView)findViewById(R.id.version_build);
version_build.setText(mVersionBuild);
我在BuildConfig.java
// Fields from default config.
public static final String STRING2 = 281116;
public static final String STRING3 = 0947;
Error:(15, 40) error: integer number too large: 0947
答案 0 :(得分:0)
因此,要实现使用Gradle在文件名中创建带有时间戳的apk,并在页面上使用此信息。
我不得不改变我的方法并放弃使用buildConfigField
的想法,因为不需要实现上述目标。
Gradle文件:
每versionNameSuffix
次添加'_' + getDateTime()
和
applicationVariants.all { variant ->
customAPKname(variant, defaultConfig, versionNameSuffix)
}
所以buildType
的每个部分都是这样的(调试示例):
debug {
versionNameSuffix '-debug' + '_' + getDateTime()
//to handle customAPKname build
applicationVariants.all { variant ->
customAPKname(variant, defaultConfig, versionNameSuffix)
}
}
在android{}
之外添加:
//to handle build process with output as: App_Name_versionName-buildType_date_time.apk
def customAPKname(variant, defaultConfig, buildType) {
variant.outputs.each { output ->
def file = output.packageApplication.outputFile
def fileName = "App_Name" + defaultConfig.versionName + buildType + ".apk"
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}
def getDateTime(){
def date = new Date()
def formattedDate = date.format('yyMMdd_HHmm')
return formattedDate
}
现在在About.java文件中:
private TextView version_number, version_build;
private String mVersionName, mVersionBuild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
if (BuildConfig.VERSION_NAME.contains("debug")){
mVersionName = "App_Name Software version: " + BuildConfig.VERSION_NAME.substring(0,11);
mVersionBuild = "Build version: " + BuildConfig.VERSION_NAME.substring(12);
}else {
mVersionName = "App_Name Software version: " + BuildConfig.VERSION_NAME.substring(0, 13);
mVersionBuild = "Build version: " + BuildConfig.VERSION_NAME.substring(14);
}
version_number = (TextView)findViewById(R.id.version_number);
version_number.setText(mVersionName);
version_build = (TextView)findViewById(R.id.version_build);
version_build.setText(mVersionBuild);
}
输出(用于调试):App_Name_1.2.0-debug_YYMMDD_HHMM.apk