简体   繁体   中英

No java imports in smali?

I was doing some experiments today in android source.

Let me tell the complete thing, I compiled framework.jar from android source and decompiled it and generated smali source and kept it aside. Then from CyanogenMod repo I added the commits of a feature to android source and compiled framework.jar again and again decompiled smali source to see the changes in terms of smali so that I can port them over to my ROM.

The feature I am porting requires importing of certain classes eg import dalvik.system.VMRuntime and extra coding for utilization of those imported classes. So now my problem is, I am only able to see the extra coding ie utilization of those classes in the smali code but not those imports. So when I port only the smali code I get java.lang.NoSuchMethodError in logcat which shows that it is unable to find that method. The reason is clear because the necessary class is not imported then how to do it in smali code? i see no way to do that in smali and due to which the newly introduced methods don't work.

Any feasible solution to this?

The only thing an import does in java is make it so that you can mention a class without having to specify the full package name. In smali, there are no imports - everything always uses the fully qualified class name that includes the package.

As such, your problem is definitely not related to imports. It sounds like you are trying to use a method that simply doesn't exist on the device.

You can disassemble the framework jars from your device and find the definition of the dalvik.system.VMRuntime and see what methods are available. Or alternately add some reflection calls and log the info to logcat.

It's worth noting that VMRuntime is not part of the public API, and it may not be present or consistent on all devices or future versions of Android.

(I don't know smali, so there may be a much better solution)

No Java program ever requires any import statement. To use eg ArrayList you need to either import it or refer to it in full, as java.util.ArrayList.

There is a significant difference between eg a C++ #include and a Java import. A C++ #include directly inserts code in your program, typically the declaration for a class you are using. The process of getting the declarations is divided into two stages in Java. First the compiler determines the fully qualified class name, then it uses its own library and the classpath to find the declaration for that name. Java import is used only in calculating the fully qualified class name, and so is not needed for any class that is only referred to by its fully qualified name.

Perhaps you could pre-process the code you are adding to replace eg VMRuntime with dalvik.system.VMRuntime etc. so that you can compile it with no imports.

Here is an example of a short program that uses java.util classes, in different ways, without any import:

public class Test {
  public static void main(String[] args) {
    java.util.List<String> list = new java.util.ArrayList<String>();
    list.add("bbb");
    list.add("aaa");
    java.util.Collections.sort(list);
    System.out.println(list);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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