android - Android studio gradle 脚本找不到 Pattern 类
问题描述
我正在尝试向我的 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 类。
怎么了?
解决方案
在您的构建脚本中,您似乎没有导入所需的类java.util.regex.Pattern
(或者您可能没有复制/粘贴整个脚本?)
在 Groovy 和 Gradle 中有一些“默认导入”(请参阅 Groovy 默认导入和Gradle 默认导入),但 java.util.regex
包不是这些的一部分,因此您必须自己导入Pattern
类。
build.gradle
在脚本顶部添加此导入
import java.util.regex.Pattern
或者干脆使用全限定名
def getIncrementationVersions()
{
// ...
//find version name in manifest
def manifestFile = file('src/main/AndroidManifest.xml')
def matcher = java.util.regex.Pattern.compile('versionName=\"(\\d+)\\.(\\d+)\"').matcher(manifestFile.getText())
// ...
}
推荐阅读
- python - 虽然循环没有按预期退出
- mercurial - 如何正确使用 hg 共享扩展?
- python - 重新加载如何将库文件处理为`so`
- python - 数据点是否在矩形内?
- xamarin - Xamarin.iOS 上缺少 DispatchQoS 结构
- python - 如何通过 python 将 json 文件/数据发布到 api url
- python - 如果行等于python中的值,则打印列名
- excel - 我们是否有“存在”功能来使用 VBA 搜索 excel 工作表
- ruby-on-rails - 如果更新时存在属性,Rails如何仅调用自定义验证方法
- python - 如何将下面提到的 R 代码转换为 python 代码?