最近在做相关第三方的SDK,使用的开发工具是android studio 。开发的时候没什么问题,直接使用了新建了library Module,重点来了,产品结束的时候,混淆打包,强调一下,现在项目查看的模式是project模式下,不然找不到文件就发愁了。
- 最普遍的方法,就是直接修改library中的build.gradle和proguard-rules.pro文件,这两个修改已经是很普通化了。build.gradle中
buildTypes { release { minifyEnabled true//只需要把这里改成true就可以 shrinkResources false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
上边的意思很简单的。就是修改build时候的配置文件。minifyEnabled是混淆的意思,true就是build程序的时候打开混淆。最后一句话就是,混淆的方式,把你要求的混淆东西都写到proguard-rules.pro文件中,比如接口类不能混淆,就要在这个文件中写到,接下来看proguard-rules.pro。
# This is a configuration file for ProGuard. # http://proguard.sourceforge.net/index.html#manual/usage.html -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -verbose # Optimization is turned off by default. Dex does not like code run # through the ProGuard optimize and preverify steps (and performs some # of these optimizations on its own). -dontoptimize -dontpreverify # Note that if you want to enable optimization, you cannot just # include optimization flags in your own project configuration file; # instead you will need to point to the # "proguard-android-optimize.txt" file instead of this one from your # project.properties file. -keepattributes *Annotation* -keep public class com.google.vending.licensing.ILicensingService -keep public class com.android.vending.licensing.ILicensingService -keepclassmembers public class com.test.application.test{ # 下面几项是为了测试暴露出来的方法 public void getPublicKey(***); } # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native -keepclasseswithmembernames class * { native <methods>; } # keep setters in Views so that animations can still work. # see http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers public class * extends android.view.View { void set*(***); *** get*(); } # We want to keep methods in Activity that could be used in the XML attribute onClick -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keepclassmembers class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator CREATOR; } -keepclassmembers class **.R$* { public static <fields>; } # The support library contains references to newer platform versions. # Don't warn about those in case this app is linking against an older # platform version. We know about them, and they are safe. -dontwarn android.support.** # Understand the @Keep support annotation. -keep class android.support.annotation.Keep -keep @android.support.annotation.Keep class * {*;} -keepclasseswithmembers class * { @android.support.annotation.Keep <methods>; } -keepclasseswithmembers class * { @android.support.annotation.Keep <fields>; } -keepclasseswithmembers class * { @android.support.annotation.Keep <init>(...); } #忽略警告 -ignorewarnings #保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar #-dontshrink #保护泛型 -keepattributes Signature -keepattributes EnclosingMethod #下面是项目中不要混淆的代码 -keep class base64.**{*;}
说实话上边的也都是我从别的地方copy过来的。重点看有汉语注释的地方。
-keep class base64.**{*;} 就是把下边包内的所有类都不混淆,这里的-keep就是保留的意思,-keepclassmembers这个保留类里面的方法的意思,剩下的就自己研究吧,反正我是没怎么用到。
这样,跑起来还是没问题的,最后的jar文件在library中build/....文件下可以找到,下边就说说往jar包里添一些东西比如assets内的东西。
- 第一步和上边的一样,生出jar,第二步就厉害了,要用到task makejar。修改文件 还是build.gradle,首先开头加一个
apply plugin: 'com.android.library'
之后就要写task makejar
task makeJar(type: Jar) { from zipTree('build/intermediates/bundles/release/classes.jar') from fileTree(dir: 'src/main', includes: ['assets/**']) baseName = "Test" destinationDir = file("build") }
上边的意思,大致能看懂吧,两个from 就是要把混淆好的jar包和assets下的文件合并 出来的名字是baseName 位置放在build路径下,不过这里可能有些问题,我出来直接在该项目下。
写完上边的的就开始点吧,android studio最右边,一般缩小着的gradle,点开,一般都是项目目录,接着点开你的library项目,应该只有Tasks目录。接着点开,层次应该是 android build help install other verification
咱们用到的 是other,点开 找到makejar,双击,等待,等待,下边不报错就好了。