Android studio 打包jar并混淆

  1. 新建一个Library module

  2. 修改build.gradle文件,新增:

    1
    2
    3
    4
      debug {
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    同时配置proguard-rules.pro

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
      #指定压缩级别
    -optimizationpasses 5

    #表示混淆时不使用大小写混合类名
    -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.

    #不跳过非公共的库的类成员
    -dontskipnonpubliclibraryclassmembers

    #混淆时采用的算法
    -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

    #对外暴露public和protected方法,混淆private方法
    -keep public class com.sunflower.utils.MacUtils { public protected *; }
  3. 同步gradle后在’build/intermediates/bundles/debug/classes.jar’即是混淆后的jar包