Android mould 生成jar包并添加混淆
1、首先确定自己mould是用的那个版本的gradle。
2、根据mould使用的gradle配置环境变量。 (如果不配置环境变量,可能找不大道makejar功能入口)
3、环境变量配置成功后cmd 输入 gradle -v 查看是否配置完成。
找到自己的gradle目录,一般如下:C:\Users\admin\.gradle\wrapper\dists
不过配置的环境不是这里,而是 C:\Users\admin\.gradle\wrapper\dists\gradle-5.4.1-all\3221gyojl5jsh0helicew7rwx\gradle-5.4.1
找到所在目录后,复制一下路径,然后打开电脑找到环境变量配置然后点击新建
填入一下信息
然后点击确定。
找到系统变量的path双击
然后输入刚才的名称即可
4、增加代码混淆 步骤:
先在moudle的build.gradle中加入如下代码 此代码跟 android { } 同一个级别
def SDK_BASENAME = "testjar";//jar包名称 def SDK_VERSION = "_v1.0.0";//版本好 def sdkDestinationPath = "build/outputs/jar/";//生成jar包路径 def zipFile = file('build/intermediates/packaged-classes/release/classes.jar') task deleteBuild(type: Delete) { delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar" } task makeJar(type: Jar) { from zipTree(zipFile) from fileTree(dir: 'src/main', includes: ['assets/**']) // 打包assets目录下的所有文件 baseName = SDK_BASENAME + SDK_VERSION destinationDir = file(sdkDestinationPath) } makeJar.dependsOn(deleteBuild, build)
另外启用混淆
buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } }
最后是在混淆文件中添加混淆规则: 这是我在网上参照别人的,感觉还行。
# Add project specific ProGuard rules here. # You can control the set of applied configuration files using the # proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile ###########################以下是AndroidStudio自带的混淆配置协议############################### # 表示混淆时不使用大小写混合类名 -dontusemixedcaseclassnames # 表示不跳过library中的非public的类 -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 # 表示不进行校验,这个校验作用 在java平台上的 -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 # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native #指定不混淆所有的JNI方法 -keepclasseswithmembernames class * { native <methods>; } # keep setters in Views so that animations can still work. # see http://proguard.sourceforge.net/manual/examples.html#beans #所有View的子类及其子类的get、set方法都不进行混淆 -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 # 不混淆Activity中参数类型为View的所有方法 -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations # 不混淆Enum类型的指定方法 -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } # 不混淆Parcelable和它的子类,还有Creator成员变量 -keepclassmembers class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator CREATOR; } # 不混淆R类里及其所有内部static类中的所有static变量字段 -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>(...); } ###########################以下是需要手动的混淆配置协议############################### #-libraryjars "C:\Program Files\Java\jre1.8.0_151\lib\rt.jar" #-libraryjars "C:\Users\admin\AppData\Local\Android\sdk\platforms\android-26\android.jar" # 注意:以上两个路径需要将以上路径是自己jar包的位置,需要根据自己情况进行修改,如果报重复配置的错误,注释掉即可 #代码迭代优化的次数,默认5 #-optimizationpasses 5 #混淆时不会产生形形色色的类名 -dontusemixedcaseclassnames #忽略警告 -ignorewarnings #以下是不需要混淆的文件 -keep class com.xhs.testjar.ToastUtil{ *; }
5、添加完混淆后就是生成jar包,点击android studio 中右边的 gradle 弹出列表,找到自己的moudle 名字,然后点击Tasks-》other 最后双击里面的makeJar 即可生成jar包,也有arr文件。 arr文件在outputs -》arr中,