Compile an Android application with a different Application Name and Package Name

This was tested on Android Studio and with the source code for Telegram.

To change the package name for your build, an easy way would be to add a flavor of the app:

Telegram\TMessagesProj\build.gradle:

Under the section android {, add the following:

productFlavors {
        securechat {
            packageName "com.joel.telmsg"
            versionCode 20
        }

    }

This will take care of changing your package name so that you can install a second version of th same application.

Next, we will take care of the Application name shown in the icon menu:

TMessagesProj/config/release/AndroidManifest.xml:
In the tag, add the following:

xmlns:tools="http://schemas.android.com/tools"

, so that the final code looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.telegram.messenger"
    android:installLocation="auto">
...

Now, in

<manifest>/<application> 

tag, add the following:

tools:replace="android:label"
        android:label="SecureChat"

to replace the existing android:label text.

To change the name of the output .apk file, add the following:

applicationVariants.all { variant ->

        def apk = variant.outputFile;
        def newName;

        newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + ".apk");

        newName = newName
                .replace("-" + variant.buildType.name, "")
                .replace(project.name, "droidzone");

        variant.outputFile = new File(apk.parentFile, newName);
        if (variant.zipAlign) {
            variant.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
        }

        logger.info('INFO: Set outputFile to ' + variant.outputFile + " for [" + variant.name + "]");
    }

to Telegram/TMessagesProj/build.gradle


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.