I am having trouble building apk in release mode. No matter what I do I always get BuildConfig.DEBUG flag set to true.
I tried the following
set android:debuggable="false" in manifest file.
Used Export option(by right clicking on project in eclipse) to create signed apk.
Use Android Tools->Export unsigned apk to create apk.
But when I decompiled the code using dex2jar and JD I saw BuildConfig.DEBUG set to true.
Also, when I used the following code in app to check debug flag, I always get result 'true'(in both debug and release mode)
Toast t=Toast.makeText(this, String.valueOf(BuildConfig.DEBUG), Toast.LENGTH_LONG);
t.show();
Please tell me the correct method to build the apk in release mode and to protect code from decompiling.
Please help me.
It has been a bug with ADT, which I am still able to reproduce
https://code.google.com/p/android/issues/detail?id=27940
As a workaround you may use your own boolean to toggle (true/false) logging as this:
public final class AppConfig {
public static final boolean DEBUG = false;
}
// and later use it as
if (AppConfig.DEBUG) {
Log.d(TAG, "lorem ipsum..");
}
Related
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.
I want to perform a code operation only under the assumption that the current build is a signed APK. Is there a way to programmatically find out?
You can check to see if you're running a debug build with BuildConfig.DEBUG. If this shows up as false, you're on a release build. For example:
if (BuildConfig.DEBUG) {
Log.d(TAG, "This is a debug build");
} else {
Log.d(TAG, "This is a release build");
}
PackageInfo.signatures contains the array of all signatures read from the package file. This can be obtained using PackageManager.getInstalledPackages() with GET_SIGNATURES flag.
I've created an application in android in release mode.
I want to remove all the logs in my project when i'm publishing my app in release mode.
how can i do it without duplicating my code?
can i remove the log using Gradle ?
You can make use of BuildConfig.DEBUG variable, which will be true for debug builds and false otherwise.
You could have a Constants file with a variable like
public static final LOG_ENABLED = BuildConfig.DEBUG;
then check the variable before you print a log.
if(Constants.LOG_ENABLED){
// print Log
}
I've signed an app with a sign which I've created. I've installed this apk in my phone and this is ok, but when I've tried to open the app crashes (doesn't show any activity), and it doesn't give me the posibility to watch the log (this dialog doesn't have the report button)
The problem is the sign which I've signed the app. I've tested with other sign and the app opens perfectly. I can't post any information about the creation of the sign, but only I can say I've used:
Letters and _ character in alias
Letters, numbers and - character in password
50 in years
and Letters in name
others field are blank.
The main problem is that the app is published in the google play, so How I can publish a new signed apk without to unpublish this app and create a new app?
Cleaning the project solved the problem in my case.
For anyone getting this in a react-native environment, remember to bundle your js before generating the signed APK.
Easy to forget if you're using something like Android Studios GUI 🙃
Delete "gen" folder from the Project then Cleaning the project
In my case, I had to change the minifyEnabled to false in build.gradle
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
You can see this publish APK and its details like version code and version name and etc. in you developer account on google play
There is one button "Upload new APK"
Click on "Upload new APK" Button
Upload new sign APK with bug fixed
deactive current APK
Active new apk
Note : Don't change package name for new APK
Another possible cause (as was the case for me) is using the wrong JDK/JRE version when building your signed APK.
If you're using Eclipse, check Window -> Preferences -> Java -> Installed JREs and see which one is checked as the default. Android is not yet compatible with Java 8.
I had the same problem today and for me it turned out the reason was:
I used void blaMethod() in my Java code which I tried to call by onClick methods of XML Buttons. I had to learn that these methods need to be public void ... - I usually do that, but this time I got a little sloppy.
The strange thing is: my unsigned APK worked perfectly fine on all my devices, but the signed one just crashed when using those methods (could not find method (view) in a parent or ancestor context). Don't get why that is...
May this help someone!
Greets
You can simply create a new keystore if you are in testing
Or simply in my case retype the password
In the newest version of ADT (r17) a generated constant was added BuildConfig.DEBUG that is set according to the build type. The problem I have is that it is never set to false, I expected it to change when doing "Android Tools -> Export Signed Application Package" but it hasn't for me.
So how do I change the build type?
Added a feature that allows you to run some code only in debug mode.
Builds now generate a class called BuildConfig containing a DEBUG
constant that is automatically set according to your build type. You
can check the (BuildConfig.DEBUG) constant in your code to run
debug-only functions
Currently you can get the correct behavior by disabling "Build Automatically", cleaning the project and then export via "Android Tools -> Export Signed Application Package". When you run the application BuildConfig.DEBUG should be false.
With Eclipse, I always disable "Build Automatically" option before Exporting the app in release. Then I clean the project and export. Otherwise it starts compiling in debug mode, and then the value of BuildConfig.DEBUG may be wrong.
With Android Studio, I simply add my own custom variable in the build.gradle:
buildTypes {
debug {
buildConfigField "Boolean", "DEBUG_MODE", "true"
}
release {
buildConfigField "Boolean", "DEBUG_MODE", "false"
}
}
When I build the project, the BuildConfig.java is generated as follows:
public final class BuildConfig {
// Fields from build type: debug
public static final Boolean DEBUG_MODE = true;
}
Then in my code I can use:
if (BuildConfig.DEBUG_MODE) {
// do something
}
I recommand to clean after switching debug/release build.
It doesn't work properly:
Issue 27940: BuildConfig.DEBUG is "true" for exported application package
It's disappointing that they sometimes release buggy features.
Check for imports, sometimes BuildConfig is imported from any class of library unintentionally. For example:
import io.fabric.sdk.android.BuildConfig;
In this case BuildConfig.DEBUG will always return false;
import com.yourpackagename.BuildConfig;
In this case BuildConfig.DEBUG will return your real build variant.
p.s I just copy this one from my answer here:BuildConfig.DEBUG always false when building library projects with gradle
It does work, but note that the code file never changes, even when exporting the signed file. The export process changes the value of this variable to false, which might give you the false impression that it is not working.
I tested this with logging statements like
if (com.mypackage.BuildConfig.DEBUG)
Log.d(TAG, location.getProvider() + " location changed");
When testing, my Log statements no longer produce any output.
From Preparing for Release:
Turn off logging and debugging
Make sure you deactivate logging and disable the debugging option
before you build your application for release. You can deactivate
logging by removing calls to Log methods in your source files. 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. Also,
remove any log files or static test files that were created in your
project.
Also, you should remove all Debug tracing calls that you added to your
code, such as startMethodTracing() and stopMethodTracing() method
calls.
More information is following the link.
The solution for me:
Project -> Build Automatically
Project -> Clean
Project -> Build
Project Export Android application
It's work in r20
I would want to propose a simple workaround if you use proguard during APK export.
Proguard provides a way to remove calls to specific functions in release mode. Any calls for debugging logs can be removed with following setting in proguard-project.txt.
# Remove debug logs
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
}
And optimization setting in project.properties.
proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt
With this, you don't need to concern any unnecessary String computation passing to debug log to which #Jeremyfa pointed. The computations are just removed in release build.
So the workaround for BuildConfig.DEBUG uses the same feature of proguard like following.
public class DebugConfig {
private static boolean debug = false;
static {
setDebug(); // This line will be removed by proguard in release.
}
private static void setDebug() {
debug = true;
}
public static boolean isDebug() {
return debug;
}
}
And following setting in proguard-project.txt.
-assumenosideeffects class com.neofect.rapael.client.DebugConfig {
private static *** setDebug();
}
I would prefer using this to disabling the Build Automatically option, because this doesn't depend on the builder's individual IDE setting but is maintained as committed file which are shared among developers.
Does not work properly as far as I understood (Android issue 22241)
I had some trouble on a project (working with Eclipse), that constant was not set to true when exporting a signed APK of my project :(
Would love to hear it works though
a good way is creating your own class :
public class Log {
public static void d(String message) {
if (BuildConfig.DEBUG)
android.util.Log.d(
"[" + (new Exception().getStackTrace()[1].getClassName()) + "]",
"{" + (new Exception().getStackTrace()[1].getMethodName()) + "} "
+ message
);
}
}
will you check your app level build.gradle debuggable true for release build
buildTypes {
release {
debuggable true
}
}
instead you keep false or comment that line
buildTypes {
release {
//debuggable true
}
}
now you will get BuildConfig.DEBUG false for release build
I've seen some strange behavior that has to do with when the values in BuildConfig are set to their final values. This may have something to do with your issue.
The simple explanation is that default values are set initially before Proguard is run, then after Proguard runs, the BuildConfig file is regenerated with the proper values. However, Proguard has already optimized your code by this point and you have issues.
Here is a bug I created against Gradle. https://code.google.com/p/android/issues/detail?id=182449