Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Auto increment build number for all platform (windows, linux and android)
Forum Update on Monday, May 27th 2025

Auto increment build number for all platform (windows, linux and android)

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    liewjls
    wrote on 19 Dec 2016, 10:43 last edited by
    #1

    Hi,

    I have a piece of code which will compile for all 3 platforms (windows, linux and android). I'm trying to keep track all the changes have gone into the each of the platforms. I did google, there's quite a few suggestion using the batch script for windows and bash script for linux. But now i'm stuck for Android version.

    I have a script for window and linux and configured .pro file of the project,

    #This is to auto generate the build number and build timestamp
    build_nr.target = $$PWD/build_number.h
    linux-g++-32{
    build_nr.commands = cd $$PWD; chmod +x build_number.sh; $$PWD/build_number.sh
    }
    win32{
    build_nr.commands = $$PWD/build_number.bat
    }
    
    build_nr.depends = FORCE
    QMAKE_EXTRA_TARGETS += build_nr
    PRE_TARGETDEPS += $$PWD/build_number.h
    }
    

    they are working but either on of it doesn't work when i tried to compile and generate APK (for android) no matter i build on windows or linux. Anyone have any idea how can i do the same thing for the Android platform?

    Thanks.

    Regards,
    jenny

    R J 2 Replies Last reply 20 Dec 2016, 11:33
    2
    • V Offline
      V Offline
      VRonin
      wrote on 19 Dec 2016, 16:48 last edited by
      #2

      Unfortunately I'm unable to help, in fact, I'd like to ask you to share the contents of build_number.bat and build_number.sh

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Paul Colby
        wrote on 19 Dec 2016, 20:43 last edited by Paul Colby
        #3

        Hi @liewjls,

        I haven't done any Qt Android development myself (yet), but looking at qmake's spec files, there's a separate android platform value which you should be able to use scope an Android-specific build-number script, such as:

        android {
            build_nr.commands = <whatever you do to update Android resources>
        }
        

        I hope that helps.

        Cheers.

        1 Reply Last reply
        2
        • L liewjls
          19 Dec 2016, 10:43

          Hi,

          I have a piece of code which will compile for all 3 platforms (windows, linux and android). I'm trying to keep track all the changes have gone into the each of the platforms. I did google, there's quite a few suggestion using the batch script for windows and bash script for linux. But now i'm stuck for Android version.

          I have a script for window and linux and configured .pro file of the project,

          #This is to auto generate the build number and build timestamp
          build_nr.target = $$PWD/build_number.h
          linux-g++-32{
          build_nr.commands = cd $$PWD; chmod +x build_number.sh; $$PWD/build_number.sh
          }
          win32{
          build_nr.commands = $$PWD/build_number.bat
          }
          
          build_nr.depends = FORCE
          QMAKE_EXTRA_TARGETS += build_nr
          PRE_TARGETDEPS += $$PWD/build_number.h
          }
          

          they are working but either on of it doesn't work when i tried to compile and generate APK (for android) no matter i build on windows or linux. Anyone have any idea how can i do the same thing for the Android platform?

          Thanks.

          Regards,
          jenny

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 20 Dec 2016, 11:33 last edited by
          #4

          @liewjls said in Auto increment build number for all platform (windows, linux and android):

          they are working but either on of it doesn't work when i tried to compile and generate APK (for android) no matter i build on windows or linux. Anyone have any idea how can i do the same thing for the Android platform?

          what is the content of the whole pro file?
          I guess you are sharing some output/intermediate folders among the different target build platforms?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • L liewjls
            19 Dec 2016, 10:43

            Hi,

            I have a piece of code which will compile for all 3 platforms (windows, linux and android). I'm trying to keep track all the changes have gone into the each of the platforms. I did google, there's quite a few suggestion using the batch script for windows and bash script for linux. But now i'm stuck for Android version.

            I have a script for window and linux and configured .pro file of the project,

            #This is to auto generate the build number and build timestamp
            build_nr.target = $$PWD/build_number.h
            linux-g++-32{
            build_nr.commands = cd $$PWD; chmod +x build_number.sh; $$PWD/build_number.sh
            }
            win32{
            build_nr.commands = $$PWD/build_number.bat
            }
            
            build_nr.depends = FORCE
            QMAKE_EXTRA_TARGETS += build_nr
            PRE_TARGETDEPS += $$PWD/build_number.h
            }
            

            they are working but either on of it doesn't work when i tried to compile and generate APK (for android) no matter i build on windows or linux. Anyone have any idea how can i do the same thing for the Android platform?

            Thanks.

            Regards,
            jenny

            J Offline
            J Offline
            joeQ
            wrote on 21 Dec 2016, 01:27 last edited by
            #5

            @liewjls
            Hi, friend. welcome.

            I search the Qt help. found the following Q_OS_*macros. maybe it can help you.

            Q_OS_AIX
            Defined on AIX.
            Q_OS_ANDROID
            Defined on Android.
            Q_OS_BSD4
            Defined on Any BSD 4.4 system.
            Q_OS_BSDI
            Defined on BSD/OS.
            Q_OS_CYGWIN
            Defined on Cygwin.
            Q_OS_DARWIN
            Defined on Darwin-based operating systems such as OS X, iOS, watchOS, and tvOS.
            Q_OS_DGUX
            Defined on DG/UX.
            Q_OS_DYNIX
            Defined on DYNIX/ptx.
            Q_OS_FREEBSD
            Defined on FreeBSD.
            Q_OS_HPUX
            Defined on HP-UX.
            Q_OS_HURD
            Defined on GNU Hurd.
            Q_OS_IOS
            Defined on iOS.
            Q_OS_IRIX
            Defined on SGI Irix.
            Q_OS_LINUX
            Defined on Linux.
            Q_OS_LYNX
            Defined on LynxOS.
            Q_OS_NETBSD
            Defined on NetBSD.
            Q_OS_OPENBSD
            Defined on OpenBSD.
            Q_OS_OSF
            Defined on HP Tru64 UNIX.
            Q_OS_OSX
            Defined on OS X.
            Q_OS_QNX
            Defined on QNX Neutrino.
            Q_OS_RELIANT
            Defined on Reliant UNIX.
            Q_OS_SCO
            Defined on SCO OpenServer 5.
            Q_OS_SOLARIS
            Defined on Sun Solaris.
            Q_OS_TVOS
            Defined on tvOS.
            Q_OS_ULTRIX
            Defined on DEC Ultrix.
            Q_OS_UNIX
            Defined on Any UNIX BSD/SYSV system.
            Q_OS_UNIXWARE
            Defined on UnixWare 7, Open UNIX 8.
            Q_OS_WATCHOS
            Defined on watchOS.
            Q_OS_WIN32
            Defined on 32-bit and 64-bit versions of Windows (not on Windows CE).
            Q_OS_WIN64
            Defined on 64-bit versions of Windows.
            Q_OS_WIN
            Defined on all supported versions of Windows. That is, if Q_OS_WIN32, Q_OS_WIN64, Q_OS_WINCE or Q_OS_WINRT is defined.
            Q_OS_WINCE
            Defined on Windows CE.
            Q_OS_WINPHONE
            Defined on Windows Phone 8.
            Q_OS_WINRT
            Defined for Windows Runtime (Windows Store apps) on Windows 8, Windows RT, and Windows Phone 8.
            
            

            Just do it!

            1 Reply Last reply
            0
            • L Offline
              L Offline
              liewjls
              wrote on 4 Jan 2017, 14:49 last edited by liewjls 1 Apr 2017, 14:51
              #6

              Hi all,
              Thanks for all suggestion.

              @raven-worx I actually found out when I use the contains(QMAKE_HOST.os,Windows) in my pro file, it works on all platforms (no matter i build Android on Windows or Linux).

              QT += core
              QT -= gui
              
              CONFIG += c++11
              VER_MAJ=1
              VER_MIN=0
              
              
              QT_varfile = ""
              for(var, $$list($$find($$list($$enumerate_vars()), ^(VERSION*|VER_MAJ*|VER_MIN*|DEFINES*).*$))) {
                  line = $$var "$$eval($$var)"
                  QT_varfile += $$join(line, "=")
              }
              
              write_file($$absolute_path("VARIABLES.txt", $$OUT_PWD), QT_varfile)
              
              
              contains(QMAKE_HOST.os,Windows) {
                  message("Host is windows")
                  build_nr.target = extra
                  build_nr.commands = $$PWD/build_number.bat $$VER_MAJ $$VER_MIN "$$cat($$OUT_PWD/build.txt)"
                  build_nr.depends = FORCE
                  QMAKE_EXTRA_TARGETS += build_nr
                  PRE_TARGETDEPS += extra
              }
              else {
                  message("Host is linux")
                  build_nr.target = extra
                  build_nr.commands = chmod +x $$PWD/build_version.sh; $$PWD/build_version.sh $$VER_MAJ $$VER_MIN
                  build_nr.depends = FORCE
                  QMAKE_EXTRA_TARGETS += build_nr
                  PRE_TARGETDEPS += extra
                  build_nr.target = $$OUT_PWD/version.h
              }
              
              
              TARGET = qtConsole
              CONFIG += console
              CONFIG -= app_bundle
              
              TEMPLATE = app
              
              SOURCES += main.cpp
              
              HEADERS += $$OUT_PWD/build_number.h
              

              But I got a problem now. When i build my APK, the script will run twice. Anyone can help me to resolve this please?

              @VRonin yes, not a problem. I did google around (sorry, i lost track with the links), come out with this

              build_number.sh

              @echo off
              cd /d %dirPath%
              set /a VERSION_BUILD=0
              
              REM Read values from file
              for /f "tokens=*" %%S in (VARIABLES.txt) do set %%S
              echo Script has been run %VERSION_BUILD% time(s)
              
              set /a VERSION_MAJOR=%1
              set /a VERSION_MINOR=%2
              set /a VERSION_BUILD+=1
              
              set VERSION_STR=%VERSION_MAJOR%.%VERSION_MINOR%.%VERSION_BUILD%
              
              REM Write new values to file
              break > VARIABLES.txt
              set VERSION >> VARIABLES.txt
              
              for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
              set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
              
              echo #define TIMESTAMP  "%ldt%" > version.h
              echo #define VER_STR "%VERSION_STR%">> version.h
              

              build_number.sh

              #!/bin/bash
              dir="$PWD"
              versionFile=$dir"/VARIABLES.txt"
              
              . $versionFile
              
              echo "VERSION"$VERSION
              echo "VERSION_BUILD:"$VERSION_BUILD
              echo "VERSION_MAJOR:"$VERSION_MAJOR
              echo "VERSION_MINOR:"$VERSION_MINOR
              echo "VERSION_STR:"$VERSION_STR
              
              VERSION_BUILD=$((VERSION_BUILD+1))
              
              VERSION_MAJOR=$1
              VERSION_MINOR=$2
              VERSION_STR=$VERSION_MAJOR"."$VERSION_MINOR"."$VERSION_BUILD
              
              #Update the new value to the file.
              echo -e "VERSION="$VERSION"\nVERSION_MAJOR="$VERSION_MAJOR"\nVERSION_MINOR="$VERSION_MINOR"\nVERSION_BUILD="$VERSION_BUILD"\nVERSION_STR="$VERSION_STR | tee $versionFile
              
              today=`date +%Y-%m-%d" "%H:%M:%S`
              
              echo -e "#define VERS_STR "\"$VERSION_STR\" "\n#define TIMESTAMP  "\"$today\" | tee version.h
              

              My scripting skills isn't great, any comments to help me improve these 2 scripts that would be great.

              thanks.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved