Android Java compiler opimization - android

I have code like this:
MyLog.d("TAG", "debug string " + aVariable + " more debug string =" + anotherVariable);
And MyLog class is like
public void d(String tag, String message) {
private static final boolean DEBUG = true;
if (DEBUG) {
Log.d(tag, message);
}
}
My question is if I set DEBUG to false, will android java compiler smartly detect that this line of code
MyLog.d("TAG", "debug string " + aVariable + " more debug string =" + anotherVariable);
does nothing
and it won't create temporary string objects for "debug string " + aVariable + " more debug string =" + anotherVariable

You're doing the string concatenation before anything related to the DEBUG matters: I doubt that would be optimized out by ProGuard, although the call to Log.d inside MyLog.d would disappear.
If you check the bytecode, it'd be worth reporting back; I'm curious how far ProGuard will follow a call chain to detect dead code. I'd be surprised if the string concatenation went away.
You also can't declare a variable private like that inside a method.

Related

Migrating from Leakcanary 1.* to 2.* Converting LeakTrace to StackTrace

I am trying to migrate from LeakCanary version 1.* to version 2.* version one used to have AnalysisResult.leakTraceAsFakeException but now I can't seem to be able to find a similar behavior I am stuck after creating my custom FakeException but I am unable to convert the LeakTrace to a StackTrace
I am following this link but I am not using BugSnag in my application
This capability was removed from LeakCanary 2, but you can recreate it for your own needs. See the code in LeakCanary 1.6: https://github.com/square/leakcanary/blob/v1.6.3/leakcanary-analyzer/src/main/java/com/squareup/leakcanary/AnalysisResult.java#L104-L133
public #NonNull RuntimeException leakTraceAsFakeException() {
if (!leakFound) {
throw new UnsupportedOperationException(
"leakTraceAsFakeException() can only be called when leakFound is true");
}
LeakTraceElement firstElement = leakTrace.elements.get(0);
String rootSimpleName = classSimpleName(firstElement.className);
String leakSimpleName = classSimpleName(className);
String exceptionMessage = leakSimpleName
+ " leak from "
+ rootSimpleName
+ " (holder="
+ firstElement.holder
+ ", type="
+ firstElement.type
+ ")";
RuntimeException exception = new RuntimeException(exceptionMessage);
StackTraceElement[] stackTrace = new StackTraceElement[leakTrace.elements.size()];
int i = 0;
for (LeakTraceElement element : leakTrace.elements) {
String methodName = element.referenceName != null ? element.referenceName : "leaking";
String file = classSimpleName(element.className) + ".java";
stackTrace[i] = new StackTraceElement(element.className, methodName, file, 42);
i++;
}
exception.setStackTrace(stackTrace);
return exception;
}
The biggest difference between 1.6 and 2 is that an analysis result used to have one leaktrace (=> converts to one stacktrace) but now LeakCanary can find many leaks at once so the analysis result will have several leaks, you'll want to create a stacktrace for each.

Logcat: make output using 'Log' class show full package name

I am using statements in my code in the same class that look like:
Log.i(TAG, "Writing command to initialize the FORA");
and
Log.i(TAG, "onDescriptorWrite signaled with status " + status);
yet the output from the first one is
Writing command to initialize the FORA
but the second one is
04-11 08:01:13.109 9030-9139/? I/com.lampreynetworks.ahd.transport.btle.b: onDescriptorWrite signaled with status 0
I would like to have the output with the package name in both cases. I thought that was determined by the TAG which in my case is
private static final String TAG = AndroidBtleHandler.class.getName();
I thought it might be because some of the statements were in a class within the class but that is not so. What do I need to do to get the full package name in the logcat output?
It's not dependent on the message itself. If you have directly following info logs with Log.i() from the same class in your Logcat, only the first one shows the class name. (I don't know why that is.) It's the same with Log.w().
If you use Log.d(), the class name is shown every time.
public class App {
public static String getTag() {
String tag = "";
final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
for (int i = 0; i < ste.length; i++) {
if (ste[i].getMethodName().equals("getTag")) {
tag = "("+ste[i + 1].getFileName() + ":" + ste[i + 1].getLineNumber()+")";
}
}
return tag;
}
}
And instead of Log.i(TAG, "Writing command to initialize the FORA"); use Log.i(App.getTag(), "Writing command to initialize the FORA")

I Build a Brain Trainer app but Not Showing my TEXTVIEW when i run my app and warning is showing do not concatenate text display with setText

sumTextView.setText(Integer.toString(a) + " + " + Integer.toString(b));
This Line show warning you see in pic..
Use String.format();
sumTextView.setText(String.format("%1$d + %2$d", a, b));
With this you can format a string correctly with multiple variables, no matter whether they are strings or integers. This example takes the value of variable a and replaces the placeholder %1$d with it. Same goes for the other variable.
take an string copy whole line in it, then show string in setText
String str = (Integer.toString(a) + " + " + Integer.toString(a));
sumTextView.setText(str);
1. The First String Says that do not concate string with setText property.
String txt = String.valueOf(a) + " + " + String.valueOf(b);
sumTextView.setText(str);
2. Second warning says that your program have possibility to crash or genearte an exception in case if value of a or b is null or not an integer.
So check condition if(a!=null and b!=null) then display text in if condition.

