chkfung / android-version-actions

MIT License
32 stars 24 forks source link

Multi Flavor Version Name #3

Open chkfung opened 2 years ago

chkfung commented 2 years ago

Hey @chkfung, thanks for the plugin. Is it feasible for you to put replaceAll in place of newGradle = newGradle.replace(versionCodeRegexPattern,$1${versionCode}); and same for versionName because we have multiple productFlavours with each flavour having its own versionCode and versionName, but it only replaces just the first occurrence.

Originally posted by @alisultan-cloaked in https://github.com/chkfung/android-version-actions/issues/2#issuecomment-1011777243

chkfung commented 2 years ago

@alisultan-cloaked Attached a reference to the documentation on how to work with multi flavor version by using android.defaultConfig.versionCode / android.defaultConfig.versionName, hope this helps

https://developer.android.com/studio/build/gradle-tips#combine-multiple-product-flavors Snippet

minApi24 { 
      ...
      versionCode 30000 + android.defaultConfig.versionCode
      versionName android.defaultConfig.versionName
      versionNameSuffix "-minApi24"
      ...
    }
alisultan-cloaked commented 2 years ago

@chkfung for example maintaining diff codes, we have

productFlavors { dev { versionCode 10 versionName "1.0.10" } staging { versionCode 20 versionName “1.0.20” } prod { versionCode 30 versionName “1.0.30” } }

and we want to build and change staging versionCode and versionName, so after running the plugin it actually changes it for the dev flavour since it comes first. replace only changes the first occurrence.

chkfung commented 2 years ago

I see, what do you think if moving the flavor configuration block to below the defaultConfig block?

alisultan-cloaked commented 2 years ago

nope, won't work

chkfung commented 2 years ago

It works in my case: https://github.com/chkfung/android_action_test_groovy/blob/main/app/build.gradle

The reason of not using replaceAll is because android.defaultConfig.versionCode / android.defaultConfig.versionName is available for dev to use it.

alisultan-cloaked commented 2 years ago

Well I found a diff solution for that according to our implementation and requirement, now I have created separate version.properties files for each flavour/environment, and added them using

def versionFile = new File('app/src/' + currentFlavorName + '/version.properties')
    if (versionFile.canRead()) {
        Properties props = new Properties()
        props.load(new FileInputStream(versionFile))
        if (props != null && props.containsKey('versionCode')
                && props.containsKey('versionName')) {
            android.defaultConfig.versionCode = Integer.valueOf(props['versionCode'])
            android.defaultConfig.versionName = String.valueOf(props['versionName']).replaceAll('"', '')
        } else {
            println 'version.properties found but some entries are missing'
        }
    } else {
        println 'version.properties not found'
    }

and then targeting it in the plugin

gradlePath: ${{ github.workspace }}/app/src/staging/version.properties