首先使用
buildConfigField在android --> buildTypes的debug和release节点中添加当前时间
在gradle里面定义apkBuildTime
import java.text.DateFormat
import java.text.SimpleDateFormat
apply plugin: 'com.android.application'
def currentTime = getCurrentTime();
def getCurrentTime() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
return df.format(Calendar.getInstance(Locale.CHINA).getTime())
}
android {
compileSdkVersion 26
defaultConfig {
applicationId "penny.free.autopatchtime"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
buildConfigField("String", "apkBuildTime", "\"${currentTime}\"")
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField("String", "apkBuildTime", "\"${currentTime}\"")
}
}
}
然后更新gradle,这样BuildConfig.java就自动嵌入最新编译时间
package penny.free.autopatchtime;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "penny.free.autopatchtime";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from build type: debug
public static final String apkBuildTime = "2017-11-30 11:45:16";
}