lol
Android
There are 100 posts filed in Android (this is page 2 of 20).
Copy the content of an input stream to an output stream
How to copy the content of an input stream to an output stream
/** * Copy the content of the input stream into the output stream, using a temporary * byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}. * * @param in The input stream to copy from. * @param out The output stream to copy to. * * @throws IOException If any error occurs during the copy. */ private static final int IO_BUFFER_SIZE = 4 * 1024; private static void copy(InputStream in, OutputStream out) throws IOException { byte[] b = new byte[IO_BUFFER_SIZE]; int read; while ((read = in.read(b)) != -1) { out.write(b, 0, read); } }
Closing streams like a boss
This is one of my favorite tips and I also consider it a good practice.
How to close any java input or output Stream.
/** * Closes the specified stream. * * @param stream The stream to close. */ private static void closeStream(Closeable stream) { if (stream != null) { try { stream.close(); } catch (IOException e) { Log.e("IO", "Could not close stream", e); } }
Android Proguard stacktrace example
So, how an obfuscated stacktrace from an application shrunk by Proguard is like?
Take a look at the following examples:
Stacktrace with out Proguard:
E java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pinapps.greekandroidapps/com.pinapps.greekandroidapps.Splash}: java.lang.NullPointerException ... E Caused by: java.lang.NullPointerException E at com.pinapps.greekandroidapps.Tools.DataHandler.init(DataHandler.java:58) E at com.pinapps.greekandroidapps.Splash.onCreate(Splash.java:81) E at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) E at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
Stacktrace with standard Proguard configuration:
E java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pinapps.greekandroidapps/com.pinapps.greekandroidapps.Splash}: java.lang.NullPointerException ... E Caused by: java.lang.NullPointerException E at com.pinapps.greekandroidapps.Tools.c.c(Unknown Source) E at com.pinapps.greekandroidapps.Splash.onCreate(Unknown Source) E at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) E at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
Stacktrace with some options for useful obfuscated stacktraces Proguard configuration:
E java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pinapps.greekandroidapps/com.pinapps.greekandroidapps.Splash}: java.lang.NullPointerException ... E Caused by: java.lang.NullPointerException E at com.pinapps.greekandroidapps.Tools.c.c(SourceFile:57) E at com.pinapps.greekandroidapps.Splash.onCreate(SourceFile:81) E at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) E at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
-printmapping out.map
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
Android Proguard Error [Fixed]
When using proguard with your Android project, you may face the following proguard error:
Proguard returned with error code 1. See console Unexpected error while evaluating instruction: Class = [org/jsoup/parser/TreeBuilder] Method = [insertInFosterParent(Lorg/jsoup/nodes/Node;)V] Instruction = [70] aload_2 v2 Exception = (null) Unexpected error while performing partial evaluation: Class = [org/jsoup/parser/TreeBuilder] Method = [insertInFosterParent(Lorg/jsoup/nodes/Node;)V] Exception = (null) java.lang.NullPointerException at proguard.evaluation.Variables.aload(Variables.java:264) at proguard.evaluation.Processor.visitVariableInstruction(Processor.java:677) at proguard.classfile.instruction.VariableInstruction.accept(VariableInstruction.java:306) at proguard.optimize.evaluation.PartialEvaluator.evaluateSingleInstructionBlock(PartialEvaluator.java:729) at proguard.optimize.evaluation.PartialEvaluator.evaluateInstructionBlock(PartialEvaluator.java:575) at proguard.optimize.evaluation.PartialEvaluator.evaluateInstructionBlockAndExceptionHandlers(PartialEvaluator.java:533) at proguard.optimize.evaluation.PartialEvaluator.visitCodeAttribute0(PartialEvaluator.java:221) at proguard.optimize.evaluation.PartialEvaluator.visitCodeAttribute(PartialEvaluator.java:180) at proguard.optimize.evaluation.LivenessAnalyzer.visitCodeAttribute(LivenessAnalyzer.java:195) at proguard.optimize.evaluation.VariableOptimizer.visitCodeAttribute(VariableOptimizer.java:102) at proguard.classfile.attribute.CodeAttribute.accept(CodeAttribute.java:101) at proguard.classfile.ProgramMethod.attributesAccept(ProgramMethod.java:79) at proguard.classfile.attribute.visitor.AllAttributeVisitor.visitProgramMember(AllAttributeVisitor.java:95) at proguard.classfile.util.SimplifiedVisitor.visitProgramMethod(SimplifiedVisitor.java:91) at proguard.classfile.ProgramMethod.accept(ProgramMethod.java:71) at proguard.classfile.ProgramClass.methodsAccept(ProgramClass.java:439) at proguard.classfile.visitor.AllMethodVisitor.visitProgramClass(AllMethodVisitor.java:47) at proguard.classfile.ProgramClass.accept(ProgramClass.java:281) at proguard.classfile.ClassPool.classesAccept(ClassPool.java:114) at proguard.optimize.Optimizer.execute(Optimizer.java:764) at proguard.ProGuard.optimize(ProGuard.java:325) at proguard.ProGuard.execute(ProGuard.java:114) at proguard.ProGuard.main(ProGuard.java:499)
Fortunately, this error has been fixed at the latest proguard release (4.7), but the Android SDK still uses a previous version.
So all you have to do, it to download the latest proguard version from here, unzip it and copy paste the 3 jars from the “lib” folder to your Android-SDK folder at ANDROID_SDK_HOME/tools/proguard/lib and replace the old jars.
Backing up is always a smart and proper move.