Ant混淆打包生成SDK的JAR包

1.eclipse已经集成了ant插件,注意如果是android官网下载的eclipse开发环境可能没有集成ant,需要自行安装插件或到官网下载eclipseIDE

 使用ant混淆,则需要使用proguard,androidsdk也已经集成了该项目,路径在android-sdk\tools\proguard 文件夹下,如果没有请自行下载

以下使用eclipse集成ant和proguard4.6混淆打包示例:

混淆需要三个文件:build.properties  属性文件,定义一些路径属性;build.xml ant构建文件;config.txt 混淆配置文件(和android自带混淆proguard-project.txt基本相同)

在项目目录下新建这些文件即可

build.properties文件:

app.source.path=src
#android指定编译的api版本,build.xml文件将会调用该API版本下的android.jar文件
sdk.folder=F\:/android-sdk/platforms/android-18/
#androidsdk根目录
apk.sdk.home=F\:/android-sdk
#jdk根目录
jdk.home=C\:/Program Files/Java/jdk1.7.0_55
#混淆项目proguard目录
proguard.home=F:\\android-sdk\\tools\\proguard4.6\\lib
android.tools=${sdk.folder}tools
apk.tools=${apk.sdk.home}/tools/
bin.dir=${jdk.home}/bin
classDir=bin/classes


build.xml文件将会引用以上的build.properties文件,内容如下:




    
	
	
	
	
	
	    
	    
	    
	    
	    
	    
		
		
		
	 

	
	
	      
	
	
 	
	
	    		
				
					
					
				
				
			
	  
	
	
		
		
		
		
			
			
			
			
			
			
			
			  
		
		
		
	



config.txt配置文件,该文件和android自带混淆一样:


-target 1.6 
#-optimizationpasses 2 
-dontusemixedcaseclassnames 
-dontskipnonpubliclibraryclasses 
-dontskipnonpubliclibraryclassmembers
-dontshrink
-dontpreverify 
-verbose 
-dontoptimize
-ignorewarnings

-keepparameternames
-renamesourcefileattribute SourceFile

-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
-allowaccessmodification

-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

-keepclasseswithmembernames class * {
    native ;
}

-keepclasseswithmembernames class * {
    public (android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public (android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

#-keep class * implements android.os.Parcelable {
#  public static final android.os.Parcelable$Creator *;
#}

-keepclassmembers class * implements android.os.Parcelable {
      public static final android.os.Parcelable$Creator *;
}

-keep class * implements java.io.Serializable { *;}

#不混淆指定的包和类
-keep class com.ble.action.gps.entity.** {*;}
-keep class com.ble.action.entity.**{*;}


eclipse下右键build.xml >> Run as >> Ant Build 生成已混淆的jar文件

控制台日志输出如下 :

Buildfile: F:\sample\build.xml
init:
compile:
    [javac] Compiling 69 source files to .....
  
optimize:
	[jar] Building jar: F:\sample\temp.jar
[java] ProGuard, version 4.6
     [java] Reading input...
     [java] Reading program jar [F:\sample\temp.jar]
     [java] Reading library jar [F:\android-sdk\platforms\android-18\android.jar]
     [java] Reading library directory [F:\sample\libs]
     [java] Initializing...
     [java] Note: You're ignoring all warnings!
     [java] Ignoring unused library classes...
     [java]   Original number of library classes: 3918
     [java]   Final number of library classes:    230
     [java] Setting target versions...
     [java] Obfuscating...
     [java] Writing output...
     [java] Preparing output jar [F:\sample\sample.jar]
     [java]   Copying resources from program jar [F:\sample\temp.jar]
   [delete] Deleting: F:\sample\temp.jar
BUILD SUCCESSFUL
Total time: 4 seconds



注意: 如果jdk是1.8,ant为1.9以下时,运行时会报Class not found: javac1.8  ,只需要升级ant即可:

1.进入http://ant.apache.org/manualdownload.cgi下载新版本的Ant,下载之后解压。

 2.进入你eclipse下的插件路径找出Ant插件,如我的就是F:\eclipse\plugins\org.apache.ant_1.8.4.v201303080030,打开该路径,将Ant新版本解压出来的文件替换到该目录下,不过要保留META-INF目录 和plugin.properties文件。

3.可以通过cmd进入该插件的位置之后运行ant -version可以查看到ant的版本号,更新成功会显示新的版本号

4.替换完成后重启eclipse,之后用ant打包就能正常兼容jdk1.8了。



你可能感兴趣的:(android)