Why does BuildConfig.DEBUG return false, when I run the application?
I use it to control the log like the following:
public static void d(String LOG_TAG, String msg){
if(BuildConfig.DEBUG){
Log.d(LOG_TAG,msg);
}
}
Check imports in the class, make sure you are using correct BuildConfig path. You may use BuildConfig not from your app, but from some library.
In your Android Studio build variant are you on debug variant?
That is applied when you use flavors, either for debug or release.
in the debug mode, BuildConfig.BUILD is true, and in the release mode, it is false.
Ensure the auto import statement of build config on the top of your class belongs to your project.
com.your.package.name.BuildConfig
the BuildConfig import might belong to a released library there DEBUG is false.
If that code is in a library, then it'll always be false, thanks to a 3-year-old bug in gradle.
Do not import BuildConfig. This is an auto-generated class and importing it is unnecessary, despite what Android Studio may tell you.
If Android Studio is prompting you to import BuildConfig it may be because you need to do an initial Gradle build to create the auto-generated class which ends up being created at com.yourdomain.yourapp.BuildConfig. This can happen when you upgrade Android Studio and Gradle, or when you run Build -> Clean project.
If you import another package's BuildConfig, then of course it'll always be false because they are only releasing their release flavours and not their debug flavours.
Regarding the other answers recommending modifying your build.gradle, I found that specifying buildType conflicted with the default behaviour of Android Studio and its generation of BuildConfig, stating I had a duplicate entry.
So essentially:
Do not import any package's BuildConfig (so let it stay red)
Do not add buildType to your build.gradle (this may conflict with the default build behaviour of auto-generating the class)
Ignore the lint error
Run build
The error should go away.
I experience this when I upgrade Android Studio and Gradle and when I clean the project.
Ignore import prompts
Do not import another package's BuildConfig—it'll always be false because they are not releasing their debug versions.
Importing will cause the error you're experiencing
In my project, if I import one of the suggested libraries, it'll show the error you're getting, because no one releases a debug build so of course it'll always be false if you're pointing to someone else's.
Ignore the intellisense and run the project
Just run a build. The class will be auto-generated and the warning will go away.
I specified debuggable true in build.config, but this is always false
After this change (simply remove it), all was working properly :
There is a workaround for the problem:
App
dependencies {
releaseCompile project(path: ':library', configuration: 'release')
debugCompile project(path: ':library', configuration: 'debug')
}
Library
android {
publishNonDefault true
}
Perhaps not ideal, but I ended up creating my own
buildTypes {
debug {
buildConfigField "boolean", "IS_DEBUG", "true" // Had issues with BuildConfig.DEBUG, created IS_DEBUG to ensure functionality behaved as expected.
}
release {
signingConfig signingConfigs.release
buildConfigField "boolean", "IS_DEBUG", "false"
}
}
And then address it like BuildConfig.IS_DEBUG programatically.
Maybe you are importing the wrong package, check that. (some Android libraries also have the BuildConfig class)
Related
I would like to make my app debuggable.
When I deploy to my nexus 5x I get this error:
Error running AndroidLauncher: Cannot debug application from module android on device lge-nexus_5x-(code here). This application does not have the debuggable attribute enabled in its manifest. If you have manually set it in the manifest, then remove it and let the IDE automatically assign it. If you are using Gradle, make sure that your current variant is debuggable.
If I add the debuggable="true" attribute to the android tag in my manifest it works.
But I'm forgetful, so I'd rather do it the proper way with gradle.
I've tried to add a lot of the things I've found on the internet to both my Android module build.gradle, and my project's root level build.gradle.
All to no avail.
What should work?
Thanks,
Chase
On an tangent, I also tried running my HTML module using the instructions from their site, and it says it's failing because I'm using java 1.7 features but the gradle source is 1.6. I've also tried googling that to no avail. I was adding some lines like this:
sourceCompatibility 1.7
targetCompatibility 1.7
You can use build types (Build Variants) in your android gradle file:
project/android/build.gradle
buildTypes {
debug {
applicationIdSuffix ".debug"
debuggable true
}
release {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-project.txt'
minifyEnabled true
pseudoLocalesEnabled false
debuggable false
signingConfig signingConfigs.AndroidKey
}
}
* There is some proguard stuff and signing config you might not need. Just remove from the release build variant.
You don't need any other configuration in other files. You can see the full source I used in my project here.
If you are using Android Studio you can change between your build variants here:
When you run the project if release is selected it will install the non debugable apk in your device. Otherwise, if debug is selected it will install the debugable apk. Both can be installed at the same time. It will show the app twice in your device.
The problem is that it should be debugable by default. You could remove the debuggable true line and the debug build variant should still be debugable.
If it does not work paste your manifest and your build.gradle files here so we can see what is going on.
You can look all theses files in my project. It is also a libGDX project, but it don't have the HTML module.
The Build Variant docs might be helpful.
According to documentation setting minifyEnabled to false must disable ProGuard run
integration {
minifyEnabled false
versionNameSuffix "-int"}
But the ProGuard is still started by Gradle! Any ideas why?
You will need to change the Build Variant to use one of the integration build variants (from the bottom left in android studio), if you want to use the configuration for integration buildType.
From what you describe you appear to be using a different buildType. By default it is debug. Make sure an integration Build Variant is selected and you should be good.
As it often happens, I found and answer after posting my question.
"This is not a bug, this is a feature":
https://groups.google.com/forum/m/#!topic/adt-dev/iS_lyRH8hL8
This is not really a problem, but certainly annoying.
The output you are seeing is related to the way the Android gradle plugin determines the set of classes that must be in the main dex file when multidex is enabled. For this purpose it uses ProGuard internally, but it is unrelated to your configuration.
In order to disable the logging output of this task, you can add the following to your build.gradle file:
tasks.whenTaskAdded { task ->
if (task.name.startsWith("transformClassesWithMultidexlistFor")) {
task.logging.level = LogLevel.ERROR
}
}
I am trying to debug over network in Android Studio. I connected via port 5555 and generally it is possible step through break points. But it often takes minutes just to execute one line of code and the other thing is that I don't see any variables which are no members. All I see is the this object, but no variables from within methods. How can I enable it?
As you can see I am within the method and at least the activity object is initialized, but it is not visible in the variables monitor.
UPDATE:
The problem remains when using USB debugging. No local variables are visible, not even when trying to evaluate expressions while debugging:
Android Studio 2.1, Gradle 2.1.0, Java 1.8
Had the same problem.
There is a bug in Android Studio, see https://code.google.com/p/android/issues/detail?id=93730
They recommend removing in build.gradle (app), this fixed the issue for me.
android {
buildTypes {
debug {
...
testCoverageEnabled true
}
}
}
After a while of figuring out this same issue, I realized I was running a release build rather than a debug build.
The build variants window may not be open in Android Studio by default. Go to Tool Windows -> Build Variants. In the Build Variants window, select the appropriate build.
In your app.gradle file, make sure debuggable is set to true in the build variant you would like to debug:
android {
// ...
buildTypes {
release {
// ...
}
debug {
debuggable true
}
}
// ...
}
If you would like to debug your release build, go ahead and add debuggable true to your release build.
Hope this helps!
I tried setting testCoverageEnabled to false but that did not work for me. In my case, I had ProGuard enabled for my debug flavor and disabling it (i.e. setting minifiyEnabled to false) was the only thing that allowed me to be able to see my local variables while debugging again.
In my case, it was because I had forgotten that my build variant was set to release. Toggling the variant back to debug and re-running correctly showed the local variables.
For me I had to set testCoverageEnabled to false like so:
android {
buildTypes {
debug {
...
testCoverageEnabled false
}
}
}
When I had this set to true, I was not getting local variables
While this is not a permanent solution to this problem, my most consistent fix (after trying the other answers here to no avail) has simply been restarting my computer.
I tried some kind of hit n trial and made it work with the settings as seen in the attachment. FYI, using latest version of Android Studio 3.3.1 and gradle version 4.6.
I had same trouble. I completely reinstalled my IDE and the trouble has been disappeared. I hope my approach will help you.
Java 1.8 does not support accessing variable values.
Update Gradle to version 2.2.0-beta3:
In your gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
In your project build.gradle file
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-beta3'
}
I want to compile the debug production apk with minifyEnable true,
But after config it, the test apk has no test method, because all the methods are removed by proguard.
Keeping the method in proguard.flag (with testProguardFile) takes no effect.
How to solve this problem?
Thanks!
ps:
I use gradle 2.2.1, and android gradle plugin 1.1.0
I found some resource about this problem, but without success.
https://code.google.com/p/android/issues/detail?id=159831
Minification can be enabled per build type. Simply omit minifyEnabled for build types where you don't want it (it is disabled by default), and add it to build types where you do:
buildTypes {
release {
minifyEnabled true
...
}
}
See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Types
I recently switched to Android Studio / Gradle and I am wondering, how ProGuard can be configured in the build.gradle script. I am new to Gradle, but I thought, configuring the Proguard task would be a good idea (as documented in the Proguard project documentation.
I want to configure Proguard to save the mapping in different files for different product flavors with the 'printmapping' setting
task myProguardTask(type: proguard.gradle.ProGuardTask) {
printmapping file("test.txt")
}
but it crashes on task-execution with
Gradle: Execution failed for task ':module:proguardFlavorVariant'.
> proguard.ConfigurationParser.<init>(Ljava/io/File;Ljava/util/Properties;)V
In the newer versions of the Gradle 'android'-plugin, Proguard seems to be included and I think this might be the reason, why configuring the Proguard task as stated on the Proguard documentation did not work. But I did not find any documentation on this topic of how to do this with the newer android-gradle-plugin.
Thanks for your help!
Proguard is built into the Android-Gradle plugin and you don't need to configure it as a separate task. The docs are at:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard
Are your flavors so different that you really want different ProGuard configurations for them? I'd think in most cases you could have one config that could cover them all.
EDIT:
If you do want to change ProGuard rules for different flavors, the Android Gradle DSL allows you to do so. The sample in the docs shows how to do it:
android {
buildTypes {
release {
// in later versions of the Gradle plugin runProguard -> minifyEnabled
minifyEnabled true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
productFlavors {
flavor1 {
}
flavor2 {
proguardFile 'some-other-rules.txt'
}
}
}
That should handle your use case, unless you're looking for a way to have it automatically determine the proguardFile value based on the flavor name without you having to set it manually; you could do that through some custom Groovy scripting.