Sto cercando di aggiornare automaticamente i parametri versionName e VersionCode in Android Manifest e li uso nel nome del file di output anziché in "app-release.apk".Come aumentare e rilasciare automaticamente l'apk firmato in Android Studio usando lo script Gradle
Da this site ho aggiunto questo codice nel mio file build.gradle:
import java.util.regex.Pattern
import com.android.builder.core.DefaultManifestParser
def mVersionCode
def mNextVersionName
def newName
task ('increaseVersionCode') << {
def manifestFile = file("src/main/AndroidManifest.xml")
def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
mVersionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode=\"" + ++mVersionCode + "\"")
manifestFile.write(manifestContent)
}
task ('incrementVersionName') << {
def manifestFile = file("src/main/AndroidManifest.xml")
def patternVersionNumber = Pattern.compile("versionName=\"(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\"")
def manifestText = manifestFile.getText()
def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
matcherVersionNumber.find()
def majorVersion = Integer.parseInt(matcherVersionNumber.group(1))
def minorVersion = Integer.parseInt(matcherVersionNumber.group(2))
def pointVersion = Integer.parseInt(matcherVersionNumber.group(3))
def buildVersion = Integer.parseInt(matcherVersionNumber.group(4))
mNextVersionName = majorVersion + "." + minorVersion + "." + pointVersion + "." + (buildVersion + 1)
def manifestContent = matcherVersionNumber.replaceAll("versionName=\"" + mNextVersionName + "\"")
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig') {
task.dependsOn 'increaseVersionCode'
task.dependsOn 'incrementVersionName'
}
}
questo codice funziona perfettamente, le 2 operazioni eseguite e aggiornare correttamente il file manifesto. Ora voglio utilizzare le 2 variabili mVersionCode
e mNextVersionName
nel blocco release
all'interno del buildTypes
in questo modo:
newName = defaultConfig.applicationId + "-" + mNextVersionName + " (" + mVersionCode + ").apk"
applicationVariants.all { variant ->
variant.outputs.each {output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace("app-release.apk", newName))
}
}
ma il valore restituito del 2 è nullo.
Ho provato anche l'impostazione delle proprietà e le proprietà aggiuntivi:
task.setProperty("vName", mNextVersionName)
ext.vName = mNextVersionName
extensions.extraProperties.set("vName", mNextVersionName)
nei 2 compiti e inducendole nel blocco release
, senza fortuna.
Qualcuno ha idee su come realizzare questo?
Sarebbe meglio se possiamo eseguire quel codice di aumento in BuildType {VersionCode ..., VersionName ...}. si, l'ho fatto, hai bisogno della mia soluzione? – VinceStyling
Certo, vediamo qual è la tua soluzione –