ITeye博客

android混淆(Obfuscate)

fonter · 2009-10-14 22:19 · android · Markdown

《Obfuscate an Android application》 这篇文章的作者介绍了如何在Android平台下混淆程序,并且给出ant的build.xml文件,经过自己的测试,发现里面少了一些target项,在Eclips会出错,后来结合zxing开源项目的build.xml文件,终于混淆成功,项目包减少了不少。

 

这里主要提一些注意事项,不提供源代码

1、创建build.xml文件

 直接在项目的目录下新建一个名为build.xml的文件

 

 

2、创建Ant打包方式

项目 –> properties –> builders –> new –> ant builder

 

 

接着Buildfile选择项目build.xml文件,Base Dir选择项目目录就行了,记得勾上你的builders

 

 

 

3、build.xml文件修改

zxing这个文件没有加入混淆包选项,所以得做一些修改,如下示例

 

<property name="proguard-home" value="D:\\Program Files\\proguard4.2\\lib\\"/>

	<target name="optimize" depends="compile">
		<jar basedir="${outdir-classes}" destfile="temp.jar"/>
		<java jar="${proguard-home}/proguard.jar" fork="true" failonerror="true">
			<jvmarg value="-Dmaximum.inlined.code.length=32"/>
			<arg value="-injars temp.jar"/>
			<arg value="-outjars optimized.jar"/>
			<arg value="-libraryjars ${android-jar}"/>
			<!-- <arg value="-libraryjars ${library-jar}/some_lib_used.jar"/> -->
			<arg value="-dontpreverify"/>
			<arg value="-dontoptimize"/>
			<arg value="-dontusemixedcaseclassnames"/>
			<arg value="-repackageclasses ''"/>
			<arg value="-allowaccessmodification"/>
			<arg value="-keep public class com.iwidsets.WidsetsActivity"/>
			<!-- <arg value="-keep public class com.just2me.obfapp.receiver.*"/> -->
			<arg value="-optimizationpasses 7"/>
			<arg value="-verbose"/>
			<arg value="-dontskipnonpubliclibraryclasses"/>
			<arg value="-dontskipnonpubliclibraryclassmembers"/>
		</java>
		<delete file="temp.jar"/>
		<delete dir="${outdir-classes}"/>
		<mkdir dir="${outdir-classes}"/>
		<unzip src="optimized.jar" dest="${outdir-classes}"/>
		<delete file="optimized.jar"/>
	</target>


 

注意:keep public class com.iwidsets.WidsetsActivity需改成你的主Activity

 

 并修改target dex

 

	<!-- Convert this project's .class files into .dex files. -->
	<target name="dex" depends="compile,optimize">
		<echo>Converting compiled files and external libraries into ${out-folder}/${dex-file}...</echo>
		<apply executable="${dx}" failonerror="true" parallel="true">
			<arg value="--dex" />
			<arg value="--output=${intermediate-dex-location}" />
			<arg path="${outdir-classes-location}" />
			<fileset dir="${external-libs-folder}" includes="*.jar"/>
		</apply>
	</target>

评论(9)

  1. #9 楼 · s123451 · 2013-05-30 13:27
    [quote][/quote]
    
  2. #8 楼 · fonter · 2011-08-23 17:47

    caitiancai writes:

    BUILD FAILED D:\personal\eclipse\S2\build.xml:188: src 'D:\personal\eclipse\core\core.jar' doesn't exist.

    请问这个怎么解决呀????

    现在的android sdk已支持混淆了,直接通过eclipse就可以混了,不用再写xml了..你可以试试... http://fonter.iteye.com/blog/942714

  3. #7 楼 · caitiancai · 2011-08-23 11:38

    BUILD FAILED D:\personal\eclipse\S2\build.xml:188: src 'D:\personal\eclipse\core\core.jar' doesn't exist.

    请问这个怎么解决呀????

  4. #6 楼 · songfantasy · 2011-04-28 16:55

    sdk8以后,google已经给提供很方便的混淆方法。

    不过,楼主不错。总结的很好

  5. #5 楼 · sunburst · 2010-10-02 00:48

    fonter writes:

    No resource found that matches the given name (at 'icon' with value '@drawable/icon').[quote=sunburst]非常感谢你的回答,现在出现了新问题,当我开始build的时候,显示下面的错误,应该如何解决?

    resource-src: [echo] Generating R.java / Manifest.java from the resources... [exec] E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:26: ERROR Error: No resource found that matches the given name (at 'icon' with value '@drawable/icon'). [exec] E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:26: ERROR Error: No resource found that matches the given name (at 'label' with value '@string/app_name'). [exec] E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:28: ERROR Error: No resource found that matches the given name (at 'label' with value '@string/app_name').

    BUILD FAILED[/quote] No resource found that matches the given name (at 'icon' with value '@drawable/icon'). 在drawable没有icon吧,“No resource found that matches the given name ”这个

    实际是有的,不然project本身也会报错,现在情况是不用ant是正常编译的,说明资源指向是正确的

  6. #4 楼 · fonter · 2010-10-01 23:05

    No resource found that matches the given name (at 'icon' with value '@drawable/icon').

    sunburst writes:

    非常感谢你的回答,现在出现了新问题,当我开始build的时候,显示下面的错误,应该如何解决?

    resource-src: [echo] Generating R.java / Manifest.java from the resources... [exec] E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:26: ERROR Error: No resource found that matches the given name (at 'icon' with value '@drawable/icon'). [exec] E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:26: ERROR Error: No resource found that matches the given name (at 'label' with value '@string/app_name'). [exec] E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:28: ERROR Error: No resource found that matches the given name (at 'label' with value '@string/app_name').

    BUILD FAILED

    No resource found that matches the given name (at 'icon' with value '@drawable/icon'). 在drawable没有icon吧,“No resource found that matches the given name ”这个

  7. #3 楼 · sunburst · 2010-09-30 21:59

    非常感谢你的回答,现在出现了新问题,当我开始build的时候,显示下面的错误,应该如何解决?

    resource-src: Generating R.java / Manifest.java from the resources... E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:26: ERROR Error: No resource found that matches the given name (at 'icon' with value '@drawable/icon'). E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:26: ERROR Error: No resource found that matches the given name (at 'label' with value '@string/app_name'). E:\Android App\sms\MyPrivateMessages\AndroidManifest.xml:28: ERROR Error: No resource found that matches the given name (at 'label' with value '@string/app_name').

    BUILD FAILED

  8. #2 楼 · fonter · 2010-09-28 19:37

    sunburst writes:

    注意:keep public class com.iwidsets.WidsetsActivity需改成你的主Activity

    这里只用添加主Activity?其他的不用添加?那我引用的jar包是否需要添加?

    看你需不需要啊。。。比如重要的代码不想让人知道,就混淆。包的话也是这样啊。。。。

  9. #1 楼 · sunburst · 2010-09-28 16:41

    注意:keep public class com.iwidsets.WidsetsActivity需改成你的主Activity

    这里只用添加主Activity?其他的不用添加?那我引用的jar包是否需要添加?

评论功能维护中,当前仅展示历史评论。