Version Control

From Android Game Development Wiki

Jump to: navigation, search

[edit] The Problem

Once you have released your game or application to the market, at some point you'll want to make some changes, fix bugs, or add new features. To do this you'll have to create a new version of your application. Once you release a new version the users are occasionally told that some of their apps have newer versions if any of them are like me however they just clear this warning and only download the newer version when they need it, if ever at all. This can lead to having several different versions of your application running on different peoples devices. Which makes it hard to debug if one of them finds a bug.

[edit] The Solution

Enforce version checking within your application. This means simply checking to see that the app is current when the user opens it and prompting them to install the latest and greatest version. To do this you need to set up some place online that you can store the current version number, so that you can check it against the version of the application that is running. You could set up a simple web service to do this, but I found what I believe is a more simple solution. Create a text file that contains only the most recent version of the application and host it online somewhere. I use Dropbox, but any place that you can host it should work fine. Then you just have to set up your app to read the text file and compare the number it gets to its own version, if it finds that it is out of date you can prompt the user to install the new version.

[edit] Sample Code

Here is the method I use to get the versions.

 private void checkVersion(){
	String medString = "https://url/of/your/textfile.txt";
	try {
                //This line will retrieve my VersionCode
		myVersion = Integer.valueOf(getPackageManager().getPackageInfo("com.yourpackagename", 0).versionCode); 
		URL url = new URL(medString);
		InputStream i = (InputStream) url.openStream();
		InputStreamReader isr = new InputStreamReader(i);
		BufferedReader br= new BufferedReader(isr);
		String strVersion = br.readLine(); // Read the current version from the text file online.
		version = Integer.valueOf(strVersion);
         catch (Exception e)
                //Handle any problems here
 }

Since you are reading one of the values from the network you could face time-out issues, so its nice to do this in a separate thread. First we will set up a Handler to take over once our thread has completed.

handler = new Handler() {
        public void handleMessage(Message msg) {  
        	Log.i(myTag,String.valueOf(msg.what));	
        	if (msg.what == 0 && alreadyDone == false){
        		alreadyDone = true;
        		Log.i(myTag, myVersion + "   " + version);
        		if(myVersion < version) {
        			AlertDialog.Builder builder = new AlertDialog.Builder(YourClass.this);
        			builder.setMessage("There is a newer version. Would you like to install it now?")
        			.setCancelable(false)
        			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        				public void onClick(DialogInterface dialog, int id) {
        					Uri uri = Uri.parse("market://details?id=com.yourpackagename");
        					startActivity(new Intent(Intent.ACTION_VIEW, uri));
        					dialog.cancel();
        				}
        			})
                                //You can leave this button out if you don't want to give the users any choice. They will either have to update, or close the app
        			.setNegativeButton("No", new DialogInterface.OnClickListener() { 
        				public void onClick(DialogInterface dialog, int id) {
        					dialog.cancel();
        				}
        			});
        			AlertDialog alertDialog = builder.create();
        			alertDialog.show();
        		}
        	}else if(msg.what == 1 && alreadyDone == false){
            		alreadyDone = true;
            		//It took to long to read from the network so we aborted.
        	}
        }
};

Now we can fire off our separate thread to check the versions

Thread checkUpdate = new Thread() {  
        public void run() {
		handler.sendEmptyMessageDelayed(1, 3500); //If it takes too long just give up for now
		checkVersion();	
		handler.sendEmptyMessage(0);
	}
};
checkUpdate.start();

Now you just have to edit your text file by incrementing the number in it each time you release an update. The next time a user opens your app they will be prompted to download the newest version. If you want to get fancy with it you can set up a fade in / fade out animation with a screen that has your logo on it that can be shown while its doing the work in the background thread. This will make this process seem more seamless to the user.

Personal tools