当前位置: 移动技术网 > 移动技术>移动开发>Android > Android怎么打包Jar包并添加混淆

Android怎么打包Jar包并添加混淆

2020年08月05日  | 移动技术网移动技术  | 我要评论

最近公司有个项目,需要我们提供底层的读写卡SDK卡(基于掌机和基于蓝牙读卡器的SDK),开发难度上算简单,但是需要打包成Jar库,以及源码不公开。由于Android是开源的,目前也没有能找到更好的方案,所以只能在打包Jar库的时候添加混淆这一个方式能实现客户的需求了。(如果有大神有其他更好的方式,请务必给我留言,哈哈哈哈)。

  • 创建一个Android Lib类库。

  • 在类库中编写代码。
  • 在lib库的gradle中的android下添加以下代码:
//配置jar包的指令
def SDK_BASENAME = "cardServer_bete";//生成的jar包的名称
    def SDK_VERSION = "_v1.0.3";//生成的jar包的版本号
    def sdkDestinationPath = "build/outputs/jar/";//jar存放的路径
    def zipFile = file('build/intermediates/bundles/release/classes.jar')
    task deleteBuild(type: Delete) {
        //每次生成前,都先删除之前的jar包
        delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
    }
    //生成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)
  • 在gradle中开启混淆开关,并在proguard-rules.pro(注意要选择你需要混淆的库的文件哦)添加你需要的混淆规则。混淆规则较多,主要是三方不可混淆,你需要公开出去的API不可混淆等等,具体可以自行百度或者参考如下:

 ###########################以下是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.lu.cardserver.cardManager.** { *; }
-keep class com.lu.cardserver.model.** { *; }
-keep class com.lu.cardserver.dataFormat.model.**{ *; }
-keep class com.lu.cardserver.dataFormat.type.**{ *; }
-keep class com.lu.cardserver.app.** { *; } 
  • 最后一步,在Android studioh中的Terminal输入gradlew makeJar,回车即可。打包,打包显示成功以后,去你在第三步中设置的路径下去取Jar即可。

本文地址:https://blog.csdn.net/lujiaquan688/article/details/107756806

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网