Andrid架包代码混淆与打包处理

1. 编辑混淆文件 proguard-rules.pro
-keep class com.hd123.piccmcarddriver.Card{*;}
-keep class com.hd123.piccmcarddriver.CardInfo{*;}
-keep class com.hd123.piccmcarddriver.CardOperateException{*;}
-keep class com.hd123.piccmcarddriver.CardOperator{*;}
-keep class com.hd123.piccmcarddriver.IntentUtil{*;}

-keep class com.hisense.pos.** {*;}

-keep, includedescriptorclasses class com.ums.** { *; }

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
#-dontwarn com.ums.**
#-dontnote com.ums.**
#-dontnote org.apache.**
#-dontnote android.net.**

#-dontwarn org.chromium.**
#-dontwarn org.apache.**
#-dontwarn io.liteglue.**
-dontwarn com.hd123.**
-dontwarn com.hisense.**

2. 设置minifyEnabled为true
 buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
3. 打包处理
在build.gradle android中添加如下代码
android.libraryVariants.all { variant ->
        task("generate${variant.name}Javadoc", type: Javadoc) {
            description "Generates Javadoc for $variant.name."
            source = variant.javaCompile.source
            ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
            classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
        }

        task("javadoc${variant.name}", type: Jar) {
            classifier = "javadoc"
            description "Bundles Javadoc into a JAR file for $variant.name."
            from tasks["generate${variant.name}Javadoc"]

        }

        task("jar${variant.name}", type: Jar) {
            description "Bundles compiled .class files into a JAR file for $variant.name."
            dependsOn variant.javaCompile
            from variant.javaCompile.destinationDir
            exclude '**/R.class', '**/R$*.class', '**/R.html', '**/R.*.html'
        }
    }
在build.gradle中添加task
task proguardJar(type: proguard.gradle.ProGuardTask) {
    //Android 默认的 proguard 文件
    configuration android.getDefaultProguardFile('proguard-android.txt')
    //manifest 注册的组件对应的 proguard 文件
    configuration 'proguard-rules.pro'
    String inJar = 'build/libs/****.jar'
    //输入 jar
    injars inJar
    //输出 jar
    outjars 'build/libs/****_release.jar'
    //设置不删除未引用的资源(类,方法等)
    dontshrink
   }

命令行中执行如下代码,在libs目录下就得到了输出的经过混淆的架包了
./gradlew clean javadocRelease jarRelease
./gradlew proguardJar 

你可能感兴趣的:(Andrid架包代码混淆与打包处理)