在Android中动态加载类

news/2025/12/7 22:32:51/文章来源:https://www.cnblogs.com/zshsboke/p/19319024

在Android中动态加载类

较为复杂

编写Hello类

package main;
public class Hello {public static void main(String[] args) {System.out.println("Hello World");}public String welcome(){return "zsh";}
}

运行

java Hello.java

编译运行

javac Hello.java
# 将Hello.class 放入main目录
java main.Hello

or

javac -d . Hello.java
java main.Hello

使用R8

D:\SoftWare\Android\Sdk\build-tools\36.0.0> ./d8 C:\Users\29051\Desktop\main\Hello.class --output C:\Users\29051\Desktop\main.zip

push到cache目录

/data/data/io.github.okhttplearn/cache/classes.dex

命令非常短少

Usage: d8 [options] [@<argfile>] <input-files>where <input-files> are any combination of dex, class, zip, jar, or apk filesand each <argfile> is a file containing additional arguments (one per line)and options are:--debug                 # Compile with debugging information (default).--release               # Compile without debugging information.--output <file>         # Output result in <file>.# <file> must be an existing directory or a zip file.--globals <file>        # Global synthetics <file> from a previous intermediate compilation.# The <file> may be either a zip-archive of global synthetics or the# global-synthetic files directly.--globals-output <file> # Output global synthetics in <file>.# <file> must be an existing directory or a non-existent zip archive.--lib <file|jdk-home>   # Add <file|jdk-home> as a library resource.--classpath <file>      # Add <file> as a classpath resource.--min-api <number>      # Minimum Android API level compatibility (default: 1).--pg-map <file>         # Use <file> as a mapping file for distribution.--intermediate          # Compile an intermediate result intended for later# merging.--file-per-class        # Produce a separate dex file per class.# Synthetic classes are in their own file.--file-per-class-file   # Produce a separate dex file per input .class file.# Synthetic classes are with their originating class.--no-desugaring         # Force disable desugaring.--desugared-lib <file>  # Specify desugared library configuration.# <file> is a desugared library configuration (json).--desugared-lib-pg-conf-output <file># Output the Proguard configuration for L8 to <file>.--main-dex-rules <file> # Proguard keep rules for classes to place in the# primary dex file.--main-dex-list <file>  # List of classes to place in the primary dex file.--main-dex-list-output <file># Output resulting main dex list in <file>.--force-enable-assertions[:[<class name>|<package name>...]]--force-ea[:[<class name>|<package name>...]]# Forcefully enable javac generated assertion code.--force-disable-assertions[:[<class name>|<package name>...]]--force-da[:[<class name>|<package name>...]]# Forcefully disable javac generated assertion code.# This is the default handling of javac assertion code# when generating DEX file format.--force-passthrough-assertions[:[<class name>|<package name>...]]--force-pa[:[<class name>|<package name>...]]# Don't change javac generated assertion code. This# is the default handling of javac assertion code when# generating class file format.--force-assertions-handler:<handler method>[:[<class name>|<package name>...]]--force-ah:<handler method>[:[<class name>|<package name>...]]# Change javac and kotlinc generated assertion code# to invoke the method <handler method> with each# assertion error instead of throwing it.# The <handler method> is specified as a class name# followed by a dot and the method name.# The handler method must take a single argument of# type java.lang.Throwable and have return type void.--thread-count <number> # Use <number> of threads for compilation.# If not specified the number will be based on# heuristics taking the number of cores into account.--map-diagnostics[:<type>] <from-level> <to-level># Map diagnostics of <type> (default any) reported as# <from-level> to <to-level> where <from-level> and# <to-level> are one of 'info', 'warning', or 'error'# and the optional <type> is either the simple or# fully qualified Java type name of a diagnostic.# If <type> is unspecified, all diagnostics at# <from-level> will be mapped.# Note that fatal compiler errors cannot be mapped.--android-platform-build# Compile as a platform build where the runtime/bootclasspath# is assumed to be the version specified by --min-api.--art-profile <input> <output># Rewrite human readable ART profile read from <input> and write to <output>.--startup-profile <file># Startup profile <file> to use for dex layout.--version               # Print the version of d8.--help                  # Print this message.

方法调用

val dexFile = File(context.cacheDir, "classes.dex")
val dexClassLoader = DexClassLoader(dexFile.absolutePath, null, null, context.classLoader)
val loadedClass: Class<*> = dexClassLoader.loadClass("main.Hello")
val instance: Any = loadedClass.getDeclaredConstructor().newInstance()
val welcomeMethod: Method = loadedClass.getMethod("welcome")
val result: String? = welcomeMethod.invoke(instance) as? String
Log.i(TAG, "WorldScreen -> result: $result, codeCacheDir: ${context.codeCacheDir}")
val mainMethod: Method = loadedClass.getMethod("main", Array<String>::class.java)
val result1 = mainMethod.invoke(instance, arrayOf<String>("zsh1", "zsh2"))
Log.i(TAG, "WorldScreen -> result1: $result1")

你会惊奇的发现,niubility
你会发现zygote64做了什么奇怪的动作.

  • MD5校验
  • 输出The ClassLoaderContext is a special shared library.
  • Increasing code cache capacity to 1024KB

