可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is there any simple way to turn Crashlytics Android SDK off while developing ?
I don't want it to send a crash every time I do something stupid
On the other hand I don't want to comment out Crashlytics.start()
and possibly risk forgetting to uncomment it and commit
回答1:
Marc from Crashlytics here. Here's a couple of ways to disable Crashlytics while you are doing your debug builds!
Use a different android:versionString for debug and release builds and then disable crash reporting from the Crashlytics web dashboard for the debug version.
Wrap the call to Crashlytics.start() in an if statement that checks a debug flag. You could use either a custom flag or an approach like the ones proposed here: How to check if APK is signed or "debug build"?
回答2:
I found the solution from Crashlytics (with Fabric integration)
Put following code inside your Application class onCreate()
Crashlytics crashlytics = new Crashlytics.Builder().disabled(BuildConfig.DEBUG).build(); Fabric.with(this, crashlytics);
EDIT:
In Crashalitics 2.3 and above, this is deprecated. The correct code is:
CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build(); Fabric.with(this, new Crashlytics.Builder().core(core).build());
or
Fabric.with(this, new Crashlytics.Builder().core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build());
(copied from Crashlytics deprecated method disabled())
EDIT2:
You can also optionally add this to your buildType
in gradle. This command disables sending the crashlytics mapping file and generating an ID for each build, which speeds up gradle builds of those flavors. (It doesn't disable Crashlytics at run time.) See Mike B's answer here.
buildTypes { release { .... } debug { ext.enableCrashlytics = false } }
回答3:
If you use Gradle just add this to a flavor:
ext.enableCrashlytics = false
回答4:
Check out the latest doc. https://docs.fabric.io/android/crashlytics/build-tools.html#gradle-advanced-setup.
Apart from adding ext.enableCrashlytics = false
in build.grade you need to do,
Crashlytics crashlyticsKit = new Crashlytics.Builder() .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()) .build(); // Initialize Fabric with the debug-disabled crashlytics. Fabric.with(this, crashlyticsKit);
回答5:
I found this to be the the easiest solution:
release { ... buildConfigField 'Boolean', 'enableCrashlytics', 'true' } debug { buildConfigField 'Boolean', 'enableCrashlytics', 'false' }
The above lines will create a static boolean field called enableCrashlytics
in the BuildConfig
file which you can use to decide whether to initiate Fabric
or not:
if (BuildConfig.enableCrashlytics) Fabric.with(this, new Crashlytics());
NOTE: With this method Fabrics is initialised only in release builds (as indicative in the above code). This means you need to put calls to statics methods in the Crashlytics
class in an if
block which checks whether Fabrics has been initialised as shown below.
if (Fabric.isInitialized()) Crashlytics.logException(e);
Otherwise the app will crash with Must Initialize Fabric before using singleton()
error when testing on the emulator.
回答6:
Use this in MyApplication#onCreate()
if (!BuildConfig.DEBUG) Crashlytics.start(this);
EDIT If you upgraded to Fabric, use this answer instead.
回答7:
There are plenty of good answers here, but for my testing I use debug builds for in-house betas and out-of-the-lab testing where crash logs are still very useful and I would still like to report them. Like the OP, all I wanted was to disable them during active development where I am often causing and quickly resolving crashes.
Rather than remove ALL debug crashes you can choose to only disable the reports while a device is connected to your development machine with the following code.
if (!Debug.isDebuggerConnected()) { Fabric.with(this, new Crashlytics()); }
回答8:
If you want to capture all crashes (for debug and release builds) but want to separate them in the Crashlytics Dashboard, you can add this line of code to build.gradle:
debug { versionNameSuffix "-DEBUG" }
For example, if your app's versionName is 1.0.0, your release builds will be tagged as 1.0.0 while debug builds will be 1.0.0-DEBUG
回答9:
Notice that you can also disable the annoying uploading of symbols in debug build:
def crashlyticsUploadStoredDeobsDebug = "crashlyticsUploadStoredDeobsDebug" def crashlyticsUploadDeobsDebug = "crashlyticsUploadDeobsDebug" tasks.whenTaskAdded { task -> if (crashlyticsUploadStoredDeobsDebug.equals(task.name) || crashlyticsUploadDeobsDebug.equals(task.name)) { println "Disabling $task.name." task.enabled = false } }
Just put it into the build.gradle
of your application module.
回答10:
Up to date easiest version when using Gradle to build:
if (!BuildConfig.DEBUG) { Fabric.with(this, new Crashlytics()); }
It uses the new Built-In Syntax from Fabric for Crashlytics and works automatically with the a Gradle build.
回答11:
The problem is that none of the solutions does work for the latest crashlytics sdk. (I'm using 2.9.0)
You can't disable it by code since it compiles into your project and runs before even a call onCreate of your application. So other solution is simple - don't compile crashlytics when not needed. Replace the 'compile' call with 'releaseCompile' within build.gradle file.
releaseCompile('com.crashlytics.sdk.android:crashlytics:2.9.0@aar') { transitive = true }
回答12:
If you're worried about BuildConfig.DEBUG
not being set up correctly, use ApplicationInfo
instead:
boolean isDebug = ( mAppContext.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) != 0; Crashlytics crashlytics = new Crashlytics.Builder().disabled( isDebug ).build(); Fabric.with( uIContext, crashlytics );
回答13:
Use flavors or build configs. Use a separate build identifier for dev build and all your crashes will keep going to a separate app. Can come handy in case of sharing the build with peers or using it without a debugger. Something like this -
productFlavors { dev { applicationId "io.yourapp.developement" } staging { applicationId "io.yourapp.staging" } production { applicationId "io.yourapp.app" }
回答14:
A strange problem that I encountered: I followed xialin's answer (which also appears on the official website) and it didn't work. Turned out that I was referencing BuildConfig
in Fabric's package which also contains a static DEBUG variable that was set to false even in debug mode.
So, if you follow the aforementioned solution and you still get debug reports, make sure that you're referencing this:
import com.yourpackagename.BuildConfig;
And not this:
import io.fabric.sdk.android.BuildConfig;
回答15:
Another way if you only want to do it on your IDE is to logout of the plugin. Apparently it will stop sending reports while you're generating builds without logging in again.
回答16:
If you want a debuggable release build, here's the way:
buildTypes { release { signingConfig signingConfigs.config debuggable true //-> debuggable release build minifyEnabled true multiDexEnabled false ext.enableCrashlytics = true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField 'boolean', 'BUILD_TYPE_DEBUG', 'false' } debug { minifyEnabled false multiDexEnabled true ext.enableCrashlytics = false ext.alwaysUpdateBuildId = false // Disable fabric build ID generation for debug builds proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField 'boolean', 'BUILD_TYPE_DEBUG', 'true' } }
When you set debuggable true
your BuildConfig.DEBUG will initialized with true, that's why I added that variable in BuildConfig class.
Init Fabric:
Crashlytics crashlytics = new Crashlytics.Builder() // disable crash reporting in debug build types with custom build type variable .core(new CrashlyticsCore.Builder().disabled(BuildConfig.BUILD_TYPE_DEBUG).build()) .build(); final Fabric fabric = new Fabric.Builder(this) .kits(crashlytics) //enable debugging with debuggable flag in build type .debuggable(BuildConfig.DEBUG) .build(); // Initialize Fabric with the debug-disabled crashlytics. Fabric.with(fabric);
回答17:
We can use fabric's isDebuggable() method.
import static io.fabric.sdk.android.Fabric.isDebuggable; if(! isDebuggable()){ // set Crashlytics ... }
Happy coding :)
回答18:
The chosen answer is not correct any more. Google changed the integration of Crashlytics. My current version is 2.9.1
and the only thing I had to do is, to add implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1'
to my Gradle file. No further things required, nice but this means that Crashlytics is always running.
Solution 1
Only compile Crashlytics in release version (maybe update version):
dependencies { ... releaseImplementation 'com.crashlytics.sdk.android:crashlytics:2.9.1' }
Solution 2
If you want to additionally configure Crashlytics then Solution 1 is not working, since the Crashlytics classes will not be found. So change the Gradle implementation back to (maybe update version):
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1'
Then go to your Manifest and add the following meta-data
tag inside the application
tag:
<application android:name="...> <meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" /> ... </application>
Add to your Launch-Activity (only one-time required, not every Activity)
if (!BuildConfig.DEBUG) { // only enable bug tracking in release version Fabric.with(this, new Crashlytics()); }
This will only enable Crashlytics in release versions. Be careful, also check for BuildConfig.DEBUG when you then configure Crashlytics, E.g:
if (!BuildConfig.DEBUG) { Crashlytics.setUserIdentifier("HASH_ID"); }
Best, Paul
回答19:
This is silly answer, I know
Just comment out Fabric.with(this, new Crashlytics());
, work on that and uncomment it when you want to release it.