For the purpose of developing an Anti-malware on Android, I need to have the smali
representation of an APK.
I'm trying to execute the the baksmali.jar
directly on the device. I asked the author of the tool GitHub: Executing baksmali on an Android device.
I followed the steps according to the post Stack overflow: How to execute the dex file in android with command?
dx --dex --output=classes.dex baksmali.jar
I used the dx
tool from android/Sdk/build-tools/25.0.2
zip baksmali.zip classes.dex
adb push baksmali.zip /sdcard/
I'm using a genymotion emulator custom phone-4.4.4 API 19
adb shell mkdir /sdcard/dalvik-cache
adb shell ANDROID_DATA=/sdcard dalvikvm -cp /sdcard/baksmali.zip org.jf.baksmali.Main
After that, I'm getting this error message Unable to locate class 'org/jf/baksmali/Main'
Precisely
Unable to locate class 'org/jf/baksmali/Main'
java.lang.NoClassDefFoundError: org/jf/baksmali/Main
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.jf.baksmali.Main" on path: DexPathList[[zip file "/sdcard/baksmali.zip"],nativeLibraryDirectories=[/system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 1 more
Suppressed: java.io.IOException: unable to open DEX file
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:296)
at dalvik.system.DexFile.<init>(DexFile.java:80)
at dalvik.system.DexFile.<init>(DexFile.java:59)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:263)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:230)
at dalvik.system.DexPathList.<init>(DexPathList.java:112)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:38)
at java.lang.ClassLoader.createSystemClassLoader(ClassLoader.java:116)
at java.lang.ClassLoader.access$000(ClassLoader.java:65)
at java.lang.ClassLoader$SystemClassLoader.<clinit>(ClassLoader.java:81)
at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:125)
... 1 more
I don't understand this error, the classe path is correct.
How can I fix this ?
Thank you in advance.
After moving around, I finally found the solution to the following problem:
" execute the baksmali.jar
in an android device, ideally the execution is launched by an application targeting another application."
So, lets say we wont to develop an application that is able to "baksmali" (disassemble) another application, without requiring any special permission.
These are the steps to get the job done:
baksmali.jar v1.2.3
jar
to the app/libs
directory of the android studio projectjar
and select the option "Add it as a Library"
(use the "Project->project" view to see the libs
directory, you may not see it in the "Project->android" view)org.jf.baksmali.main.main
methode by passing the CLI
arguments to that method using a String[]
Here is a helloWorld App illustrating the use cas:
After launchig the application you should see folders in the /data/data/com.example.bartman.baksmalihelloworld/<name_of_baksmali_output>