混淆代码 [英] Obfuscated code

查看:82
本文介绍了混淆代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人要求我为Android创建一个简单的应用.实际上第一个我会得到报酬,所以我真的不想把它搞砸了:).要求之一是必须对代码进行混淆.

I was asked to crate a simple app for android. The first one in fact that I'll be paid for, so I really don't want to screw it up :). One of the requirements was that the code must be obfuscated.

我了解了混淆的基本概念,但是我不想犯任何愚蠢的错误.

I learned the general idea of obfuscating, but I don't want to make any silly mistakes.

我到底该怎么做才能使代码变得模糊?将其导出为版本是否可以完成工作,还是需要执行其他一些步骤?任何评论也表示赞赏.

What precisely do I have to do to make the code obfuscated? Does exporting it as release do the job, or some other steps are required? Any remarks are also appreciated.

PS.如果需要的话,我正在使用Eclipse.

PS. I'm using Eclipse if it matters.

编辑

摘自anwsers中建议的文章:

From the article suggested in the anwsers:

要启用ProGuard,使其能够作为Ant或Eclipse构建的一部分运行, 在 /project.properties文件.路径可以是绝对的 路径或相对于项目根目录的路径.

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

如果将proguard.cfg文件保留在其默认位置( 项目的根目录),您可以像这样指定其位置:

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

proguard.config=proguard.cfg

我的项目目录中确实有 project.properties 文件.但是我没有 proguard.cfg 文件.相反,我有 proguard-project.txt 文件.我想这是一个替代品.

I indeed have the project.properties file in my project's dir. But I don't have the proguard.cfg file. Instead I have the proguard-project.txt file. I guess it's a replacement.

project.properties:

project.properties:

此文件由Android Tools自动生成. 不要修改此文件-您的更改将被清除!

This file is automatically generated by Android Tools. Do not modify this file -- YOUR CHANGES WILL BE ERASED!

必须在版本控制系统中检查此文件.

This file must be checked in Version Control Systems.

要定制Ant构建系统使用的属性,请编辑 "ant.properties",并覆盖值以使脚本适应您的 项目结构.

To customize properties used by the Ant build system edit "ant.properties", and override values to adapt the script to your project structure.

要使ProGuard能够缩小和混淆代码,请取消注释(可用属性:sdk.dir,user.home):

To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):

proguard.config = $ {sdk.dir} \ tools \ proguard \ proguard-android.txt:proguard-project.txt

proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt

项目目标. target = android-7

Project target. target=android-7

最初,除最后一行外的所有内容都被注释掉了.

Originally everything but the last line is commented out.

proguard-project.txt 中,所有内容均已注释掉.

In proguard-project.txt everything is commented out.

我想我在这里迷路了,所以如果有人可以一步一步告诉我我应该做什么以及如何检查它是否真的有效,我将非常感激.

I guess I'm lost here, so I'd be very thankful If somebody could tell me step by step what I am supposed to do and also how to check if it actually works.

推荐答案

官方开发网站上的文章已过时.从SDK Tools r17开始,他们更改了ProGuard配置的工作方式.基本上,他们将以前为proguard.cfg的文件分成两个文件(也简单地使它们为.txt文件):proguard-android.txtproguard-project.txt. proguard-android.txt位于您的{SDK dir}\tools\proguard目录中,而proguard-project.txt在您的每个项目目录中.

The article on the official dev site is out of date. Starting with SDK Tools r17 they changed the way the ProGuard configuration works. Basically they split what used to be proguard.cfg into two files (and also simply made them .txt files): proguard-android.txt and proguard-project.txt. proguard-android.txt resides in your {SDK dir}\tools\proguard directory and proguard-project.txt is in each of your project directories.

他们分割了文件,以便Android团队认为相当标准的常规配置/标志将出现在proguard-android.txt文件中,并且可以随每个SDK Tools版本进行更新(基本上以静默方式进行更新).然后,任何开发人员/项目特定的标志,开发人员都可以添加到proguard-project.txt文件中.

