Pavel Shorokhov
Pavel Shorokhov

Reputation: 4974

How to sync Android project by gradle task?

I want to publish app in one click in Android Studio.

And I have 3 tasks: increment-version, build-app, and publish-app:

increaseVersionCode assembleRelease crashlyticsUploadDistributionRelease

The task increaseVersionCode (my own) just increment versionCode in build.gradle, but after that the task assembleRelease runs with previous value of versionCode (not incremented). Finally (after all 3 tasks), I have increased versionCode in build.gradle, and published app, but with old versionCode.

I think, I should run "Sync Project" after increaseVersionCode. But I don't know how to do it with gradle.

Is it possible, to run "Sync Project" of Android Studio by gradle task?

Source of increaseVersionCode:

import java.util.regex.Pattern
task('increaseVersionCode') << {
    def buildFile = file("build.gradle")
    def pattern = Pattern.compile("versionCode\\s+(\\d+)")
    def manifestText = buildFile.getText()
    def matcher = pattern.matcher(manifestText)
    matcher.find()
    def versionCode = Integer.parseInt(matcher.group(1))
    def manifestContent = matcher.replaceAll("versionCode " + ++versionCode)
    buildFile.write(manifestContent)
}

UPD, ANSWER

We need to split command into two bash commands:

./gradlew increaseVersionCode && ./gradlew assembleRelease crashlyticsUploadDistributionRelease

Using Android-Studio plugin BashSupport we may to run bash scripts from studio. So, we need to put this command into file, and run it with Android-Studio. Thanks to @k3b.

Upvotes: 1

Views: 845

Answers (1)

k3b
k3b

Reputation: 14755

note: gradle-scripts are compiled and not interpreted.

gradle compiles all build files and then executes the build. it does not notice that one of it-s own build files has changed after the build-compile so the app is build with the old version info.

i donot know if it is possible to tell gradle to rebuild the build script or execute a sub gradle with a freshly compiled build script

maybe you can create a batch file that you can execute from android studio and that does not depend on android-stuidio-build so you donot have the "tell android studio" issue:

  • gradlew increaseVersionCode
  • rem a second call to gradlew should recompile the build script
  • gradlew clean assembleRelease crashlyticsUploadDistributionRelease

Upvotes: 1

Related Questions