Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio gradle script can't find Pattern class

I'm trying to add an autoincremental version name feature to my gradle script but I'm getting the error:

Could not get unknown property 'Pattern' for project ':app' of type org.gradle.api.Project

My 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'
}

It seems I can use any Groovy class in an android studio gradle script.

What is happening?

like image 640
Notbad Avatar asked May 11 '26 22:05

Notbad


1 Answers

In your build script, it seems that you don't import the needed class java.util.regex.Pattern (or maybe you did not copy/paste the whole script?)

In Groovy and Gradle there are some "default imports" (see Groovy default imports and Gradle default imports) , but java.util.regex package is not part of these, so you have to import the Patternclass yourself.

Add this import at top of your build.gradle script

import java.util.regex.Pattern

Or simply use full qualified name

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())

    // ...
}    
like image 102
M.Ricciuti Avatar answered May 14 '26 14:05

M.Ricciuti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!