Thursday, May 15, 2014

How to detect if application has been updated or installed

In some cases there is a need to know if user has installed or updated your app. For example to put different texts or something else. To do this you may use code below:
static public boolean isUpdated(Context ctx) {
    PackageInfo packageInfo = null;
    try {
            packageInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),
                PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Can't get package info");
        }
    return (packageInfo.firstInstallTime != packageInfo.lastUpdateTime);
}

Wednesday, May 14, 2014

Settings up AndEngine in Android studio


When i decided to use AndEngine in my project I discovered that it doesn't support Android studio and there is not any passable tutorial about how to make it work. So I spent some time and set it up by myself.

Let's assume that you already have created a project and it has default structure. First create folder named third_party in the root directory of the project. Then in third_party directory create subdirectories called andengine and andenginebox2d. I assume that you already downloaded or cloned AndEngine and Box2d extension for it. Put AndEngine and AndEngineBox2d in andengine and andenginebox2d directories respectively. Create file named build.gradle in andengine directory and andenginebox2d directory. Build.gradle files is a file that tells gradle how to build your project.
Next you have to put content in your build.gradle files. In file located in andengine folder put:

apply plugin: 'android-library'

android {
    compileSdkVersion 17
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-project.txt')
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}
By doing so you are telling gradle that minSdkVersion is 14 and targetSdk is 19. Above all I'm using Java 7 in my project so there is
compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
block.

In file located in andenginebox2d folder put such content:
apply plugin: 'android-library'

android {
    compileSdkVersion 17
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-project.txt')
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

dependencies {
    compile project(':third_party:andengine')
}
As you can see it is almost identical to previous except dependencies block. That block tells gradle that andenginebox2d depends on andengine and gradle should include it as a library project.

We are almost done. Next step is to tell gradle that you want it to compile AndEngine with your project.
Open settings.gradle that is located in your project's root directory and add two lines to it:
include ':third_party:andengine'
include ':third_party:andenginebox2d'
Next open build.gradle which locates in app directory and add
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':third_party:andenginebox2d')
    compile project(':third_party:andengine')
to dependencies block. You already know that it means that your application is dependent of andengine project.

The final step is to open AndroidManifest.xml in andegine and andenginebox2d directories and make them look like this:


    
<application /> block is needed because of bug in manifest merging tool.
That's it! Now clean your project and press run. Everything should work just fine.