They split the file so that general configuration/flags that the Android team deems pretty standard will be in the proguard-android.txt file and can be updated (basically silently) with each SDK Tools revision. And then any developer/project specific flags, the developer can add to the proguard-project.txt file.

这就是为什么您注意到proguard-project.txt文件中的所有内容都被注释掉的原因,因为通常大多数人没有任何项目特定的需求,他们只是想使用标准设置运行ProGuard并完成此操作

This is why you noticed everything in your proguard-project.txt file was commented out, because in general most people don't have any project specific needs, they just want to run ProGuard with standard settings and be done with it.

作为参考(很好的解释): http://tools.android.com/recent/proguardimprovements

For reference (a very good explanation): http://tools.android.com/recent/proguardimprovements

分步操作(每天:P)

(使用Eclipse IDE)仅使用默认设置即可启用ProGuard及其缩小和模糊处理:

(Using Eclipse IDE) To enable ProGuard and it's shrinking and obfuscation using only the default settings:

  • 首先将您的SDK工具更新为最新版本(在撰写本文时为r19)
  • project.properties中,取消注释以下行:
  • First update your SDK Tools to newest (r19 at time of writing)
  • In project.properties uncomment the line that says:

proguard.config = $ {sdk.dir} \ tools \ proguard \ proguard-android.txt:proguard-project.txt

proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt

如果您在r17之前创建了项目,则可能看不到相同的内容.如果是这种情况,我建议您(在更新SDK工具之后)创建一个新的虚拟项目,然后复制proguard-project.txtproject.properties文件.

If you created the project before r17 then you might not see the same things. If that's the case I suggest you create a new dummy project (after updating SDK Tools) and just copy over the proguard-project.txt and project.properties files.

就是这样! ProGuard现在将被启用,并且将运行,但是在发布版本中(仅当您使用Android工具并导出已签名/未签名的apk时).

That's it! ProGuard will now be enabled and it will run, but only on release builds (when you use Android Tools and export a signed/unsigned apk).

进行发行版本时,您会注意到将在项目文件夹中创建一个新的ProGuard文件夹.在此文件夹中,您可以找到四个文件,其中包含诸如保留或删除哪些类/方法之类的内容.还要注意,当混淆时,还会在发生错误时混淆堆栈跟踪.

When you make a release build you'll notice that a new ProGuard folder will be created in your project folder. Within this folder you find four files with stuff like what classes/methods were kept or removed. Also note that when you obfuscate, you'll also obfuscate your stacktraces in the event of errors.

(9/4/12)只是想更新此答案以反映Tools r20中的更改.

(9/4/12) Just wanted to update this answer to reflect changes in Tools r20.

Android团队现在添加了一个附加的proguard配置文件.它是proguard-android-optimize.txt,它也位于{SDK dir}\tools\proguard目录中.此文件与原始proguard-android.txt配置文件相同,除了,它已启用了防护优化.

The Android team has now added one additional proguard configuration file. It is proguard-android-optimize.txt and it also resides in the {SDK dir}\tools\proguard directory. This file is the same as the original proguard-android.txt config file, except it has proguard optimization turned on.

添加了这个额外的文件是为了方便那些想启用优化的人(也许他们可以做一些事情,例如删除Log调用,这需要启用优化).现在,您可以像这样在project.properties文件中直接指向该文件:

This additional file was added for convenience for those who wanted to turn on optimizations (maybe so they can do stuff like strip out Log calls, which requires optimizations be turned on). Now you can simply point to this file instead in your project.properties file like so:

proguard.config=${sdk.dir}\tools\proguard\proguard-android-optimize.txt:proguard-project.txt

和以前一样,请勿尝试更改任何proguard-android...配置文件.任何与项目相关的标志都应放在您的项目文件夹中的proguard-project.txt文件中.

As before, don't try to alter any of the proguard-android... config files. Any project specific flags should go in the proguard-project.txt file in your project folder.

这篇关于混淆代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