Android studio gradle脚本可以';找不到模式类
我正在尝试将自动增量版本名功能添加到gradle脚本中,但出现错误:Android studio gradle脚本可以';找不到模式类,android,android-studio,gradle,groovy,build.gradle,Android,Android Studio,Gradle,Groovy,Build.gradle,我正在尝试将自动增量版本名功能添加到gradle脚本中,但出现错误: Could not get unknown property 'Pattern' for project ':app' of type org.gradle.api.Project 我的身材。格雷德尔: apply plugin: 'com.android.library' android { compileSdkVersion 28 defaultConfig { minSdkVersio
Could not get unknown property 'Pattern' for project ':app' of type org.gradle.api.Project
我的身材。格雷德尔:
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'
}
apply插件:“com.android.library”
安卓{
编译DK28版
默认配置{
第15版
targetSdkVersion 28
def版本=getIncrementationVersions()
版本代码100
版本名称版本
}
建筑类型{
调试
{
minifyEnabled false
}
释放{
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”;
其他的
outputFileName=“lib release.aar”;
//outputFileName=“${archivesBaseName}-${version}.aar”
}
}
}
buildToolsVersion“28.0.3”
}
def getIncrementationVersions()
{
List runTasks=gradle.startParameter.getTaskNames();
//在清单中查找版本名
def manifestFile=file('src/main/AndroidManifest.xml')
def matcher=Pattern.compile('versionName=\'(\\d+\\。(\\d+\)).matcher(manifestFile.getText())
matcher.find()
//提取版本名部分
def firstPart=Integer.parseInt(matcher.group(1))
def secondPart=Integer.parseInt(matcher.group(2))
//检查runTask是否已发布
//如果发布-增量版本
用于(字符串项:runTasks)
{
第二部分++
}
def版本名称=第一部分+“+”第二部分
//更新清单
def manifestContent=matcher.replaceAll('versionName=\'+versionName+'\')
manifestFile.write(manifestContent)
println“incrementVersionName=“+versionName”
返回版本名
}
依赖关系{
实现文件树(包括:['*.jar'],目录:“libs”)
实现'com.android.support:appcompat-v7:28.0.0-rc02'
测试实现'junit:junit:4.12'
androidTestImplementation'com.android.support.test:runner:1.0.2'
androidTestImplementation'com.android.support.test.espresso:espresso核心:3.0.2'
实现'com.squareup.Reformation2:Reformation:2.4.0'
实现“net.pubnative:advertising\u id\u client:1.0.1”
实现'com.google.code.gson:gson:2.8.5'
}
似乎我可以在android studio gradle脚本中使用任何Groovy类
发生了什么?在构建脚本中,似乎没有导入所需的类
java.util.regex.Pattern
(或者可能没有复制/粘贴整个脚本?)
在Groovy和Gradle中有一些“默认导入”(请参见和),但是java.util.regex
包不是这些导入的一部分,因此您必须自己导入模式
类
将此导入添加到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())
// ...
}