而且生成了一个奇怪的目录oat.
image
这些文件有什么作用呢?
想要解密这些,还是相当复杂的需要了解art虚拟机的知识.

路漫漫其修远兮,吾将上下而求索!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/991867.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Flutter for HarmonyOS 创建指南(一):环境搭建与项目创建

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025最新西双版纳地接社TOP5评测!品牌实力+服务口碑权威榜单发布,专业赋能品质旅行体验

随着西双版纳成为国内外游客热门的旅游目的地,选择一家靠谱的地接社成为提升旅行体验的关键。本榜单基于综合实力、资源优势、服务口碑、定制能力四大维度,结合行业数据及游客反馈,权威解析2025年五大西双版纳地接社…

详细介绍:[特殊字符] 微前端部署实战:Nginx 配置 HTTPS 与 CORS 跨域解决方案(示例版)

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

Git预提交钩子实现代码美化自动化

本文介绍如何使用Git的pre-commit钩子结合astyle工具,在提交代码前自动美化代码格式,尤其适用于需要遵循特定编码规范(如Linux内核风格)的开发场景,提高代码质量和一致性。大家好。 在这篇文章中,我将分享我创建…

五、Java数组

本文系统讲解Java数组的核心概念与应用,涵盖数组声明创建、内存分析、多维数组、Arrays工具类及稀疏数组实现,适合Java初学者夯实基础,并为后续学习集合框架奠定坚实基础。1. 数组概述数组是相同数据类型的有序集合…

122_尚硅谷_init函数

122_尚硅谷_init函数1.init基本介绍 2.如果一个文件同时包含全局变量, init函数和main函数, 那么执行的顺序是:全局变量定义_init函数_main函数 3.utils包中有init会在main中导入后,先执行utils.go文件中的init

《安全测试指南》——会话管理测试【学习笔记】

【会话管理测试】*部分括号内容均为个人理解 。 1. 会话管理架构绕过测试(OTG-SESS-001)主要测试会话cookie是否不可预测。攻击模式:cookie搜集(搜集足够数量的cookie样本)cookie逆向工程(分析Cookie生成算法) …

氛围编程工具个人推荐

氛围编程工具个人推荐大模型推理能力(脑子) > 编辑器功能(手)。它更推崇拥有强大逻辑架构能力的模型(如Claude),而不是单纯的IDE集成工具。第一梯队 (First Echelon):主力军这一层级是作者认为完成“Vibe C…

Windows 11全面AI化:语音助手与自主代理技术解析

某中心对Windows 11进行大规模改造,引入了“Hey Copilot”语音唤醒、可分析屏幕内容的Copilot Vision以及能自主操控电脑完成任务的Copilot Actions等AI功能。这些技术构建了全新的人机交互模式,并涉及新的安全架构以…

20251207

明天试试自己独立完成老师布置的作业

MyBatis自定义拦截器

在Spring Boot中配置MyBatis拦截器的核心是将自定义拦截器注册到MyBatis的SqlSessionFactory中(MyBatis所有拦截器都需通过该工厂注册才能生效)。以下是完整、分场景的配置方案,包含基础配置、多拦截器、属性定制、…

网线大鲨鱼

使用wireshark打开下载文件,搜索字符串ctf,选择分组字节流发现flag,/?s=moectf%7Bw1r3shark_1s_s0_3asy%7D&_pjax=%23page 解码获得最终flag:moectf{w1r3shark_1s_s0_3asy}

深入解析:mysql内置函数——了解常用的函数

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

【P1】win10安装 Docker教程 - 详解

【P1】win10安装 Docker教程 - 详解2025-12-07 22:11 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !imp…

csq-蓝桥杯python-基础语法1-逻辑运算与条件语句

csq-蓝桥杯python-基础语法1-逻辑运算与条件语句一、什么是逻辑运算?在 Python 中,比较运算(如 > < ==)的结果是布尔值 True 或 False。 实际开发中经常需要组合多个条件,例如:年龄大于 18 并且身高大于 1…

高级语言程序设计第八次个人作业

这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/gjyycx 这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/gjyycx/homework/15590 学号:102400111 姓名:蔡伟仡 一.编写并运行书本第11章11.13编…

Cor1e的支票

打开下载文件,看见。!?三个符号,想到OoK加密将。改成OoK.,!改成OoK!,?改成OoK?在https://www.dcode.fr/ook-language 解密,获得flag

卷积神经网络是从多层感知机基础上发展起来的吗?

卷积神经网络是从多层感知机基础上发展起来的吗? 在深度学习的发展历程中,卷积神经网络(CNN)和多层感知机(MLP)是两大核心架构,二者时常被放在一起比较。一个常见的疑问是:卷积神经网络是否从多层感知机的基础…

gaussdb json解析

gaussdb (GaussDB Kernel 503.1.0.SPC1200 build c28d95e9) -- 序列 CREATE SEQUENCE seq_head_id START WITH 1; CREATE SEQUENCE seq_line_id START WITH 1;-- 头表 CREATE TABLE head_t (head_id BIGINT PRIMARY KE…