1 Star 0 Fork 0

Never/Needle

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

Needle

Needle 是基于字节码修改技术实现的Android java 类替换工具。

类替换

在日常开发中,工程中存在多个公共模块或者JAR包,很多是公司内部基建产物或者其他第三方SDK。在定制化开发过程中,无法灵活修改这些公共库。 且传统插桩方案有如下问题:

  1. 寻找合适插桩位置困难,严重依赖对象的内部逻辑,版本升级后点位失效可能性高
  2. 运行时侵入代码较多,增加运行时耗时
  3. 方法、参数或整个类逻辑替换等复杂场景实现困难
  4. 侵入代码无OO语法约束,容易产生运行时错误

基于以上问题,如果能够实现编译期间类替换,即可实现简单,快速的公共逻辑替换能力。

开始使用

引用配置

添加工程根目录脚本插件引用

buildscript {
    repositories {
        ...
    }

    dependencies {
        // classpath
        ...
        classpath 'never.end.needle:needle-plugin:0.1'
    }
}

项目模块脚本引用

apply plugin: 'never.end.needle-plugin'
android {
    ...
}

dependencies {
    implementation 'never.end.needle:needle-lib:0.1'
    annotationProcessor 'never.end.needle:needle-compiler:0.1'
}

使用

Needle使用注解指定要替换的类,且注入类必须是被替换类的子类。

@TargetClass("never.end.needle.sample.FirstActivity\$StaticInnerClass")
class HookInnerClass : FirstActivity.StaticInnerClass() {
    fun printMyself() {
        Log.d("HookInnerClass", "this is HookInnerClass")
    }
}

适用场景

  1. 替换方式:在编译期完成代码中所有对象的多态替换。
  2. 替换要求:必须是子类替换父类,编译后没有任何副作用
  3. 替换范围:支持源代码替换和未混淆的jar、aar包替换
BSD 3-Clause License Copyright (c) 2021-2024, Never End Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

Needle 是基于字节码修改技术实现的Android java类编译器全局替换工具。 展开 收起
Java 等 2 种语言
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/never0end/needle.git
git@gitee.com:never0end/needle.git
never0end
needle
Needle
Release

搜索帮助

Cb406eda 1850385 E526c682 1850385