How to programmatically tell if my app is being debugged? - android

I'm looking for a way to tell if my app is running under the debugger or running "normally", under Android Studio..
These two cases are
1. Run the app by clicking the "Run app" button (green arrow)
2. Run the app by clicking the "Debug app" button (gear icon)
I would like to output more verbose diagnostics (using Log.*) while debugging.
I tried checking BuildConfig.DEBUG but that is TRUE in either case. I suspect this is because Android Studio signs the app with the Debug Certificate in both cases.
Does anyone know how to distinguish these two cases at runtime?

typically one would check for BuildConfig.DEBUG (or a boolean variable holding it) and then log:
if(BuildConfig.DEBUG) {Log.d("SomeActivity", "debug message");}
see the documentation... most relevant for debugging is build-config debuggable true/false.
the run button does not start the debugger; no matter the build-config (it just skips all breakpoints).
in multi-module projects, one should check with:
(getContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0
to tell them apart, two build types need to be configured:
android {
...
buildTypes {
debug {
...
renderscriptDebuggable true
jniDebuggable true
debuggable true
}
release {
...
renderscriptDebuggable false
jniDebuggable false
debuggable false
}
}
}
and to precisely answer the question, there even is one method called isDebuggerConnected(), which would always return false when hitting the run button (no matter the build-config).

"Run app"
This will install the application into your device/emulator
"Debug app"
This will enable you to debug function by placing the break points into your code.
which means you will be able to stop execution of code at that point and debug it line by line. To know more about debugging in android see this
Both the application are debug applications.
To know more about build type please read this

Related

Espresso-Web testing not working for release app

I am testing an app with UIAutomation tool provided by Android (https://developer.android.com/training/testing/other-components/ui-automator). It is working fine but not for some webviews . Then I started exploring the espresso-web (https://developer.android.com/training/testing/espresso/web ) library with the automator library and it is working for webviews in debug app but when I tried this in release app then it is not working and giving error message
android.support.test.espresso.NoActivityResumedException: No activities found ...
I tried finding out this error and most of the blogs says to use Activity Scenario OR Activity rule to initialise the app but My app is initialised through adb shell am start and for debug I am not getting this message and its working fine.
Please check two types of generate APK with release and build when build types debug is true then working in testing UI automation
buildTypes{
release{
debuggable false
}
debug
{
debuggable true
}
}

"No debuggable process" is shown in Android Studio Profiler

When I try to use profiler in Android Studio, I am able to see my connected device but I am getting "No debuggable process" in the Profiler. Can someone please help me why i am getting "No debuggable process".
You can try any/all of the following:
Invalidating cache and restarting your Android Studio: To do this, from the menu bar select File > Invalidate Caches / Restart
Restart your phone
Disable and enable USB debugging
Restart your computer
You can also check if your app is marked as debuggable in build.gradle. To do this, just go to your app-level build.gradle file, and under buildTypes check if the debuggable property is set to false, if yes change it to true. Something like:
buildTypes{
release {
debuggable false
}
debug {
debuggable false
}
}
Alternatively, to do the same you can also do it the old way by adding the following line to your Manifest file under the Application tag:
android:debuggable="true"
This should fix it.

Why is BuildConfig.DEBUG still true if app isn't launched using the debug button?

I am trying to launch my application in debug mode by using the little debug icon on the right of the run icon. i assumed this is the way to start debug mode.
The problem is that i am checking BuildConfig.DEBUG to see if i am in debug mode but it's always true and when i want to check in generated build.config file i found : public static final boolean DEBUG = Boolean.parseBoolean("true");
My question is : am i doing something wrong in launching debug? is it not the way we do it? how can i use debug mode?
You're confusing debugging with a debug build.
BuildConfig.DEBUG is an indication of whether or not your app is a debug build or a release build, it doesn't have anything to do with launching the app to debug. Even launching the app by just running it will also have BuildConfig.DEBUG as true, because it's still a debug build, this will only change once you actually create a signed release.
The icon you're referring to attaches the android debugger to the process, allowing you to use breakpoints, but it generates the same output as it would by simply running the app as well.
BuildConfig.DEBUG will only be false once you create a signed release build, so it has nothing to do with launching the app to debug
Thanks to Adam Burley's comment that lead me to figure out why my build always had BuildConfig.DEBUG = true, even when doing a signed release build.
If you have either of the following set for a buildType in your build.gradle:
testCoverageEnabled true
debuggable true
Then your DEBUG value will be true for that buildType, no matter if it's a signed release build or not.

Enable LogCat on Release Build in Android Studio

By default, when I change Build Variants to release I don't get any Logs on the logcat, but I do need to read release logs of my app, how can I enable this?
Add android:debuggable="true" (default is false) to your Manifest inside the <application> tag.
From the docs:
android:debuggable
Whether or not the application can be debugged,
even when running on a device in user mode — "true" if it can be, and
"false" if not.
respectively
You can disable debugging by removing the android:debuggable attribute
from the tag in your manifest file, or by setting the
android:debuggable attribute to false in your manifest file.
Edit
You may need to add the following to your build.gradle file inside the android{...} tag:
lintOptions {
checkReleaseBuilds false
}
And as a side-note: Right on the device the Logs are always written, no matter if your application's debuggable is set to false or true. But via the LogCat in Android Studio it's only possible if debuggable is set to true. (Just tested this)
You should add
android {
buildTypes {
release {
debuggable true
In this case you can use Log. or System.out.println and see logs.
If you cannot run release version (app is disabled), and error is shown: "apk is not signed. Please configure the signing information for the selected flavor using the Project Structure dialog", see app-release-unsigned.apk is not signed.
I do not like the other solution because then you are not testing how the App really is deployed.
A better solution is to open the Android Device Monitor where you can see the logs even when in release configuration with debuggable=false.
Find it here:
Tools -> Android -> Android Device Monitor
Update:
Android Device Monitor was removed in Android Studio 3.2. However, it is still present in SDK, and you can use it to see the logs (it is located in $ANDROID_SDK/tools/)
debuggable true in build.gradle works well, except that BuildConfig.DEBUG is also going to be true. This might be a problem if your app relies on BuildConfig.DEBUG to do something only when it's a debug build.
In such a case, try Log.wtf(BuildConfig.APPLICATION_ID, "something went wrong"), which will print to logcat even if it's a release build.
This approach will obviously help you to get logs while testing the production build. But be careful while uploading your app to Google Play Store, Toggle debuggable to false before uploading to production.
buildTypes {
debug {
manifestPlaceholders = [crashlyticsCollectionEnabled: "false"]
}
release {
manifestPlaceholders = [crashlyticsCollectionEnabled: "false"]
lintOptions {
checkReleaseBuilds false
abortOnError false
}
shrinkResources true
minifyEnabled true
debuggable true
signingConfig signingConfigs.productionrelease
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
set crashlyticsCollectionEnabled to false to avoid your crashes to report to Google Play-Store while debugging.

Android studio omits breakpoints

When I'm trying to debug application using android studio, I set some breakpoints in the IDE and after starting the debugger I've got an info on every single one breakpoint (in the baloon):
Warning : No executable code found at line ...
It looks like the message appears when the application reaches first BP.
Just to be clear - I have executable code in those lines like String s = "asd";
In my case a Build - Clean Project did help.
set the minifyEnabled to false:
From the project section select the project
Right click on project and click open module settings
select the module you are running and from Build Type set the Minify Enabled to false
Try to insert the next snippet code into the android{} block on the app build.gradle file:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false //<---- THIS FIX THE PROBLEM
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'pro
guard-rules.pro'
}
}
Do you debug on device or on emulator? If device then try to switch back to Dalvik from ART
The first line breakpoint works only
Responding to user3167086's post -
I had the same problem with the breakpoints not working in the middle of a method. One line of code was fine, and the break point icon had a "check mark" in it, but the next point had an "x" in the icon and gave the warring of "no executable code". I checked the Project Structure and the Build Type had already defaulted to "false", but I set it to false again and clicked OK.
For those using Android Studio 1.5 like I am, the complete procedure - using the main menu - is to
select File -> Project Structure.
Then select your "App" module on the left, and then the "Build Types" tab across the top.
Make sure you have "Debug" selected and not "release" on the left (you should see this at the top of the right hand column too) and then set Minify Enabled to FALSE.
Make sure that you use a "Debug" build variant - otherwise breakpoints don't work.
I saw this error message in a pop-up over the dreaded breakpoint with an X in it, in Android Studio's "stable" version 2.1.2 (Gradle: 2.10, Android Plugin: 2.1.2), and the fix was to simply hit the red 'stop' button on the current run session in Android Studio.
I have no idea how the current run session could interfere with setting a break point in source (I have everything under 'Instant Run' unchecked), but this worked for some reason.
For the future:
In my case ALL lines of code were unavailable for debugger. Solution for my problem was disabling jack to avoid creation of intermediate code.
These lines in my gradle.build were to blame:
defaultConfig {
jackOptions {
enabled true
}
}
I turned jack options on several months earlier and then switched back to Java7 forgetting about how my application works. No suprisde Android Studio couldn't find matching code.
I hope it will help.

Categories

Resources