├── .editorconfig ├── .github_changelog_generator ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── build.gradle.kts ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src └── main ├── kotlin └── com │ └── github │ └── alexfu │ ├── AndroidAutoVersionExtension.kt │ ├── AndroidAutoVersionPlugin.kt │ ├── Version.kt │ └── VersionType.kt └── resources └── META-INF └── gradle-plugins └── com.github.alexfu.androidautoversion.properties /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.github_changelog_generator: -------------------------------------------------------------------------------- 1 | issues=false 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /.gradle/ 3 | /build/ 4 | *.iml 5 | gradle.properties 6 | .DS_Store 7 | local.properties 8 | out 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [3.2.0](https://github.com/alexfu/androidautoversion/tree/3.2.0) (2020-02-23) 4 | 5 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/3.1.0...3.2.0) 6 | 7 | **Merged pull requests:** 8 | 9 | - Version tasks [\#41](https://github.com/alexfu/androidautoversion/pull/41) ([alexfu](https://github.com/alexfu)) 10 | - Add task descriptions [\#40](https://github.com/alexfu/androidautoversion/pull/40) ([alexfu](https://github.com/alexfu)) 11 | 12 | ## [3.1.0](https://github.com/alexfu/androidautoversion/tree/3.1.0) (2020-02-22) 13 | 14 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/3.0.2...3.1.0) 15 | 16 | **Merged pull requests:** 17 | 18 | - Update to Kotlin [\#39](https://github.com/alexfu/androidautoversion/pull/39) ([alexfu](https://github.com/alexfu)) 19 | 20 | ## [3.0.2](https://github.com/alexfu/androidautoversion/tree/3.0.2) (2019-05-04) 21 | 22 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/3.0.1...3.0.2) 23 | 24 | **Merged pull requests:** 25 | 26 | - Bugfix for Issue 34: Manifest file not found [\#37](https://github.com/alexfu/androidautoversion/pull/37) ([ewoks](https://github.com/ewoks)) 27 | 28 | ## [3.0.1](https://github.com/alexfu/androidautoversion/tree/3.0.1) (2018-12-02) 29 | 30 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/3.0.0...3.0.1) 31 | 32 | **Merged pull requests:** 33 | 34 | - Fix missing versionCode/versionName from BuildConfig [\#36](https://github.com/alexfu/androidautoversion/pull/36) ([alexfu](https://github.com/alexfu)) 35 | 36 | ## [3.0.0](https://github.com/alexfu/androidautoversion/tree/3.0.0) (2018-06-11) 37 | 38 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/2.0.2...3.0.0) 39 | 40 | **Merged pull requests:** 41 | 42 | - Fix \#32 Compatibility issue with Gradle 4.6 [\#33](https://github.com/alexfu/androidautoversion/pull/33) ([hstorz](https://github.com/hstorz)) 43 | - Add instructions for adding to existing project [\#31](https://github.com/alexfu/androidautoversion/pull/31) ([ChrisGuzman](https://github.com/ChrisGuzman)) 44 | - updated version reference in README [\#30](https://github.com/alexfu/androidautoversion/pull/30) ([jamesmosier](https://github.com/jamesmosier)) 45 | 46 | ## [2.0.2](https://github.com/alexfu/androidautoversion/tree/2.0.2) (2017-11-20) 47 | 48 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/2.0.1...2.0.2) 49 | 50 | **Merged pull requests:** 51 | 52 | - Fix double suffix [\#29](https://github.com/alexfu/androidautoversion/pull/29) ([alexfu](https://github.com/alexfu)) 53 | 54 | ## [2.0.1](https://github.com/alexfu/androidautoversion/tree/2.0.1) (2017-11-19) 55 | 56 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/2.0.0...2.0.1) 57 | 58 | **Merged pull requests:** 59 | 60 | - Apply versionCode and versionName to merged flavors [\#28](https://github.com/alexfu/androidautoversion/pull/28) ([alexfu](https://github.com/alexfu)) 61 | 62 | ## [2.0.0](https://github.com/alexfu/androidautoversion/tree/2.0.0) (2017-11-15) 63 | 64 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/1.0.0...2.0.0) 65 | 66 | **Merged pull requests:** 67 | 68 | - Version 2 [\#26](https://github.com/alexfu/androidautoversion/pull/26) ([alexfu](https://github.com/alexfu)) 69 | 70 | ## [1.0.0](https://github.com/alexfu/androidautoversion/tree/1.0.0) (2017-02-06) 71 | 72 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.3.0...1.0.0) 73 | 74 | **Merged pull requests:** 75 | 76 | - FlavorConfig [\#25](https://github.com/alexfu/androidautoversion/pull/25) ([alexfu](https://github.com/alexfu)) 77 | 78 | ## [0.3.0](https://github.com/alexfu/androidautoversion/tree/0.3.0) (2017-02-01) 79 | 80 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.2.2...0.3.0) 81 | 82 | **Implemented enhancements:** 83 | 84 | - Use a default version file [\#18](https://github.com/alexfu/androidautoversion/pull/18) ([alexfu](https://github.com/alexfu)) 85 | - Post hooks [\#16](https://github.com/alexfu/androidautoversion/pull/16) ([alexfu](https://github.com/alexfu)) 86 | 87 | **Merged pull requests:** 88 | 89 | - Do not apply revision to normal releases [\#22](https://github.com/alexfu/androidautoversion/pull/22) ([alexfu](https://github.com/alexfu)) 90 | - Code cleanup [\#15](https://github.com/alexfu/androidautoversion/pull/15) ([alexfu](https://github.com/alexfu)) 91 | - Remove Git dependency [\#14](https://github.com/alexfu/androidautoversion/pull/14) ([alexfu](https://github.com/alexfu)) 92 | 93 | ## [0.2.2](https://github.com/alexfu/androidautoversion/tree/0.2.2) (2016-12-01) 94 | 95 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.2.1...0.2.2) 96 | 97 | **Fixed bugs:** 98 | 99 | - Use findByName instead of getByName [\#12](https://github.com/alexfu/androidautoversion/pull/12) ([alexfu](https://github.com/alexfu)) 100 | 101 | ## [0.2.1](https://github.com/alexfu/androidautoversion/tree/0.2.1) (2016-09-23) 102 | 103 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.2.0...0.2.1) 104 | 105 | ## [0.2.0](https://github.com/alexfu/androidautoversion/tree/0.2.0) (2016-09-22) 106 | 107 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.1.4...0.2.0) 108 | 109 | **Implemented enhancements:** 110 | 111 | - Beta support [\#9](https://github.com/alexfu/androidautoversion/pull/9) ([alexfu](https://github.com/alexfu)) 112 | - Custom version name format [\#8](https://github.com/alexfu/androidautoversion/pull/8) ([alexfu](https://github.com/alexfu)) 113 | 114 | ## [0.1.4](https://github.com/alexfu/androidautoversion/tree/0.1.4) (2016-06-13) 115 | 116 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.1.3...0.1.4) 117 | 118 | ## [0.1.3](https://github.com/alexfu/androidautoversion/tree/0.1.3) (2016-03-18) 119 | 120 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.1.2...0.1.3) 121 | 122 | **Fixed bugs:** 123 | 124 | - Fix Unsopported major.minor version 52.0 error [\#4](https://github.com/alexfu/androidautoversion/pull/4) ([alexfu](https://github.com/alexfu)) 125 | 126 | **Merged pull requests:** 127 | 128 | - Added code syntax highlighting to Readme [\#2](https://github.com/alexfu/androidautoversion/pull/2) ([ravidsrk](https://github.com/ravidsrk)) 129 | 130 | ## [0.1.2](https://github.com/alexfu/androidautoversion/tree/0.1.2) (2016-03-02) 131 | 132 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.1.1...0.1.2) 133 | 134 | ## [0.1.1](https://github.com/alexfu/androidautoversion/tree/0.1.1) (2016-02-24) 135 | 136 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/0.1...0.1.1) 137 | 138 | ## [0.1](https://github.com/alexfu/androidautoversion/tree/0.1) (2016-02-24) 139 | 140 | [Full Changelog](https://github.com/alexfu/androidautoversion/compare/7b6835ad8e8bc86f46a05ce39a91f7ae0214657a...0.1) 141 | 142 | 143 | 144 | \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)* 145 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Alex Fu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidAutoVersion 2 | This is a Gradle plugin, for Android developers, that automates app versioning. 3 | 4 | ``` 5 | AndroidAutoVersion tasks 6 | ------------------------ 7 | bumpMajor - Increases major version by 1, zeroes out minor and patch version 8 | bumpMinor - Increases minor version by 1 and zeroes out patch version 9 | bumpPatch - Increases patch version by 1 10 | versionMajor - Executes bumpMajor and commits the changes to git 11 | versionMinor - Executes bumpMinor and commits the changes to git 12 | versionPatch - Executes bumpPatch and commits the changes to git 13 | ``` 14 | 15 | # Features 16 | 17 | - Update app version from CLI 18 | - Automatically commit version update to git 19 | - Automatically creates git tag 20 | 21 | # Requirements 22 | 23 | - Android Gradle Plugin 3.0+ 24 | - Gradle 4.0+ 25 | - Git 26 | 27 | # Installation 28 | 29 | ## Step 1 30 | Include the following in your top-level `build.gradle` file: 31 | 32 | ```groovy 33 | buildscript { 34 | repositories { 35 | maven { url 'https://jitpack.io' } 36 | } 37 | dependencies { 38 | classpath 'com.github.alexfu:androidautoversion:$latest_version' 39 | } 40 | } 41 | ``` 42 | 43 | Change `$latest_version` to the latest release version found [here](https://github.com/alexfu/androidautoversion/releases). 44 | 45 | ## Step 2 46 | Include the following in your app-level `build.gradle` file: 47 | 48 | ```groovy 49 | apply plugin: 'com.github.alexfu.androidautoversion' 50 | ``` 51 | 52 | ## Step 3 53 | Replace `versionCode` and `versionName`: 54 | 55 | ```groovy 56 | defaultConfig { 57 | versionName androidAutoVersion.versionName 58 | versionCode androidAutoVersion.versionCode 59 | } 60 | ``` 61 | 62 | # Usage 63 | When building your project for the first time with this plugin, you should notice a new file added to your project: `[module name]/version`. This is called a version file. You should check this file into version control (i.e. git) since this file will contain the current version information. 64 | 65 | If you're adding AndroidAutoVersion to an already existing project you need to populate the version file's `major`, `minor`, `patch`, and `buildNumber` fields with what was in the `versionName` and `versionCode` fields. 66 | 67 | For example, a project with the following `app/build.gradle` file: 68 | 69 | ```groovy 70 | defaultConfig { 71 | versionName "2.0.5" 72 | versionCode 9 73 | } 74 | ``` 75 | 76 | Should update the version file to look like so: 77 | 78 | ```json 79 | {"major":2,"minor":0,"patch":5,"buildNumber":9} 80 | ``` 81 | 82 | Every time you want to make a release, decide if it's a major, minor, or a patch. If you're not sure, check out the rules outlined [here](http://semver.org/) to make your decision. Then, once you've decided, run one of the following gradle tasks: 83 | 84 | - `./gradlew bumpPatch` 85 | - `./gradlew bumpMinor` 86 | - `./gradlew bumpMajor` 87 | 88 | Running one of these will update the version, but it will be up to you to commit any changes to version control. 89 | 90 | If you use `git` (which you should), the plugin also has tasks that will execute the corresponding bump task, commit the version update, and create the necessary version tag. 91 | 92 | - `./gradlew versionPatch` 93 | - `./gradlew versionMinor` 94 | - `./gradlew versionMajor` 95 | 96 | ## Example 97 | 98 | See this repo for an example / demo of this plugin: https://github.com/alexfu/androidautoversiondemo 99 | 100 | # Tips 101 | 102 | ## Alpha/Beta 103 | If you have alpha/beta versions of your app and want to signify that in your version, i.e. `1.2.3.alpha`, then you can use the `versionNameSuffix` property in your alpha/beta product flavors. For example: 104 | 105 | ```gradle 106 | android { 107 | productFlavors { 108 | alpha { 109 | versionNameSuffix ".alpha" 110 | } 111 | 112 | beta { 113 | versionNameSuffix ".beta" 114 | } 115 | } 116 | } 117 | ``` 118 | 119 | ## Automate release workflow 120 | Because this plugin allows you to update your app version from the command line, you can completely automate your entire release workflow with a simple script. 121 | 122 | ```bash 123 | $ ./gradlew clean versionPatch assembleRelease 124 | ``` 125 | 126 | # Migrating from v1 127 | If you're updating this plugin from version 1, read the [migration guide](https://github.com/alexfu/androidautoversion/wiki/Migration-Guide). 128 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | maven { 4 | url = uri("https://plugins.gradle.org/m2/") 5 | } 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("com.gradle.publish:plugin-publish-plugin:0.9.2") 10 | } 11 | } 12 | 13 | plugins { 14 | kotlin("jvm") version "1.3.61" 15 | kotlin("plugin.serialization") version "1.3.61" 16 | `kotlin-dsl` 17 | `maven-publish` 18 | } 19 | 20 | group = "com.github.alexfu" 21 | version = "3.3.0" 22 | 23 | repositories { 24 | mavenCentral() 25 | maven { 26 | url = uri("https://dl.google.com/dl/android/maven2") 27 | } 28 | jcenter() 29 | } 30 | 31 | dependencies { 32 | implementation(gradleApi()) 33 | implementation(kotlin("stdlib")) 34 | implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0") 35 | implementation("com.android.tools.build:gradle:3.4.0") 36 | } 37 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexfu/androidautoversion/a563639fcb3bee785d09e670b128d526ad6a3dab/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Sep 06 12:12:44 EDT 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/kotlin/com/github/alexfu/AndroidAutoVersionExtension.kt: -------------------------------------------------------------------------------- 1 | package com.github.alexfu 2 | 3 | open class AndroidAutoVersionExtension(val versionName: String, val versionCode: Int) 4 | -------------------------------------------------------------------------------- /src/main/kotlin/com/github/alexfu/AndroidAutoVersionPlugin.kt: -------------------------------------------------------------------------------- 1 | package com.github.alexfu 2 | 3 | import com.android.build.gradle.AppPlugin 4 | import com.android.build.gradle.LibraryPlugin 5 | import com.github.alexfu.VersionType.* 6 | import kotlinx.serialization.json.Json 7 | import kotlinx.serialization.json.JsonConfiguration 8 | import org.gradle.api.Plugin 9 | import org.gradle.api.Project 10 | import org.gradle.api.Task 11 | import org.gradle.api.tasks.TaskProvider 12 | import java.io.File 13 | 14 | private typealias VersionFile = File 15 | private const val LOG_TAG = "[AndroidAutoVersion]" 16 | 17 | class AndroidAutoVersionPlugin : Plugin { 18 | private lateinit var project: Project 19 | private lateinit var version: Version 20 | private lateinit var versionFile: VersionFile 21 | private val json by lazy { Json(JsonConfiguration.Stable) } 22 | 23 | override fun apply(project: Project) { 24 | this.project = project 25 | val isAppPlugin = project.plugins.findPlugin(AppPlugin::class.java) != null 26 | val isLibraryPlugin = project.plugins.findPlugin(LibraryPlugin::class.java) != null 27 | check(!isAppPlugin || !isLibraryPlugin) { 28 | "'com.android.application' or 'com.android.library' plugin required." 29 | } 30 | setUp() 31 | createTasks() 32 | } 33 | 34 | private fun setUp() { 35 | versionFile = project.file("version") 36 | if (!versionFile.exists()) { 37 | info("Version file does not exist, auto generating a default one.") 38 | Version().writeTo(versionFile) 39 | } 40 | version = versionFile.read() 41 | project.extensions.create("androidAutoVersion", AndroidAutoVersionExtension::class.java, version.versionName, version.buildNumber) 42 | } 43 | 44 | private fun createTasks() { 45 | registerTask( 46 | name = "bumpBuild", 47 | description = "Increases build number by 1", 48 | exec = { incrementVersion(BUILD) } 49 | ) 50 | 51 | val bumpPatchTask = registerTask( 52 | name = "bumpPatch", 53 | description = "Increases patch version by 1", 54 | exec = { incrementVersion(PATCH) } 55 | ) 56 | 57 | val bumpMinorTask = registerTask( 58 | name = "bumpMinor", 59 | description = "Increases minor version by 1 and zeroes out patch version", 60 | exec = { incrementVersion(MINOR) } 61 | ) 62 | 63 | val bumpMajorTask = registerTask( 64 | name = "bumpMajor", 65 | description = "Increases major version by 1, zeroes out minor and patch version", 66 | exec = { incrementVersion(MAJOR) } 67 | ) 68 | 69 | registerTask( 70 | name = "versionPatch", 71 | description = "Executes bumpPatch and commits the changes to git", 72 | dependencies = listOf(bumpPatchTask), 73 | exec = ::commitToGit 74 | ) 75 | 76 | registerTask( 77 | name = "versionMinor", 78 | description = "Executes bumpMinor and commits the changes to git", 79 | dependencies = listOf(bumpMinorTask), 80 | exec = ::commitToGit 81 | ) 82 | 83 | registerTask( 84 | name = "versionMajor", 85 | description = "Executes bumpMajor and commits the changes to git", 86 | dependencies = listOf(bumpMajorTask), 87 | exec = ::commitToGit 88 | ) 89 | } 90 | 91 | private fun incrementVersion(type: VersionType) { 92 | version = version.increment(type) 93 | version.writeTo(versionFile) 94 | } 95 | 96 | private fun commitToGit() { 97 | project.exec { setCommandLine("git", "add", versionFile.absolutePath) } 98 | project.exec { setCommandLine("git", "commit", "-m", "Update to $version") } 99 | project.exec { setCommandLine("git", "tag", "v$version") } 100 | } 101 | 102 | private fun registerTask(name: String, description: String, dependencies: Iterable>? = null, exec: () -> Unit): TaskProvider { 103 | return project.tasks.register(name) { 104 | group = "AndroidAutoVersion" 105 | setDescription(description) 106 | if (dependencies != null) { 107 | setDependsOn(dependencies) 108 | } 109 | doLast { exec() } 110 | } 111 | } 112 | 113 | private fun warn(message: String, error: Throwable?) { 114 | if (error == null) { 115 | project.logger.warn("$LOG_TAG $message") 116 | } else { 117 | project.logger.warn("$LOG_TAG $message", error) 118 | } 119 | } 120 | 121 | private fun info(message: String) { 122 | project.logger.info("$LOG_TAG $message") 123 | } 124 | 125 | private fun VersionFile.read(): Version { 126 | if (!exists()) return Version() 127 | return try { 128 | json.parse(Version.serializer(), readText()) 129 | } catch (error: Error) { 130 | warn("Error reading version file, falling back to default", error) 131 | Version() 132 | } 133 | } 134 | 135 | private fun Version.writeTo(file: VersionFile) { 136 | file.writeText(json.stringify(Version.serializer(), this)) 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/main/kotlin/com/github/alexfu/Version.kt: -------------------------------------------------------------------------------- 1 | package com.github.alexfu 2 | 3 | import kotlinx.serialization.Serializable 4 | 5 | @Serializable 6 | data class Version( 7 | val buildNumber: Int = 1, 8 | val patch: Int = 0, 9 | val minor: Int = 0, 10 | val major: Int = 0 11 | ) { 12 | val versionName: String 13 | get() = "$major.$minor.$patch" 14 | 15 | fun increment(type: VersionType): Version { 16 | return when (type) { 17 | VersionType.MAJOR -> { 18 | copy(buildNumber = buildNumber + 1, patch = 0, minor = 0, major = major + 1) 19 | } 20 | VersionType.MINOR -> { 21 | copy(buildNumber = buildNumber + 1, patch = 0, minor = minor + 1) 22 | } 23 | VersionType.PATCH -> { 24 | copy(buildNumber = buildNumber + 1, patch = patch + 1) 25 | } 26 | VersionType.BUILD -> { 27 | copy(buildNumber = buildNumber + 1) 28 | } 29 | } 30 | } 31 | 32 | override fun toString(): String { 33 | return versionName 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/kotlin/com/github/alexfu/VersionType.kt: -------------------------------------------------------------------------------- 1 | package com.github.alexfu 2 | 3 | enum class VersionType { 4 | MAJOR, MINOR, PATCH, BUILD 5 | } 6 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/gradle-plugins/com.github.alexfu.androidautoversion.properties: -------------------------------------------------------------------------------- 1 | implementation-class=com.github.alexfu.AndroidAutoVersionPlugin 2 | --------------------------------------------------------------------------------