Câu hỏi Làm thế nào để autoincrement versionCode trong Android Gradle
Tôi đang thử nghiệm với hệ thống xây dựng Android mới dựa trên Gradle và tôi đang nghĩ, cách tốt nhất để autoincrease versionCode với nó là gì. Tôi đang nghĩ về hai lựa chọn
- tạo tệp versionCode, đọc số từ nó, tăng nó và ghi nó trở lại tệp
- phân tích cú pháp AndroidManifest.xml, đọc versionCode từ nó, tăng nó và viết nó trở lại tệp AndroidManifest.xml
Có giải pháp đơn giản hay phù hợp hơn không?
Có ai sử dụng một trong các tùy chọn mentiod và có thể chia sẻ nó với tôi không?
76
2017-07-03 12:52
gốc
Các câu trả lời:
Tôi đã quyết định lựa chọn thứ hai - để phân tích cú pháp AndroidManifest.xml
. Đây là đoạn mã hoạt động.
task('increaseVersionCode') << {
def manifestFile = file("AndroidManifest.xml")
def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"")
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}
versionCode
được phát hành cho bản phát hành bản phát hành trong trường hợp này. Để tăng nó để gỡ lỗi xây dựng phương trình task.name thay đổi trong task.whenTaskAdded
gọi lại.
56
2017-07-08 11:12
Tôi đang sử dụng mã này để cập nhật cả versionCode và versionName, sử dụng lược đồ "major.minor.patch.build".
import java.util.regex.Pattern
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()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"")
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))
def mNextVersionName = majorVersion + "." + minorVersion + "." + pointVersion + "." + (buildVersion + 1)
def manifestContent = matcherVersionNumber.replaceAll("versionName=\"" + mNextVersionName + "\"")
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig' || task.name == 'generateDebugBuildConfig') {
task.dependsOn 'increaseVersionCode'
task.dependsOn 'incrementVersionName'
}
}
39
2018-02-22 07:35
nó không có vẻ là thiết lập chính xác mà bạn đang sử dụng, nhưng trong trường hợp của tôi, các bản dựng đang được chạy bởi jenkins và tôi muốn sử dụng $ BUILD_NUMBER làm mã phiên bản của ứng dụng. sau đây đã làm các trick cho tôi ở đó.
defaultConfig {
...
versionCode System.getenv("BUILD_NUMBER") as Integer ?: 9999
...
}
36
2018-04-16 17:46
Tôi đang sử dụng dấu thời gian cho mã phiên bản:
def date = new Date()
def formattedDate = date.format('yyMMddHHmm')
def code = formattedDate.toInteger()
defaultConfig {
minSdkVersion 10
targetSdkVersion 21
versionCode code
}
13
2018-02-23 07:26
Nếu bạn đang giữ mã phiên bản trong tệp build.gradle, hãy sử dụng đoạn mã tiếp theo:
import java.util.regex.Pattern
task('increaseVersionCode') << {
def buildFile = file("build.gradle")
def pattern = Pattern.compile("versionCode\\s+(\\d+)")
def manifestText = buildFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode " + ++versionCode)
buildFile.write(manifestContent)
}
6
2018-03-25 13:17
Để lấy cả sản phẩm và các loại hình xây dựng vào tài khoản và sử dụng logic của @ sealskej để phân tích tệp kê khai:
android.applicationVariants.all { variant ->
/* Generate task to increment version code for release */
if (variant.name.contains("Release")) {
def incrementVersionCodeTaskName = "increment${variant.name}VersionCode"
task(incrementVersionCodeTaskName) << {
if (android.defaultConfig.versionCode == -1) {
def manifestFile = file(android.sourceSets.main.manifest.srcFile)
def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
android.defaultConfig.versionCode = versionCode + 1
def manifestContent = matcher.replaceAll("versionCode=\"" + android.defaultConfig.versionCode + "\"")
manifestFile.write(manifestContent)
}
}
def hookTask = variant.generateBuildConfig
hookTask.dependsOn(incrementVersionCodeTaskName)
}
}
4
2017-10-03 23:42
Gradle Advanced Build Version là một plugin cho Android giúp tạo ra versionCode và versionName tự động. có rất nhiều tùy chỉnh. ở đây bạn có thể tìm thêm thông tin về nó
https://github.com/moallemi/gradle-advanced-build-version
4
2018-01-12 05:24
Công việc VersionCode tăng dần (Integer):
Điều này hoạt động bằng cách tăng Mã phiên bản lên 1
, ví dụ:
android:versionCode="1"
1 + 1 = 2
import java.util.regex.Pattern
task incrementVersionCode << {
def manifestFile = file('AndroidManifest.xml')
def matcher = Pattern.compile('versionCode=\"(\\d+)\"')
.matcher(manifestFile.getText())
matcher.find()
def manifestContent = matcher.replaceAll('versionCode=\"' +
++Integer.parseInt(matcher.group(1)) + '\"')
manifestFile.write(manifestContent)
}
Tác vụ VersionName tăng dần (String):
Cảnh báo: Phải chứa 1
thời gian cho Regex
Điều này hoạt động bằng cách tăng Tên phiên bản theo 0.01
, ví dụ:
Bạn có thể dễ dàng sửa đổi và thay đổi số tiền của bạn hoặc thêm nhiều chữ số hơn.
android:versionName="1.0"
1.00 + 0.01 -> 1.01
1.01 + 0.01 -> 1.02
1.10 + 0.01 -> 1.11
1.99 + 0.01 -> 2.0
1.90 + 0.01 -> 1.91
import java.util.regex.Pattern
task incrementVersionName << {
def manifestFile = file('AndroidManifest.xml')
def matcher = Pattern.compile('versionName=\"(\\d+)\\.(\\d+)\"')
.matcher(manifestFile.getText())
matcher.find()
def versionName = String.format("%.2f", Integer
.parseInt(matcher.group(1)) + Double.parseDouble("." + matcher
.group(2)) + 0.01)
def manifestContent = matcher.replaceAll('versionName=\"' +
versionName + '\"')
manifestFile.write(manifestContent)
}
Trước:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exmaple.test"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.0" >
Sau:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exmaple.test"
android:installLocation="auto"
android:versionCode="2"
android:versionName="1.01" >
3
2017-09-29 02:38
Để thêm vào bài đăng của @ sealskej, đây là cách bạn có thể cập nhật cả mã phiên bản và tên phiên bản của bạn (Ở đây tôi giả sử phiên bản chính và nhỏ của bạn là cả 0):
task('increaseVersion') << {
def manifestFile = file("AndroidManifest.xml")
def patternVersionCode = Pattern.compile("versionCode=\"(\\d+)\"")
def manifestText = manifestFile.getText()
def matcherVersionCode = patternVersionCode.matcher(manifestText)
matcherVersionCode.find()
def versionCode = Integer.parseInt(matcherVersionCode.group(1))
def manifestContent = matcherVersionCode.replaceAll("versionCode=\"" + ++versionCode + "\"")
manifestFile.write(manifestContent)
def patternVersionNumber = Pattern.compile("versionName=\"0.0.(\\d+)\"")
manifestText = manifestFile.getText()
def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
matcherVersionNumber.find()
def versionNumber = Integer.parseInt(matcherVersionNumber.group(1))
manifestContent = matcherVersionNumber.replaceAll("versionName=\"0.0." + ++versionNumber + "\"")
manifestFile.write(manifestContent)
}
2
2017-07-13 05:50
cái này thì sao ?
thêm vào build.gradle (mô-đun ứng dụng)
def getBuildVersionCode() {
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
def formattedSeconds = date.format('HHmmssSSS')
def formatInt = formattedDate as int;
def SecondsInt = formattedSeconds as int;
return (formatInt + SecondsInt) as int
}
defaultConfig {
applicationId "com.app"
minSdkVersion 17
targetSdkVersion 22
versionCode getBuildVersionCode()
versionName "1.0"
}
1
2017-07-16 13:16