利用android proguard混淆代碼

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   java   sp   for   

參考文檔:http://blog.csdn.net/xueyepiaoling/article/details/8202359轉載自:http://glblong.blog.51cto.com/3058613/1263969

在新版本的ADT建立項目時,混碼的檔案不再是proguard.cfg,而是project.properties和proguard-project.txt。

如果需要對項目進行全域混碼,只需要進行一步操作:

將project.properties的中

“#  proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt”的“#”去掉就可以了。

如 果有一些代碼不能被混淆,比如需要加入了so檔案,需要調用裡面的方法,那麼調用JNI訪問so檔案的方法就不能被混碼。在匯出的時候,可能不會報錯。但 是在手機上啟動並執行時候,需要調用so檔案的時候,就會報某某方法無法找到。這個時候就需要用到proguard-project.txt。

在老版本中,建立項目的時候,會給出proguard.cfg,但是在的新版中建立項目則不會有任何提示。這個時候需要只要將proguard.cfg的內容加入到proguard-project.txt中,再根據自己的需要進行編輯即可。

==================proguard.cfg防反編譯方法==================================

To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the proguard.config property in the<project_root>/project.properties file. The path can be an absolute path or a path relative to the project‘s root.

If you left the proguard.cfg file in its default location (the project‘s root directory), you can specify its location like this:

proguard.config=proguard.cfg

You can also move the the file to anywhere you want, and specify the absolute path to it:

proguard.config=/path/to/proguard.cfg

When you build your application in release mode, either by running ant release or by using the Export Wizardin Eclipse, the build system automatically checks to see if the proguard.config property is set. If it is, ProGuard automatically processes the application‘s bytecode before packaging everything into an .apk file. Building in debug mode does not invoke ProGuard, because it makes debugging more cumbersome.

===========【轉】混淆檔案proguard.cfg詳解======================================

-injars  androidtest.jar【jar包所在地址】

-outjars  out【輸出地址】

-libraryjars    ‘D:\android-sdk-windows\platforms\android-9\android.jar‘ 【引用的庫的jar,用於解析injars所指定的jar類】

-optimizationpasses 5

-dontusemixedcaseclassnames 【混淆時不會產生形形色色的類名 】

-dontskipnonpubliclibraryclasses 【指定不去忽略非公用的庫類。 】

-dontpreverify 【不預校正】

-verbose

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 【最佳化】

-keep public class * extends android.app.Activity  【不進行混淆保持原樣】

-keep public class * extends android.app.Application

-keep public class * extends android.app.Service

-keep public class * extends android.content.BroadcastReceiver

-keep public class * extends android.content.ContentProvider

-keep public class * extends android.app.backup.BackupAgentHelper

-keep public class * extends android.preference.Preference

-keep public class com.android.vending.licensing.ILicensingService

-keep public abstract interface com.asqw.android.Listener{

public protected <methods>;  【所有方法不進行混淆】

}

-keep public class com.asqw.android{

public void Start(java.lang.String); 【對該方法不進行混淆】

}

-keepclasseswithmembernames class * { 【保護指定的類和類的成員的名稱,如果所有指定的類成員出席(在壓縮步驟之後)】

native <methods>;

}

-keepclasseswithmembers class * { 【保護指定的類和類的成員,但條件是所有指定的類和類成員是要存在。】

public <init>(android.content.Context, android.util.AttributeSet);

}

-keepclasseswithmembers class * {

public <init>(android.content.Context, android.util.AttributeSet, int);

}

-keepclassmembers class * extends android.app.Activity {【保護指定類的成員,如果此類受到保護他們會保護的更好 】

public void *(android.view.View);

}

-keepclassmembers enum * {

public static **[] values();

public static ** valueOf(java.lang.String);

}

-keep class * implements android.os.Parcelable {【保護指定的類檔案和類的成員】

public static final android.os.Parcelable$Creator *;

}

 

 

 

 

 

=====================================常見異常===================================

參考:http://blog.csdn.net/vrix/article/details/7100841

 

加入第三方jar包之後常出現的幾個異常:

proguard returned with error code 1.See console

情況1:

Proguard returned with error code 1. See console

Error: C:/Documents (系統找不到指定檔案)

後來發現是因為將整個工程放到了案頭上,而案頭的目錄是C:/Documents and Settings/Administrator/案頭,在這裡面有空格,而proguard進行發編譯的時候是不允許有空格的

如果換了正確路徑還不好用的話,直接刪除proguard就好了

注意:SDK和程式路徑最好不要有空格符

情況2:

Proguard returned with error code 1. See console

異常:

java.lang.ArrayIndexOutOfBoundsException

解決辦法:將proguard.cfg中的"-dontpreverify"改成“-dontoptimize”

參考文章:http://groups.google.com/group/android-developers/browse_thread/thread/eca3b0f5ce6ad00f

 

我把項目中產生的proguard檔案夾(此時檔案夾是空的)刪掉,然後再重新運行項目,就OK 了。

情況3:

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] Proguard returned with error code 1. See console

 

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] java.io.IOException: Can‘t read [[email protected]] (No such file or directory)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.readInput(InputReader.java:230)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.readInput(InputReader.java:200)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.readInput(InputReader.java:178)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.execute(InputReader.java:100)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.ProGuard.readInput(ProGuard.java:195)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.ProGuard.execute(ProGuard.java:78)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.ProGuard.main(ProGuard.java:499)

拋出這樣的異常的原因是第三方jar的引用路徑不對,沒有找到這個需要忽略混淆的jar包。

 

利用android proguard混淆代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.