我试图在我的gradle脚本中添加自动增量版本名称功能,但出现错误:
Could not get unknown property 'Pattern' for project ':app' of type org.gradle.api.Project
我的build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
def version = getIncrementationVersions()
versionCode 100
versionName version
}
buildTypes {
debug
{
minifyEnabled false
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
libraryVariants.all { variant ->
variant.outputs.all { output ->
if (outputFile != null && outputFileName.endsWith('.aar')) {
if (name.equals(com.android.builder.core.BuilderConstants.DEBUG))
outputFileName = "lib-debug.aar";
else
outputFileName = "lib-release.aar";
//outputFileName = "${archivesBaseName}-${version}.aar"
}
}
}
buildToolsVersion '28.0.3'
}
def getIncrementationVersions()
{
List<String> runTasks = gradle.startParameter.getTaskNames();
//find version name in manifest
def manifestFile = file('src/main/AndroidManifest.xml')
def matcher = Pattern.compile('versionName=\"(\\d+)\\.(\\d+)\"').matcher(manifestFile.getText())
matcher.find()
//extract versionName parts
def firstPart = Integer.parseInt(matcher.group(1))
def secondPart = Integer.parseInt(matcher.group(2))
//check is runTask release or not
// if release - increment version
for (String item : runTasks)
{
secondPart++
}
def versionName = firstPart + "." + secondPart
// update manifest
def manifestContent = matcher.replaceAll('versionName=\"' + versionName + '\"')
manifestFile.write(manifestContent)
println "incrementVersionName = " + versionName
return versionName
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation "net.pubnative:advertising_id_client:1.0.1"
implementation 'com.google.code.gson:gson:2.8.5'
}
似乎我可以在android studio gradle脚本中使用任何Groovy类。
发生了什么事?
答案 0 :(得分:1)
在您的构建脚本中,似乎您没有导入所需的类java.util.regex.Pattern
(或者也许您没有复制/粘贴整个脚本?)
在Groovy和Gradle中有一些“默认导入”(请参阅Groovy default imports和Gradle default imports),但是java.util.regex
包不属于其中,因此必须导入{{ 1}}给自己上课。
将此导入内容添加到Pattern
脚本的顶部
build.gradle
或者只是使用完整的限定名称
import java.util.regex.Pattern