GithubHelp home page GithubHelp logo

chenguandong / android-nologs-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from roottony/android-nologs-plugin

0.0 1.0 0.0 192 KB

Removes logs from android application built with Gradle & Android New Build System

Groovy 100.00%

android-nologs-plugin's Introduction

NoLogs plugin

Removes logs completely from project before compillation, then brings them back.

Plugin is designed for Android apps that are built with Gradle and Android New Build System.

Why do I need this?

Release builds of Android applications should not contain any debug logs.

One of the approaches of removing Android logs in production is the following

if (BuildConfig.DEBUG) {
    Log.d("TAG", "Some log");
} 

This works pretty well but you must always add an extra if statement.

Another approach if to remove logs with the following Proguard rule:

-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

This removes simple logs completely, but converts more complex logs (assuming width and height are fields of a class)

Log.d("TAG", "width: " + width + "; height: " + height);

to something like

new StringBuilder("width: ").append(this.b).append("; height: ").append(this.c);

So, Log methods are removed, but the strings are left in the code.

Setup

Apply plugin:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // apply android gradle plugin here
        
        // Version may change, see the latest version on jcenter
        classpath 'com.roottony.gradle:nologs:0.9'
    }
}
apply plugin: 'com.roottony.nologs'

Note that this plugin must be applied after

apply plugin 'com.android.application' 

or

apply plugin 'com.android.library'

Set what to remove

To remove calls of all android.util.Log methods, add this to build.gradle

nologs {
    logClass = 'Log'
}

If you have your own logging class, add it instead

nologs {
    logClass = 'MyLoggingClass'
}

Select whether logs should be removed for particular variant with the following closure

nologs {
    shouldRemoveLogs = { variant ->
        return variant.buildType.name == 'release'
    }
}

Other options

Disable plugin completely with

nologs {
    enabled = false
}

Disable uncommenting logs (useful to understand what's happeing under the hood)

nologs {
    disableUncomment = true
}

How does it work?

No magic, just a regexp.

Before compillation occurs, all java source files are processes with the regexp

'^\ *' + logClass + '\.[\s\S]+?(?=\)\ *;\ *$)\)\ *\;'

After compilation completes, java source files are processed with another regexp

'\ *' + logClass + '\.[\s\S]+?(?=\)\ *;\ *)\)\ *\;'

where logClass is the name of the class you specified.

See the details in the source code.

Is it safe?

Well, almost :)

Dangerous code constructions are:

if (someCondition)
    MyLogClass.d("Some log");
    
doSomethingElse();

as doSomethingElse() call will be removed. Note that

if (someCondition) MyLogClass.d("Some log");
    
doSomethingElse();

is OK as if (someCondition) MyLogClass.d("Some log"); line will not be touched at all.

In general, a log will not be removed if it is not the first statement in the line:

doSomethingElse(); MyLogClass.d("Some log");

android-nologs-plugin's People

Contributors

roottony avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.