Article Image
Article Image
read

A common requirement when developing Android apps is the possibility to have multiple versions of the app installed. For example, when your app communicates with a backend server you might want to have a version that points to your local, test and production server. That has the advantage, that you can easily switch between those versions during testing and also keep app specific settings in place.

So, how can we accomplish that with Qt?

manage multiple android versions on your phone

The Qt way

First we need to create a new variable in our Qt pro file. You can choose whatever name you want; I will use _EXECUTIONTARGET_. This variable holds the target (local, test, production) that we need to change before triggering a new build.

#LOCAL, TEST, PRODUCTION
_EXECUTIONTARGET_ = "TEST"

After that, we are adding the following to the android section of our Qt pro file (make sure that you put that in the android section of your Qt pro file, so that it only gets executed when we are building for android). Furthermore you probably want to adapt the package identifier MY_PACKAGE_IDENTIFIER and the app name MY_APP_NAME in the code snippet below ;-)

android{
	QT_varfile = ""

	contains(_EXECUTIONTARGET_, LOCAL){
	    QT_varfile += "MY_PACKAGE_IDENTIFIER=com.gympulsr.gympulsr.local"
	    QT_varfile += "MY_APP_NAME=gympulsr (LOCAL)"
	}

	contains(_EXECUTIONTARGET_, TEST){
	    QT_varfile += "MY_PACKAGE_IDENTIFIER=com.gympulsr.gympulsr.test"
	    QT_varfile += "MY_APP_NAME=gympulsr (TEST)"
	}

	contains(_EXECUTIONTARGET_, PRODUCTION){
	    QT_varfile += "MY_PACKAGE_IDENTIFIER=com.gympulsr.gympulsr"
	    QT_varfile += "MY_APP_NAME=gympulsr"
	}

	message($$absolute_path("VARIABLES.txt", $$OUT_PWD))
	write_file($$absolute_path("VARIABLES.txt", $$OUT_PWD), QT_varfile)
}

What the above code does, is that it writes the package identifier and the app name to a file called VARIABLES.txt. The VARIABLES.txt file is written to the Android build directory and gets read later on by the Android build system when the apk gets created.

The next thing we need to do, is to adapt our build.gradle file and add the following to the top of the file:

def readVarFile() {
    File varFile = file('../VARIABLES.txt')
    def qtVars = [:]
    varFile.eachLine { line ->
        if (line.trim()) {
          def eq = line.indexOf('=')
          if (eq > 0) {
              def key = line.substring(0, eq)
              def value = line.substring(eq+1)
              qtVars."$key" = value
          }
        }
    }
    return qtVars
}

def QtApp = readVarFile() 

This code snippet reads the VARIABLES.txt file (that gets generated by qmake) from the build directory and stores the key/value pairs in the variable QtApp.

Now, add the following to the defaultConfig section of your build.gradle file:

defaultConfig {
	applicationId QtApp.MY_PACKAGE_IDENTIFIER
	buildConfigField "String", MY_APP_NAME, QtApp.MY_APP_NAME)

	def addConstant = {constantName, constantValue ->
		buildConfigField "String", "${constantName}", "\"${constantValue}\""
	}

	manifestPlaceholders = ["MY_APP_NAME" : QtApp.MY_APP_NAME]
}

This sets the applicationId and exposes the variable MY_APP_NAME to the AndroidManifest.xml.

The last step that’s now needed is to adapt the AndroidManifest.xml to use MY_APP_NAME. Therefore, modify the android:label in the application and the main activity section in the following way:

android:label="${MY_APP_NAME}"

That’s it. You can now easily change the target by changing the value of the variable _EXECUTIONTARGET_. Don’t forget to invoke qmake afterwards to make sure, that the changes are getting processed. Have fun with it and stay tuned for the next Qt related article.

About gympulsr

gympulsr is a fun community for fitness enthusiasts to connect with like minded people around the world. Mark your gym on a map to connect with people that are working out nearby. Post motivating pictures, show people your gym transformation or just write about your last workout.

Wanna give it a try? Download gympulsr from the Apple or Google Play Store and say hello.

Want to read more about Qt related stuff?
Subscribe now!
Blog Logo

Bernhard


Published

Image

Gympulsr - Blog

All about Gympulsr

Back to Overview