I am new to java and I need to manipulate java bytecode for some purposes (see this). Java bytecode manipulation need following imports:

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

I resolved org.objectweb.asm by downloading ASM package from asm website and related imports have been resolved.
I don't know how to resolve java.lang.instrument, My default ADT bundle hasn't it:

enter image description here

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. Just like AWT and Swing. Have a look at this question:

Android & Dalvik - Get the size of an object

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. There's no contract there that says they have to support the standard Java library.

Sorry :(

其他提示

You're out of luck – find a way that does not rely on java.lang.instrument. java.lang.instrument is part of Java SE, but is not available on Android because of fundamental limitations of Dalvik.

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. .class files do not appear on Android at all.

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

Javassist is another tools to manipulate java bytecode. There is already someone who tried to use javassist in android. 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). Manipulation on compile time is a different story, because java .class file generated first before converted to Dalvik bytecode. So if you modify .class file before being dexed, the dexed classes will be the modified one.

This article also worth reading, because it noted of ASMDEX which claim can manipulate DEX bytecode.

The java.lang.instrument package was removed. So you can't perform bytecode manipulation at runtime. However you can perform bytecode manipulation at build time with javaassist or ASM.

This sample project performs bytecode manipulation. It's usage is discussed here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top