Gradle – Automatically rename the .apk file and copy the ProGuard mapping.txt file

ProGuard writes the mapping file under the build directory of the project. Under normal circumstances all files and directories under the build dirs are excluded from version control. That is a bit of an issue if you want to be able to save that file with every release build. I always forget to this manually. So, let’s do that at build time with Gradle and while we are at it, we might as well rename our apk file, so we don’t have to do that manually either.

Here is the relevant code to put in our build.gradle file:

import java.util.regex.Matcher
import java.util.regex.Pattern

buildTypes {
        release {
        debuggable false
        minifyEnabled true
        proguardFiles 'proguard.cfg'

        // Rename the apk file and copy the ProGuard mapping file to the root of the project
        applicationVariants.all { variant ->
            if (variant.getBuildType().name.equals("release")) {
                def formattedDate = new Date().format('yyyyMMddHHmmss')
                def projectName = ""
                variant.outputs.each { output ->
                    def fullName =
                    projectName = fullName.substring(0, fullName.indexOf('-'))
                    // ${} has the value of "paidRelease"
                    output.outputFile = new File((String) output.outputFile.parent, (String)".apk", "-v${variant.versionName}-${formattedDate}.apk"))
                def mappingFile = "${rootDir}/${projectName}/build/outputs/mapping/${getCurrentFlavor()}/release/mapping.txt"
                println("mappingFile:  ${mappingFile}")
                if (variant.getBuildType().isMinifyEnabled()) {
                    variant.assemble.doLast {
                        copy {
                            from "${mappingFile}"
                            into "${rootDir}"
                            rename { String fileName ->

    debug {
        debuggable true

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern;

    if( tskReqStr.contains( "assemble" ) )
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher( tskReqStr )

    if( matcher.find() )
    else {
        println "NO MATCH FOUND"
        return "";

The Android SDK Manager fails to fetch the platform and tools list from Google

If you get the following error message when launching the Android SDK Manager:

Failed to fetch URL, reason: File not found

most likely the owner/permissions are not set correctly for the ~/.android directory.

This error message is very confusing and it took me while to find the issue.

In my case the owner of this directory and all the files and directories in it was root.

There are numerous “solutions” given to this in many forums. Most of them recommend launching Android Studio as root. That is definitely the wrong approach. It will get rid of the error, since you will no longer be faced with permissions issues, but running Android Studio as root is a vary bad idea.

Instead, you should change the owner of the ~/.android directory to your user id.

So, find the .android directory (most likely it is in your home directory). Then change the ownership recursively to your user:

cd ~
sudo chown -R myuser: .android/

You should be now good to go.

Tags: , , ,

Find out what DNS requests are made in real time via command line

Since all DNS requests are over port 53, we can use the raw tcpdump utility and just look at the packets on that port:

tcpdump -vvv -s 0 -l -n port 53

Someone took the extra step to write a php script to massage that data, so that it is easier to read as well as focusing on those queries that either do not resolve or take a long time to resolve:

Tags: , , ,

Tuesday, May 10th, 2016 IT Security, Linux, Networking, Ubuntu No Comments

Android Studio- What files and directories to exclude when importing into Subversion (or Git)

List of directories and files to be excluded when importing into a version control (relative to the root of the project):

.gradle (directory)
build (directory) (file)
app/build (directory)
.idea/libraries (directory)
.idea/gradle.xml (file)
.idea/workspace.xml (file)

There are a couple of different ways to have the above directories and files not present in the version control:

1. Exclude them from the initial import
2. Delete them after the import

svn delete -m "Delete .gradle dir" http://server/path/to/repository/.gradle
svn delete -m "Delete build dir" http://server/path/to/repository/build
svn delete -m "Delete app/build dir" http://server/path/to/repository/app/build
svn delete -m "Delete" http://server/path/to/repository/
svn delete -m "Delete .idea/libraries dir" http://server/path/to/repository/.idea/libraries
svn delete -m "Delete gradle.xml" http://server/path/to/repository/.idea/gradle.xml
svn delete -m "Delete workspace.xml" http://server/path/to/repository/.idea/workspace.xml

Now that we have gone through that, the version control does not have these directories and files, but locally, the project on your computer has these (if not they will be automatically generated when you first open the project in Android Studio or you build the project).

That means that we need to delete them from svn locally and tell svn locally to ignore them:

svn ps svn:ignore '.gradle build' .
svn ps svn:ignore build app
svn ps svn:ignore 'libraries gradle.xml workspace.xml' .idea

Tags: , , , ,

Friday, April 22nd, 2016 Android, Android Studio, Java, Programming 1 Comment

Set up a MySQL user dedicated to backups

It is not a good idea to use the root MySQL account to do backups.

So, let’s create a user dedicated exclusively to doing backups.

Log on to MySQL as root:

mysql -u root -p

Then create the new user and grant it the necessary permissions:

CREATE USER 'backup_user_name'@'localhost' IDENTIFIED BY 'my_pass';
GRANT SELECT, SHOW VIEW, RELOAD, EVENT, TRIGGER, LOCK TABLES ON *.* TO 'backup_user_name'@'localhost';

Now that we are all set up, you can do backups with that new dedicated user:

mysqldump -u backup_user_name -pmy_pass db_name | gzip > /home/ddarazha/backups/ninelets/ninelets_`date '+%Y%m%d'`.sql.gz

Tags: , ,

Tuesday, January 19th, 2016 IT Security, Linux, MySQL, SQL, Ubuntu No Comments




October 2016
« Jul