1

Android Studio 2.0 Preview 2, Gradle Wrapper 2.8, Mac OS X

-MainProjectWorkspace    
-|-build.gradle    
-|-settings.gradle    
-|-gradle.properties    
-|-gradle    
-|-MyLibraryDependencies    
-|-MyMainModule   
--|-build.gradle    
--|-build    
--|-src    
---|-androidTest    
---|-main    
----|-assets    
----|-jniLibs    
----|-libs    
----|-java    
-----|-com    
----|-res    
----|-AndroidManifest.xml

MyMainModule/build.gradle

//Not a single SourceSets configurations.

productFlavors {
    flavor1 {
    }
    flavor2 {
    }
}    

buildTypes {
    release {
    }
    debug {
    }
}

A genius developer left System.out.println statements, instead of Log statements in several hundreds of Java source-files in 'src/main/java'. Ideally, we do not want Sysout statements getting bundled with either of the applicationVariants, specially flavor1Release and flavor2Release.

Before we make amends to those hundreds of Java source-files and eventually switch the Sysout statements to Log statements, we would need to turn them off urgently.

Two possible solutions -

  1. Execute a simple script that will remove all the Sysout statements in the Java source-files in 'src/main/java'. But about that, variants flavor1Debug and flavor2Debug need the Loggers displaying what's going on in the App.
  2. Execute a simple Gradle task at build-time, copy Java source-files from 'src/main/java' to 'src/release/java', ensuring Sysout statements are omitted.

Solution 2 appears to be quickest, easiest, elegant. Particularly when the Gradle Custom-task is executed independently. Except for that, Gradle-sync in Android-Studio seems to be copying everything from 'src/main' to 'src/release' including assets, jniLibs, libs, res and even the AndroidManifest.xml. Fortunately, the Java source-files are ripped-off the Sysout statements in 'src/release', which is the expected result, and ideally, only 'src/release/java' should remain without any other folders.

task prepareRelease(type: Task) {
Pattern sysoutRegex = Pattern.compile(<Sysout-Pattern>)
try {
    File releaseFolder = file("src/release")
    File mainFolder = file("src/main")
    ByteArrayOutputStream output = new ByteArrayOutputStream()
    exec {
        commandLine = "sh"
        args = ["-c", "find ${mainFolder.canonicalPath} -name '*' -type f -print ",
                "| xargs egrep -l '${sysoutRegex.pattern()}'"]
        standardOutput = output
    }
    def fileList = output.toString().split(System.getProperty("line.separator"))
    fileList.each { line ->
        File srcFile = file(line)
        File destFile = file(srcFile.canonicalPath.replace("main", "release"))

        def content = srcFile.readLines()
        if (!destFile.exists()) {
                destFile.parentFile.mkdirs()
                destFile.createNewFile()
                destFile.writable = true
         }
         destFile.withWriter { out ->
            content.eachWithIndex { contentLine, index ->
                    if (!sysoutRegex.matcher(contentLine).find()) {
                        out.println contentLine
                    }
            }
        } 
    } catch (Exception fail) {
        throw fail
    }
}

There is nothing in the custom Gradle-task that may cause this error of making "src/release" an exact copy of "src/main", which was not intended to be.

Any pointers to prevent this default copy of "src/main" to "src/release" will be greatly appreciated.

AndroidRocks
  • 292
  • 4
  • 16
  • How about redirecting S.O.P to log? http://stackoverflow.com/questions/1200175/log4j-redirect-stdout-to-dailyrollingfileappender – RaGe Dec 11 '15 at 20:44
  • http://stackoverflow.com/questions/5712764/redirect-system-out-println-to-log4j-while-keeping-class-name-information – RaGe Dec 11 '15 at 20:44
  • http://bonfab.io/2015/09/redirect-system-out-to-logger/ – RaGe Dec 11 '15 at 20:45
  • For a Large Multi-national Corporation, the Android App is subjected to rigorous Security Clearance procedures, particularly the APK files, and not just the source-code repositories by different Security Enforcing Teams. The Client-data in the App could potentially all add-up to a couple Trillions of Dollars. Any security vulnerability can cause a huge loss to Trading Markets across the globe. If Security Teams say, they don't want to see Sysout statements, nobody can argue with that. We'd better rip them all off, rather than redirecting to the default Logger. – AndroidRocks Dec 11 '15 at 20:56
  • How about using `*.java` as your `find` pattern instead of `*` ? – RaGe Dec 11 '15 at 21:24

1 Answers1

0

Based off RaGe's comment - "How about using *.java as your find pattern instead of * ?"

Kudos. I was not supposed to break the "find | xargs egrep " before the '|' into two separate args in the args[] in the exec task. Indeed, a Genius!!!

AndroidRocks
  • 292
  • 4
  • 16