Android
  • Introduction
  • Android Studio
    • AS的项目结构
    • adb
    • aapt
    • dx
    • Gradle
    • Kotlin on android
  • Smaller
  • decompiler
  • MISC
  • 框架 framework
  • 设计模式
  • dex
  • reinforce加固
  • code
    • Java Tips
      • 负数 negative
      • The Java Tutorials
        • 原始数据类型 Primitive Data Types
      • 运算符
        • 一元运算符
        • 算术运算符
        • 移位运算符
        • 关系运算符
      • 逻辑运算符
        • 逻辑 非 ! 关系值表
        • 逻辑 与 && 关系值表
        • 逻辑 或 || 关系值表
        • 与 & And
        • 或 | Or
        • 非 ~ Nor
        • 异或 ^ Xor
        • 赋值运算符
        • tips
      • == equals
      • Try Catch finally
        • 有意思的东西
      • String、StringBuilder、StringBuffer区别
      • inner classes、nested static classes
    • runtime_memory
    • javaStackTrace
    • Guava
    • FFMPEG
    • GoogleSamples
    • Full Kotlin Reference
    • release屏蔽Log代码
    • Thread
  • ANR
  • 注解改进代码检查
Powered by GitBook
On this page
  • 愿望
  • 原理
  • assumenosideeffects
  • 方法
  • 效果检验

Was this helpful?

  1. code

release屏蔽Log代码

愿望

在RELEASE包中,代码中的指写的Log.v() Log.d() Log.i() Log.w() Log.e() 都优化删除掉.不打包进软件

原理

通过proguard删除各种日志输出代码。然后编译出apk时,将会删除掉日志代码。

通过配置proguard,将类android.util.Log的方法给置为无效代码。(proguard是一个代码优化的工具,也可以混淆代码)

assumenosideeffects

assumenosideeffects,assume no side effects;假定无效

assumenosideeffects的官方说明:

In the optimization step, ProGuard will then remove calls to such methods, if it can determine that the return values aren't used.ProGuard will analyze your program code to find such methods automatically.It will not analyze library code, for which this option can therefore be useful.

In general, making assumptions can be dangerous; you can easily break the processed code. Only use this option if you know what you're doing!

proguard添加以下配置:

-assumenosideeffects class android.util.Log { public static boolean isLoggable(java.lang.String, int); public static int v(...); public static int i(...); public static int w(...); public static int d(...); public static int e(...); }

使用此配置要注意-dontoptimize不能有。

方法

  1. 打开项目build.gradle

    android.buildTypes.release里
    >minifyEnabled true
    >proguardFiles getDefaultProguardFile('**proguard-android-optimize.txt**'), 'proguard-rules.pro'
    或 > proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

proguard-android-optimize.txt 为 sdk>tools>proguard里的默认文件

  1. proguard-android.txt中添加

    -assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
    }

    且配置里不能有-dontoptimize。

  2. 打包运行看日志

效果检验

  1. 使用AS打开项目的app\build\outputs\apk的文件,解压classes.dex

  2. 使用工具dex2jar转成jar文件

  3. 再使用jd-gui查看jar的反编译代码,可以看到程序中的Log代码都消失了.

PreviousFull Kotlin ReferenceNextThread

Last updated 5 years ago

Was this helpful?