🟢 शुरुआत · Lesson 08
Logcat, Debugging और Emulator
Log.d वगैरह से logging
Phone पर println() ज़्यादा काम नहीं आता। Log class use करें — messages Logcat window में time, tag और level के साथ दिखते हैं।
Kotlin
import android.util.Log
private const val TAG = "NotesScreen"
Log.v(TAG, "verbose – very detailed")
Log.d(TAG, "debug – notes loaded: ${notes.size}")
Log.i(TAG, "info – user opened editor")
Log.w(TAG, "warning – reminder time is in the past")
Log.e(TAG, "error – could not save note", exception) // prints the stack trace too| Level | Function | किसके लिए |
|---|---|---|
| Verbose | Log.v | सब कुछ (कम ज़रूरत) |
| Debug | Log.d | Development में values |
| Info | Log.i | ज़रूरी normal events |
| Warn | Log.w | कुछ अजीब पर handled |
| Error | Log.e | Failures, exception के साथ |
Logcat filter करना
Logcat filter box
package:mine # only your app tag:NotesScreen # only one tag level:error # errors and above package:mine level:warn # combine filters "notes loaded" # free text search
Crash पढ़ना (stack trace)
जब app "App keeps stopping" के साथ बंद हो, Logcat में लाल FATAL EXCEPTION दिखता है। ऊपर से पढ़ें और आपके package वाली पहली line ढूँढें।
Kotlin – buggy code
class MainActivity : AppCompatActivity() {
private lateinit var tvTitle: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
tvTitle.text = "Hello" // line 18 – used before it was set!
setContentView(R.layout.activity_main)
}
}Logcat – crash
FATAL EXCEPTION: main
Process: com.example.helloapp, PID: 12345
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloapp/com.example.helloapp.MainActivity}:
kotlin.UninitializedPropertyAccessException: lateinit property tvTitle has not been initialized
at com.example.helloapp.MainActivity.onCreate(MainActivity.kt:18) ← YOUR file and line
at android.app.Activity.performCreate(Activity.java:8595)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3790)
...
- 1लाल "FATAL EXCEPTION" ढूँढें
- 2Exception type और message पढ़ें (क्या गलत हुआ)
- 3पहली "at com.your.package…(File.kt:line)" ढूँढें (कहाँ)
- 4नीले file link पर click → Android Studio उसी line पर ले जाता है
- 5Fix करें, फिर चलाएँ
| Exception | आम कारण |
|---|---|
| NullPointerException / UninitializedPropertyAccessException | View या variable set होने से पहले use करना |
| ActivityNotFoundException | Activity AndroidManifest.xml में नहीं |
| NetworkOnMainThreadException | Main thread पर internet call |
| Resources.NotFoundException | String resource ID की जगह number देना (setText(5)) |
| IllegalStateException: Cannot access database on the main thread | Coroutine के बिना Room query |
Breakpoints और debugger
- 1Line number के बाईं ओर gutter में click → लाल dot (breakpoint)
- 2▶ Run की जगह bug icon 🐞 (Debug) दबाएँ
- 3App उस line पर रुकती है — Variables panel में सारी values देखें
- 4Step Over (F8) अगली line चलाता है · Step Into (F7) function के अंदर जाता है
- 5Resume (F9) अगले breakpoint तक चलाता है
Layout Inspector
Tools → Layout Inspector चलती app का live view tree 3D में दिखाता है। जब कोई view दिख न रहा हो, size 0 हो या दूसरे view के पीछे छिपा हो तब use करें।
Emulator power tips
- ⋯ Extended controls panel से fake GPS location, battery level और network speed set कर सकते हैं।
- State saving test करने के लिए rotate buttons से rotate करें।
- Image file को emulator पर drag करें ताकि gallery में आ जाए (photo-picker testing के लिए)।
- Emulator अटक जाए तो Device Manager से Cold boot करें।
Debugging routine
आम गलतियाँ
- Stack trace की सिर्फ़ आख़िरी line पढ़ना → कारण ऊपर के पास (और "Caused by:" में) होता है।
- Private data (passwords, tokens) log करना।
- Release build debug करना → breakpoints काम नहीं करते; debug build use करें।
सारांश
Log.d/i/w/e(TAG, msg)+ Logcat filters (package:mine, tag:, level:)।- Crash = FATAL EXCEPTION → type + आपके package की पहली line।
- Values देखने के लिए Breakpoints + Debug ▶; UI problems के लिए Layout Inspector।
✅ Practice
ऊपर वाला crash जानबूझकर बनाइए, Logcat में पढ़िए, link से line पर जाकर fix कीजिए। फिर click listener में breakpoint लगाकर कोई variable inspect कीजिए।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
मेरा Logcat खाली क्यों है?
Logcat के ऊपर सही device और app process चुना है या नहीं, और आपका filter (जैसे package:mine) messages छिपा तो नहीं रहा, यह check करें।
क्या release से पहले Log statements हटाने चाहिए?
Passwords या personal data कभी log न करें। Debug logs नुकसानदेह नहीं पर शोर करते हैं; कई apps release builds में R8 rules से हटाती हैं या if (BuildConfig.DEBUG) में रखती हैं।
ANR क्या है?
Application Not Responding: main thread लगभग 5 seconds block रहा, आमतौर पर network या database काम से। उस काम को background dispatcher वाले coroutine में ले जाएँ।
