Neacy / Koala

从 Java 字节码到 ASM 实践

Home Page:https://www.jianshu.com/p/c2c1d350d245

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Koala

Download

相关博文

在开发之前学习了一些 Java 字节码、Java 虚拟机、ASM 的相关知识,梳理成博文如下,请多多指教

简介

  • 在开发项目的时候,经常会想看一个方法的入参、返回结果和执行耗时,我们通常的做法是打日志、打断点调试去看,这样做确实可以达到我们的目的,但是效率是低下的。
  • JakeWharton 大神的 hugo 库便具有这样的功能,可以打印方法的入参、返回结果和执行耗时。
  • Koala 也具有和 hugo 同样的功能,可以打印方法的入参、返回结果和执行耗时。Koala 和 hugo 都是基于 AOP **,和 hugo 不同的是,hugo 使用 AspectJ 技术实现的,而 Koala 是使用 ASM 修改字节码实现的。

添加依赖

添加 Koala Gradle Plugin 依赖

在项目工程的 build.gradle 中添加如下代码:

    buildscript {
        repositories {
            maven {
                url "https://plugins.gradle.org/m2/"
            }
        }
        dependencies {
            classpath "gradle.plugin.com.lijiankun24:buildSrc:1.1.1"
        }
    }

在需要使用的 module 中的 build.gradle 中添加如下代码:

    apply plugin: "com.lijiankun24.koala-plugin"

添加 Koala 依赖

Gradle:

    compile 'com.lijiankun24:koala:x.y.z'

Maven:

    <dependency>
        <groupId>com.lijiankun24</groupId>
        <artifactId>koala</artifactId>
        <version>x.y.z</version>
        <type>pom</type>
    </dependency>

替换上面的 xyz为最新的版本号: Download

使用

使用起来还是非常简单的,在 Java 的方法上添加 @KoalaLog 注解,如下所示:

    @KoalaLog
    public String getName(String first, String last) {
        SystemClock.sleep(15); // Don't ever really do this!
        return first + " " + last;
    }

当上述方法被调用的时候,Logcat 中的输出如下所示:

09-04 20:51:38.008 12076-12076/com.lijiankun24.practicedemo I/0KoalaLog: ┌───────────────────────────────────------───────────────────────────────────------
09-04 20:51:38.008 12076-12076/com.lijiankun24.practicedemo I/1KoalaLog: │ The class's name: com.lijiankun24.practicedemo.MainActivity
09-04 20:51:38.008 12076-12076/com.lijiankun24.practicedemo I/2KoalaLog: │ The method's name: getName(java.lang.String, java.lang.String)
09-04 20:51:38.008 12076-12076/com.lijiankun24.practicedemo I/3KoalaLog: │ The arguments: [li, jiankun]
09-04 20:51:38.008 12076-12076/com.lijiankun24.practicedemo I/4KoalaLog: │ The result: li jiankun
09-04 20:51:38.008 12076-12076/com.lijiankun24.practicedemo I/5KoalaLog: │ The cost time: 15ms
09-04 20:51:38.008 12076-12076/com.lijiankun24.practicedemo I/6KoalaLog: └───────────────────────────────────------───────────────────────────────────------

混淆规则

 -keep class com.lijiankun24.koala.** { *; }

About Me

鸣谢

感谢以下四个库和它的作者:

License

 Copyright 2018, lijiankun24

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

About

从 Java 字节码到 ASM 实践

https://www.jianshu.com/p/c2c1d350d245


Languages

Language:Java 80.3%Language:Groovy 19.7%