一般假設
所以我發現很多關於使用proguard從代碼中去除日誌語句。基本上所有的說--assumenosideeffects與使用$ {sdk.dir} /tools/proguard/proguard-android-optimize.txt配置將做的伎倆。我的理解是讓字節等於做一些如何去掉android.Log.d?
if(Consts.DEBUG) Log.d("","");
又名我認爲讓呼叫從我的apk消除doSomeExpensiveStuff():
android.util.Log.d("Hello","World"+(new Foo().doSomeExpensiveStuff()));
我的代碼
public class MainActivity extends Activity {
private class Slooow {
@Override
public String toString() {
// being slow
try {
Thread.sleep(5000);
} catch(InterruptedException e) {
}
return "bla";
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("tag", "onCreate: " + (new Slooow().toString()));
}
}
proguard-project.txt:
-repackageclasses ''
-optimizationpasses 5
-dontobfuscate
-assumenosideeffects class android.util.Log { public * ; }
反編譯DEX文件
.method public onCreate(Landroid/os/Bundle;)V
.limit registers 5
; this: v3 (Lcom/example/test/MainActivity;)
; parameter[0] : v4 (Landroid/os/Bundle;)
.line 18
invoke-super {v3,v4},android/app/Activity/onCreate ; onCreate(Landroid/os/Bundle;)V
.line 19
const/high16 v0,32515
invoke-virtual {v3,v0},com/example/test/MainActivity/setContentView ; setContentView(I)V
.line 20
new-instance v0,java/lang/StringBuilder
const-string v1,"onCreate: "
invoke-direct {v0,v1},java/lang/StringBuilder/<init> ; <init>(Ljava/lang/String;)V
new-instance v1,com/example/test/MainActivity$Slooow
const/4 v2,0
invoke-direct {v1,v2},com/example/test/MainActivity$Slooow/<init> ; <init>(B)V
invoke-virtual {v1},com/example/test/MainActivity$Slooow/toString ; toString()Ljava/lang/String;
move-result-object v1
invoke-virtual {v0,v1},java/lang/StringBuilder/append ; append(Ljava/lang/String;)Ljava/lang/StringBuilder;
.line 21
return-void
.end method
問題
現在,如果我部署此我的設備,我沒有得到任何日誌輸出,但仍是5秒睡眠(或其它任何延遲或崩潰的代碼,不該」不要打擾我的用戶)。 我在做什麼錯?
進一步調查
Log.d("t", "h" + "w"); // would get stripped just fine.
if(DEBUG)
Log.d("t", "h: " + (new Slooow().toString())); // would get optimized away, too.
Log.d("t", "h" + bundle.toString()); // has just the same problem as described above.
遇到同樣的問題的應用程序崩潰時,我使用的是由'ant釋放命令混淆的apk – juned
@juned這個問題沒有任何事情要做與應用程序崩潰;) – JesusFreke