0

Please edit the question if needed.

I am Using android UiAutomator.

UiAutomation run perfectly , but I want use java reflection on UiAutomator.jar (Which internally contain claasses.dex)

So problem is that java reflection API use some.jar file (which contain xyz.class instead of .dex)

So how can access this .dex file from UiAutomationTest.jar

I am using this tool which convert .dex to .jar( which contain .class files) Link : how to use DEXtoJar

I need it programatically. -

Now i am unable to load UiAutomator.jar into memory ( for getting .dex file from jar then again convert to claasses.dex to classes.jar-)

so it is givng following error.

Code snip I am using.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Here I am placed my UiAutomationTest.jar which produce from UiAutomation ant build tool.
        String sourceDir = Environment.getExternalStorageDirectory().toString()+"/UiAutomationTest.jar"; //getApplicationInfo().sourceDir;
        try {
            DexFile dexFile = new DexFile(sourceDir);  // Exception propagate from here.
        } catch (IOException e) {
            e.printStackTrace();
        }

        //exploreJar();  // For exploring jar from dex.
    }

Logcat message:

07-31 15:50:18.339: E/dalvikvm(19748): Dex cache directory isn't writable: /data/dalvik-cache
07-31 15:50:18.339: W/System.err(19748): java.io.IOException: unable to open DEX file
07-31 15:50:18.339: W/System.err(19748):    at dalvik.system.DexFile.openDexFile(Native Method)
07-31 15:50:18.339: W/System.err(19748):    at dalvik.system.DexFile.<init>(DexFile.java:78)
07-31 15:50:18.339: W/System.err(19748):    at com.example.dextestproject.MainActivity.onCreate(MainActivity.java:35)
07-31 15:50:18.349: W/System.err(19748):    at android.app.Activity.performCreate(Activity.java:5008)
07-31 15:50:18.349: W/System.err(19748):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-31 15:50:18.349: W/System.err(19748):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2026)
07-31 15:50:18.349: W/System.err(19748):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
07-31 15:50:18.349: W/System.err(19748):    at android.app.ActivityThread.access$600(ActivityThread.java:133)
07-31 15:50:18.349: W/System.err(19748):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
07-31 15:50:18.349: W/System.err(19748):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 15:50:18.349: W/System.err(19748):    at android.os.Looper.loop(Looper.java:137)
07-31 15:50:18.349: W/System.err(19748):    at android.app.ActivityThread.main(ActivityThread.java:4803)
07-31 15:50:18.349: W/System.err(19748):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 15:50:18.349: W/System.err(19748):    at java.lang.reflect.Method.invoke(Method.java:511)
07-31 15:50:18.349: W/System.err(19748):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-31 15:50:18.349: W/System.err(19748):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-31 15:50:18.359: W/System.err(19748):    at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85

1 Answers1

4

as I learned from here Is there a way to get the class names in a given classes.dex file?

you should use:

File f = new File(Environment.getExternalStorageDirectory()+"/classes.zip");
File tmp = new File(Environment.getExternalStorageDirectory()+"/classes.odex");

DexFile dexfile = DexFile.loadDex(f.getAbsolutePath(), tmp.getAbsolutePath(), 0);

instead of:

DexFile dexfile = new DexFile(f);
Community
  • 1
  • 1
Hadi tavakoli
  • 1,267
  • 2
  • 16
  • 30
  • Note after calling `DexFile.loadDex` once with `outputPathName` being the exact same path and filename, but an `odex` extension instead of `zip`; `new DexFile("....zip")` will also magically start working. [source](https://android.googlesource.com/platform/dalvik/+/android-4.4.4_r2/vm/JarFile.cpp#185) – TWiStErRob Mar 30 '20 at 01:25