Facebook API Error code 100 - Unity

I'm developing a game with Unity3D for Android. I'm using Facebook App for sharing game score in Facebook. But I receive a error message ;
My codes are here ;
//facebook share start
public static void share(string link, string pictureLink, string name,string caption, string description, string redirectUri){
Application.OpenURL(ShareUrl +
"?app_id=" + AppId +
"&link=" + WWW.EscapeURL( link )+
"&picture=" + WWW.EscapeURL(pictureLink) +
"&name=" + WWW.EscapeURL(name) +
"&caption=" + WWW.EscapeURL(caption) +
"&description=" + WWW.EscapeURL(description) +
"&redirect_uri=" + WWW.EscapeURL(redirectUri));
}//facebook share end
if(GUI.Button(new Rect(Screen.width/2,(Screen.height/2-30),80,20), "Share")){
print("Share");
share("http://www.halilcosgun.com","https://24.media.tumblr.com/avatar_ce3a5b939737_64.png","Facebook skor paylaşma denemesi " + score,"Skor da mı paylaşmıyah?","oyun çok yakında!","http://facebook.com");
}
I tried to write many adresses (many many configurations) intead of "http://facebook.com", but I can't find the true one.
If you know the solution, can you halp me please?
I would like to thank you for your interest.
there must me an invalid parameter that you have provided . try to find it out and make the change it will solve your issue
try the share with first s in caps example
Share("http://www.halilcosgun.com","https://24.media.tumblr.com/avatar_ce3a5b939737_64.png","Facebook skor paylaşma denemesi " + score,"Skor da mı paylaşmıyah?","oyun çok yakında!","http://facebook.com");
}

Android PDF Writer(APW) Enconding

I am using Android PDF Write(APW) to create a PDF, but it doesn't work with some special characters(portuguese).
mypdf.addText(170, 50, 40,"Coração");
The standard enconding is:
mypdf.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
outputToFile("helloworld.pdf",pdfcontent,"ISO-8859-1");
I'v tried
outputToFile("helloworld.pdf",pdfcontent,"UTF-8");
outputToFile("helloworld.pdf",pdfcontent,"UTF-16");
outputToFile("helloworld.pdf",pdfcontent,"Cp1252");
and didn't succeed.
Any ideas what should I do?
EDIT
The method outputToFile is defined as:
private void outputToFile(String fileName, String pdfContent, String encoding) {
File newFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
try {
newFile.createNewFile();
try {
FileOutputStream pdfFile = new FileOutputStream(newFile);
pdfFile.write(pdfContent.getBytes(encoding));
pdfFile.close();
} catch(FileNotFoundException e) {
//
}
} catch(IOException e) {
//
}
}
The method addText is defined as:
public void addText(int leftPosition, int topPositionFromBottom, int fontSize, String text, String transformation) {
addContent(
"BT\n" +
transformation + " " + Integer.toString(leftPosition) + " " + Integer.toString(topPositionFromBottom) + " Tm\n" +
"/F" + Integer.toString(mPageFonts.size()) + " " + Integer.toString(fontSize) + " Tf\n" +
"(" + text + ") Tj\n" +
"ET\n"
);
}
Besides, I change the font color to white adding the following rawcontent:
mypdf.addRawContent("1 1 1 rg\n");
Then I come back to the black font color:
mypdf.addRawContent("0 0 0 rg\n");
I took all the information provided, wrote the following simple unit test method and ran it.
public void test19192108()
{
PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT);
mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
mPDFWriter.addText(170, 50, 40,"Coração");
String pdfcontent = mPDFWriter.asString();
outputToFile("helloworld19192108.pdf",pdfcontent,"ISO-8859-1");
}
(outputToFilebeing the helper method from the APW PDFWriterDemo class)
The result looks like this:
This seems pretty much to fulfill the expectations.
Thus, in whichever way it doesn't work with some special characters(portuguese) for the OP, some vital information is missing for reproducing the issue.
PS: Depending on the setup of the development environment, there might be an issue with non-ASCII characters in the source code. Thus, it might be a good idea to replace
mPDFWriter.addText(170, 50, 40,"Coração");
with
mPDFWriter.addText(170, 50, 40,"Cora\u00e7\u00e3o");
PPS: Adobe Reader after viewing a file generated like this wants to repair it. The reason is that the cross reference table is broken. The code generating entries for it is this:
public void addObjectXRefInfo(int ByteOffset, int Generation, boolean InUse) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("%010d", ByteOffset));
sb.append(" ");
sb.append(String.format("%05d", Generation));
if (InUse) {
sb.append(" n ");
} else {
sb.append(" f ");
}
sb.append("\r\n");
mList.add(sb.toString());
}
(from CrossReferenceTable.java)
Counting the characters in this entry we get 10 + 1 + 5 + 3 + 2 = 21.
According to the specification, though:
Each entry shall be exactly 20 bytes long, including the end-of-line marker
(from section 7.5.4 Cross-Reference Table of ISO 32000-1)
When using (the current version of) the Android PDF Writer, you should fix this code, too.

Categories

Resources