简体   繁体   English

Android和java字节码操作

[英]Android and java bytecode manipulation

I am new to java and I need to manipulate java bytecode for some purposes (see this ). 我是java的新手,我需要为某些目的操纵java字节码(见这个 )。 Java bytecode manipulation need following imports: Java字节码操作需要以下导入:

  1. org.objectweb.asm org.objectweb.asm
  2. java.lang.instrument java.lang.instrument中

I resolved org.objectweb.asm by downloading ASM package from asm website and related imports have been resolved. 我通过从asm网站下载ASM包解决了org.objectweb.asm并解决了相关的导入问题。
I don't know how to resolve java.lang.instrument, My default ADT bundle hasn't it: 我不知道如何解决java.lang.instrument,我的默认ADT包没有它:

在此输入图像描述

How do I resolve this import? 如何解决此导入问题? Should I download any library? 我应该下载任何图书馆吗? from where? 从哪里?
any help would be appreciated. 任何帮助,将不胜感激。
Thanks. 谢谢。

The java.lang.instrument package isn't available for Android. java.lang.instrument包不适用于Android。 Just like AWT and Swing. 就像AWT和Swing一样。 Have a look at this question: 看看这个问题:

Android & Dalvik - Get the size of an object Android和Dalvik - 获取对象的大小

But it makes sense. 但这是有道理的。 Android apps are written at the source level in Java, but they don't run on the JVM -- they run on the dalvik VM. Android应用程序是在Java源代码级别编写的,但它们不在JVM上运行 - 它们在dalvik VM上运行。 There's no contract there that says they have to support the standard Java library. 那里没有合同说他们必须支持标准的Java库。

Sorry :( 对不起:(

You're out of luck – find a way that does not rely on java.lang.instrument . 你运气不好 - 找到一种不依赖于java.lang.instrument java.lang.instrument is part of Java SE , but is not available on Android because of fundamental limitations of Dalvik. java.lang.instrument是Java SE的一部分 ,但由于Dalvik的基本限制,因此在Android上不可用。

The java.lang.instrument package was removed from dalvik core library, because this package makes a fundamental assumption that the execution format being used by the VM is a .class file . java.lang.instrument包已从dalvik核心库中删除,因为该软件包基本假设VM使用的执行格式是.class file .class files do not appear on Android at all. .class文件根本不会出现在Android上。

https://groups.google.com/forum/#!topic/android-developers/MR4W2roQ3Xw https://groups.google.com/forum/#!topic/android-developers/MR4W2roQ3Xw

Javassist is another tools to manipulate java bytecode. Javassist是另一种操作java字节码的工具。 There is already someone who tried to use javassist in android . 已经有人试图在android中使用javassist You might want to try it. 你可能想尝试一下。 As far as I know, bytecode manipulation on android runtime isn't possible, except in instrumentation (usually for testing). 据我所知,Android运行时的字节码操作是不可能的,除了仪器(通常用于测试)。 Manipulation on compile time is a different story, because java .class file generated first before converted to Dalvik bytecode. 编译时的操作是一个不同的故事,因为java .class文件在转换为Dalvik字节码之前首先生成。 So if you modify .class file before being dexed, the dexed classes will be the modified one. 因此,如果在dexed之前修改.class文件,则dexed类将是修改后的类。

This article also worth reading, because it noted of ASMDEX which claim can manipulate DEX bytecode. 这篇文章也值得一读,因为它注意到ASMDEX声称可以操作DEX字节码。

The java.lang.instrument package was removed. 删除了java.lang.instrument包。 So you can't perform bytecode manipulation at runtime. 因此,您无法在运行时执行字节码操作。 However you can perform bytecode manipulation at build time with javaassist or ASM. 但是,您可以使用javaassist或ASM在构建时执行字节码操作。

This sample project performs bytecode manipulation. 示例项目执行字节码操作。 It's usage is discussed here . 它的用法在这里讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM