Reprinted from See Snow http://bbs.pediy.com/showthread.php?t=189610, thank the original author to share!
The beginning of the mobile side, please predecessors a lot of advice!
This article refer to http://www.kaifazhe.com/android_school/380973.html
Here, thank the author!
Tracking APK The general practice is to insert the log output in the Smali code of the decompile, and then recompile the run to see the output log, this method is laborious, if you can dynamically debug the best. The following is to introduce Apk+eclipse to debug Smali.
Pre-Preparation:
Eclipse.
JDK or JRE, and set the environment variable, mine is version 7.0.
Apktool2.0
Signature file
Target apk:light.apk.
1. Copy the light.apk to the Apktool directory,
Use Apktool to decompile light.apk.
Command:
D:\apktool2.0>java-jar Apktool.jar d-d light.apk-o out (you must use the-D parameter, so that the decompile code suffix is Java, because only Java files can be debugged by Eclipse),
In the current directory, a post-compilation out file directory is generated:
2. Set debug tags and find main class
In the output Out folder, open Androidmanifest.xml with the text Editing tool and set the property android:debuggable= "true" in the application node.
Continue in Androidmanifest.xml, search for the following keywords
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
Locate the Activity node that contains the above information, and record the value of its Android:name property, which is the main class to which it is applied. The main class of this example is com.devuni.flashlight.MainActivity.
3. Add a debug wait in the OnCreate event of the main class.
Open the main class file with the text Editing tool, find the OnCreate method, insert invoke-static {}, Landroid/os/debug;->waitfordebugger () V before the first sentence, and remember to add a=0;// The prefix remains up and down, and the result is as follows:
4. Save the file and use Apktool to recompile the package to debug.apk
Command:
D:\apktool2.0>java-jar Apktool.jar b-d Out-o debug.apk
Generate the debug.apk file:
5. Sign the debug.apk (need to download the signature tool), I put the signature tool under the signapk folder, generate DEBUG.SIGN.APK
Command:
D:\apktool2.0>java-jar. \sign\signapk. JAR. \sign\testkey. X509. PEM. \sign\testke
Y.pk8 DEBUG. APK DEBUG. Sig. APK
6. Upload DEBUG.SIGN.APK to phone or simulator, then install and run. At this point you will see the program to stay in the white screen interface, then do not move the device and quit the program, because the program is now running to just add the Waitfordebugger code here, this line of code means to hang up, wait for the debugger.
The following starts setting up the environment for real-time debugging.
7. Enter the Out folder of the 1th step, and delete the build and Dist folder inside, which is generated when Apktool compiles the APK.
8. Start Eclipse, build a Java project
1) Java project, Project, File---New
2) Project name is random, use the default location option removed, location selects the Out folder, and next
3) Set the Smali folder to source, then finish
9. In Eclipse, open the main class found in step 2nd and find the OnCreate method, starting with the first method after Waitfordebugger to add a breakpoint. Such as
10. Open Ddms, if you run the modified program in the 6th step, the program that can be debugged is displayed in the DDMS device list.
The last column of the corresponding program is 8602/8700, where 8602 is the port where the program is debugged.
11. Now all you have to do is associate the code with the debugger. Back to eclipse, configuring remote debugging
1) Debug configurations, menu run, debug
2) Double-click the remote Java application,host default localhost on the line, port fill the 10th step to get 8602, and then apply, Debug.
12. Eclipse automatically switches to the debug view and sees that the program has run and breaks the executable code on the next line, and the related variables can be viewed directly.
You can already debug Smali with Eclipse, and the example above starts with debugging from the beginning of the program, but it's really troublesome to debug the code where you care. It is recommended to use Jd-gui and other software directly to view the anti-compiled Java code, determine the location to debug, then enter the Smali location breakpoint and debugging in real time, you can do more with less.
Android Dynamic Debug Samli code