Is BuildConfig.DEBUG still bugged? - android

According to this Blog BuildConfig.DEBUG was unreliable.
Since my colleague is using BuildConfig.DEBUG extensively (seemingly like test code in production code), I'm wondering if this flag is still as bugged as it was a few years ago.

I can confirm this bug still exists, tested with Android Studio 1.2 Build AI-140.1782451 and Gradle 1.1 compiling against Android API Level 21.
The issue is visible with a Nexus 10 on Android 5.0.2 or a similar device.
If you open BuildConfig.DEBUG in the source editor it says:
public static final boolean DEBUG = Boolean.parseBoolean("true");
But if you debug the app under question, DEBUG stays on false.
This hinders my Retrofit-debugging, as I wanted to enable it conditionally depending on the build-type.

It seems the issue to which you are referring is specific to ADT + Eclipse. So I believe that if you're using Gradle and Android Studio, this should not be an issue.
Crucially: this only occurs if you're using the Build Automatically option, and you don't clean your project. Because of this, I would hardly consider this a bug. After all, who says what should and shouldn't be rebuilt whenever you make a code change and have Build Automatically enabled?
As a matter of good practice, you should always clean and rebuild your project prior to an actual release, in which case this is a non-issue.
So yes, this is still a problem if you're using this setting, and not rebuilding your project prior to release, and you're still using ADT and Eclipse (which seems to be destined for deprecation).
Here's the discussion of the bug: https://code.google.com/p/android/issues/detail?id=27940

I had always problems with the predefined variable, so I created my own:
buildTypes {
// If we use the same keystore for debug and release, we don't have to wipe all preferences
debug {
//noinspection GroovyAssignabilityCheck
signingConfig signingConfigs.releaseConfig
zipAlignEnabled true
resValue "bool", "DEBUG", "true"
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//noinspection GroovyAssignabilityCheck
signingConfig signingConfigs.releaseConfig
zipAlignEnabled true
resValue "bool", "DEBUG", "false"
}
}
I your code you can read this variable:
if (!MyApplication.get().getResources().getBoolean(R.bool.DEBUG)) {
// Firebase Crash reporting
FirebaseCrash.report(e);
}

Related

Android Profiler: No debuggable processes

It can find my device, but without any debuggable process. It's not the first time this problem occurred. But it still worked yesterday, I used profiler to observe my app's memory info. And when I try to debug my app, the process is debuggable. I tried all solutions just like restart Android Studio, none worked. Does anyone know why?
Works for me:
In build.gradle(:app) -> find "buildTypes" -> to "debug" section -> set it to "true" -> done
buildTypes {
release {
debuggable false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug {
debuggable true
}
}
Mabe you can Close the AndroidStudio.And then restart the adb service.try again.
Keep your application installed is a Debug mode.
if is done,it will be work. I tried.
Since running apps on an emulator is slow, sometimes you may have more than one instance of your app on the emulator which will cause a problem. Just delete the existing emulator and create a new one, or you can instead just wipe the data on an existing emulator:

How do I get optimisation in Android without obfuscation using R8

I have some big chunks of code which test the behaviour of some incompletely documented Android APis (sigh) which seem to behave differently for different Android versions and I want to switch them in and out of debug builds (they are always removed from release builds) which I try to do using buildConfigFields in build.gradle like this
buildTypes {
release {
minifyEnabled true
buildConfigField("boolean", "RINGERMODETEST", "false")
...
}
debug {
// set this to false to remove the optional code
buildConfigField("boolean", "RINGERMODETEST", "true")
...
}
}
Then in my code I have
if (BuildConfig.RINGERMODETEST) {
// optional code
}
To get the optional code removed I need to turn on the optimiser, but setting minifyEnabled would turn on obfuscation as well which I don't want in a debug build. There is an earlier answer to this question for Proguard at Proguard shrinking and optimizing without obfuscation, but there doesn't seem to be any documentation which says how to do it for R8. I really don't want to have to learn how to construct a complete proguard control file when it's supposed to be superseded by R8. and useProguard will apparently soon be deprecated.

"Unexpected attempt to get register for a value without a register" during release builds but not debug

Debug builds work fine for me. When I choose Active Build Variant = release, and try to run Build -> Generate Bundle(s) / APK(s) -> Build APK, the build runs for a while, then I get the following error:
Unexpected attempt to get register for a value without a register in method java.util.List com.chrynan.chords.parser.AsciiChordParser.parseLineAsString(java.lang.String, int, java.util.Set).
That is referencing an external library I'm pulling in. Source code for that function is available here.
What does that error mean? My searches returned nothing remotely like it.
I solved it! I have no idea why, but I set minifyEnabled = true in my build.gradle (:app):
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
This gave me instant crashing on app start due to this problem, although I think that's unrelated. I fixed that, and now my build works.
I still have no idea why that error came up though.
For some one else having the same problem, I solved it by changing
getDefaultProguardFile( 'proguard-android-optimize.txt') to
getDefaultProguardFile( 'proguard-android.txt') in build.gradle

Android Release build can be attached

I recently find my Android Release version can be attached through Android Studio and all logs are available to be seen as well, even though I'm sure that AndroidManifest.xml file doesn't contain "android:debuggable=true" and app's build.gradle file specified that
buildTypes {
...
release {
...
debuggable false
...
}
...
}
Do you guys have any good idea to avoid this?
You can only keep like this code below:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
hopefully works it.
Logs are nothing to do with debuggable true.
The option debuggale is to making your application debuggable in release mode, so you can attach debugger even on your release build by default it is false.
If you are using Log class and printing any log it will always display until and unless you put the check before logging them.
What you can do is put a check before every log is Build.Debug == true then print the log.
Or you can use open source library for this work like this one which provide the control of logging based on your configuration.
Or you can find a more helpful answer here.

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.

Categories

Resources