Skip to content

Commit

Permalink
Merge pull request #25 from hannesa2/AutomaticVersionName
Browse files Browse the repository at this point in the history
Automatic version name/code
  • Loading branch information
hannesa2 authored Nov 16, 2020
2 parents 4683586 + 2f28303 commit 16413a3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions BasicExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "paho.mqtt.java.example"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionCode getGitCommitCount()
versionName getTag()
}

useLibrary 'android.test.runner'
Expand Down
30 changes: 30 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,33 @@ allprojects {
}

}


@SuppressWarnings('unused')
static def getTag() {
def tagVersion = "$System.env.VERSION"
if (tagVersion == "null") {
// with local un-commited changes a -DIRTY is added
def processChanges = "git diff-index --name-only HEAD --".execute()
def dirty = ""
if (!processChanges.text.toString().trim().isEmpty())
dirty = "-DIRTY"

def process = "git describe --tags".execute()
tagVersion = process.text.toString().trim() + dirty
} else {
def tagVersionToken = tagVersion.split("/")
if (tagVersionToken.size() > 2)
tagVersion = tagVersionToken[2]
else
tagVersion = tagVersionToken[0]
}
return tagVersion
}

@SuppressWarnings('unused')
static def getGitCommitCount() {
def process = "git rev-list HEAD --count".execute()
return process.text.toInteger()
}

4 changes: 2 additions & 2 deletions org.eclipse.paho.android.sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId 'org.eclipse.paho.android.sample'
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "0.1"
versionCode getGitCommitCount()
versionName getTag()
}

useLibrary 'android.test.runner'
Expand Down
4 changes: 4 additions & 0 deletions org.eclipse.paho.android.service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ android {
minSdkVersion 14
targetSdkVersion 30

versionCode getGitCommitCount()
// Android Studio 4.1 doesn't generate versionName in libraries any more
buildConfigField "String", 'VERSION_NAME', "\"" + getTag() + "\""

testApplicationId "org.eclipse.paho.android.service.test"
}

Expand Down

0 comments on commit 16413a3

Please sign in to comment.