Android Studio cannot find local variable in debugger - android

When I try to debug my app, I can't see variables values in the debugger.
I have the following error when I try to evaluate an expression for instance (same with variables watchers) :
Cannot find local variable 'data' with type com.myorg.myapp.data.objects.DataToUpdate
The IDE seems to understand the type of my variable but it can't find it.
The variable is used just after so it has not been optimized away.
I belive that the code has been optimized but only part of it.
I built an apk and decompiled it with some decompilers online to see if it was minifyed or something.
Part of my code is not
My class fields are ok, I can see their values in the debugger and their name is the same as in my code
However, other parts of my code seem to be optimized (variables names are not the same)
E.g. : my variable "data" of type DataToUpdate becomes "DataToUpdate r118" in the decompiled code
I had nothing put in the debug buildType of my build.gradle. I added the following lines according to what I saw on the web to try to make it work :
buildTypes {
debug {
debuggable true
testCoverageEnabled = false
minifyEnabled false
useProguard false
}
}
I am using Android Studio 4.0 Beta 4 but it also didn't work reliably in Android Studio 3.6.

I ended up using the logcat to understand and then correct the bugs in my code.
The debugger worked in other methods so it may be usefull to try to reproduce the problem in other methods, to debug it there.
A debugger working as expected would be better, but I couldn't make it work in this particular method.

Related

java- Android Studio [ClassNotFoundException]

I got problem when change name packages in android studio,
first i use some open source project, then when i want to change the previous packages some of function cant be used, the error message is "ClassNotFoundException:yuku.kpri.model.Song" even thoughni already change packages "yuku" to "buna" and all packages that related to "yuku" change to "buna" and also packages "yuku.kpri.model.Song" changes to "buna.kpri.model.Song" but the code still run to yuku.kpri.model.Song,
I already try many solution like
Close/Unselect Compact Empty Middle Packages ,Then right click your package and rename it. Here
Clean Project
Rebuild Project
change the applicationId Here
Error Message when try to use the function
Here
Help me to solve this problem, what i miss and my mistake...
im looking for your answer
One thing you could try is selecting File --> Invalidate Caches/Restart, sometimes it works magic.
This could be a Proguard issue.
Try to set "minifyEnabled false" for both Release & Debug build types in your projects build.gradle file :
buildTypes {
debug {
minifyEnabled false
}
release {
minifyEnabled false
}
}
If this works, you should exclude the class in the proguard-ruls.pro file.

Android bug: how to stay on Android error page about "method not mocked"?

When "not mocked" error occurs, error message referencign the following page:
http://g.co/androidstudio/not-mocked
But this page has redirect to somewhere else so I have no time to read about an error. On the redirected page, although, an error is not covered.
It can help you: Unit testing on Android Studio: "not mocked" error
Text from your link(sorry, i can't set this post as a comment) :
Note that when running tests from Gradle, we will execute tests for
every variant of your code (see here). This means that tests will end
up executing at least twice (once with the release build of your code,
once with the debug build of production code).
"Method ... not mocked."
The android.jar file that is used to run unit tests does not contain
any actual code - that is provided by the Android system image on real
devices. Instead, all methods throw exceptions (by default). This is
to make sure your unit tests only test your code and do not depend on
any particular behaviour of the Android platform (that you have not
explicitly mocked e.g. using Mockito). If that proves problematic, you
can add the snippet below to your build.gradle to change this
behavior:
android {
// ...
testOptions {
unitTests.returnDefaultValues = true
}
}
We are aware that the default behavior is problematic when using
classes like Log or TextUtils and will evaluate possible solutions in
future releases.

Add build machine signature to gradle generated variable

I'd like to stamp some variable generated from gradle (in my case it's User Agent used later with HTTP requests) to later be able to distinguish which developer build the app (for example if some developer made a mistake and his app is DDoSing the server).
So for now I can distinguish release from debug with:
buildTypes {
debug {
buildConfigField "String", "USER_AGENT", "\"Android-debug\""
}
release {
buildConfigField "String", "USER_AGENT", "\"Android-release\""
}
}
But for the debug I'd like to add something to know who built the app instance, it may be git login, machine name, or something else.
A gradle build file is actually Groovy code, and you're free to put whatever you want in it. You just have to make sure that the code runs before it would be used in the DSL that describes the build. So if you want to grab something from the system, just write the Groovy code to do that. Groovy is a lot like Java, and you have the full JDK to work with at runtime, so it should be easy to get started.
If you want to access things about the build machine and environment, you might have to shell out to different commands in order to gather that data. Populate some variables with that data. Then use buildConfigField as you already are to drop those values into BuildConfig.java.
Bear in mind that you might want to provide some value in both debug and release so they both generate the same BuildConfig symbols. Otherwise your app might not compile in one config or the other.
BTW. You can tell the difference between debug and release with properties that are already added to BuildConfig, so you don't need to add anything more to tell the difference. Lines like these will always appear (look in the generated BuildConfig.java to see for yourself):
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String BUILD_TYPE = "debug";

PackageNameSuffix breaks test - Robolectric

When I add a packageNameSuffix to my build.gradle debug build type (see https://plus.google.com/108967384991768947849/posts/XGXH3arvbrK), all of my Robolectric tests are failing due to the following:
Caused by: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f050000
at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:354)
at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:387)
at android.content.res.Resources.openRawResource(Resources.java)
at com.myapp.utilities.CSVUtility.parseRawResourceCSV(CSVUtility.java:38)
The problem seems to be that the raw folder src/main/res/main no longer being found. This folder contains a csv which is parsed at application start... which is where the tests go boom.
Architecture/data restructuring suggestions aside (I know CSV may not be the best way to get this data loaded at app start), does anyone know how I might remedy this problem?
Edit: I tried moving the file to the assets folder instead, and then my tests failed on a Context.getString() call. Resources look to be getting completely hosed when adding packageNameSuffixes.
Edit: tmurakami posted on the github issue - https://github.com/robolectric/robolectric/issues/1001#issuecomment-42740897
I've copied the full response:
Try using this gradle snippet.
This works fine in my environment.
def hasLibraryVariants = android.hasProperty('libraryVariants')
def variants = hasLibraryVariants ? android.libraryVariants : android.applicationVariants
tasks.withType(Test).whenTaskAdded {
it.systemProperty 'android.package', variants.iterator().next().processResources.packageForR
}
The original package name has been stored in the following fields of any variant:
variantData.variantConfiguration.originalPackageName
processResources.packageForR
generateBuildConfig.buildConfigPackageName
However these are internal only, so might become inaccessible in the future.
If you don't want to use these fields, try the following snippet:
tasks.withType(Test).whenTaskAdded {
it.systemProperty 'android.package', android.defaultConfig.packageName
}
To use this, you need to add the main package name in the 'android.defaultConfig' section.
android {
defaultConfig {
packageName 'main.package.name'
}
}
Looks like I need to add an android.package system property for the package name. See this issue conversation on Github - https://github.com/robolectric/robolectric/issues/1001

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