Is there any way of getting version from build.gradle of a kotlin project?

Viewed 1080

I want to get the version of my project inside my project which I have set in build.gradle. Is there any method to get the version inside my project as I need to show the version inside my software and used for compairing update info, so that I don't need to change twice every time I release a update. Is there any way to make it?

group 'ProjectName.group'
version "ProjectVersion" 

1 Answers

You typically do that by loading a properties file, and configuring gradle to filter your properties file in the processResources task.

Example:

build.gradle:

version = '1.5.0'

processResources {
    def properties = ['version': project.version]
    inputs.properties(properties)
    filesMatching('version.properties') {
        expand(properties)
    }
}

version.properties:

version=${version}

App.java:

public static void main(String[] args) throws IOException {
    Properties properties = new Properties();
    properties.load(App.class.getResourceAsStream("/version.properties"));
    System.out.println(properties.getProperty("version"));
}
Related