简体   繁体   English

如何使用“minifyEnabled true”调试应用程序并在发布模式下禁用MultiDexApplication

[英]How to debug application with “minifyEnabled true” and disable MultiDexApplication in release mode

When build.gradle has lines: build.gradlebuild.gradle

buildTypes {
    debug{
        minifyEnabled true
    }

we cannot debug an application. 我们无法调试应用程序。 A solution is to write: 一个解决方案是写:

    ...
    multiDexEnabled true
}
buildTypes {
    debug{
        minifyEnabled false
        debuggable true
    }

...
implementation 'com.android.support:multidex:1.0.3'

And in MyApplication file write: 并在MyApplication文件中写道:

public class MyApplication extends MultiDexApplication

But when we turn to release mode the application still inherits from MultiDexApplication . 但是当我们转向发布模式时,应用程序仍然继承自MultiDexApplication So there can be some compatibility problems with old devices. 因此旧设备可能存在一些兼容性问题。 Is there a way, for instance, in Gradle, to avoid MultiDexApplication in release mode and retain it only in debug mode? 例如,有没有办法在Gradle中避免MultiDexApplication处于发布模式并仅在调试模式下保留它? I read https://xrubio.com/2016/10/disabling-removing-code-on-release-builds/ but unsure if it can help. 我阅读了https://xrubio.com/2016/10/disabling-removing-code-on-release-builds/,但不确定它是否有帮助。

One option would be to use build variants . 一种选择是使用构建变体 Basically, this lets you specify code that is only included in either the debug or release versions of your app. 基本上,这允许您指定仅包含在应用程序的调试版或发行版中的代码。 You do this by putting the code you only want in one version in src/debug or src/release instead of src/main . 你可以通过在src/debugsrc/release而不是src/main中将你想要的代码放在一个版本中来实现。 Exactly what code goes where is up to you. 究竟代码在哪里取决于你。 You have a couple of options here. 你有几个选择。

1. You could have two copies of MyApplication , one extending MultiDexApplication in src/debug and the other extending the base Application class in src/release . 1.您可以拥有两个MyApplication副本,一个在src/debug扩展MultiDexApplication ,另一个在src/release扩展基本Application类。 This way, the multi-dex code is only included in the debug build, not the release build. 这样,多索引代码仅包含在调试版本中,而不包含在发布版本中。 You still specify your application class in src/main/AndroidManifest.xml . 您仍然在src/main/AndroidManifest.xml指定您的应用程序类。 This option is best if you don't have anything else in your Application class. 如果您的Application类中没有任何其他内容,则此选项最佳。

2. If your application class has significant code in it beyond multi-dex, it may be easier to have a base abstract class that contains everything else, and extend it in src/debug and src/main , adding the following to your debug version (as described here ): 2.如果你的应用程序类在multi-dex之外有重要代码,那么可能更容易拥有一个包含其他所有内容的基本抽象类,并在src/debugsrc/main扩展它,将以下内容添加到调试版本中(如所描述的在这里 ):

public class MyApplication extends BaseApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

3. The third option is to actually declare different Application classes in debug vs. release. 3.第三个选项是在debug和release中实际声明不同的Application类。 This can be done by having that declaration in src/debug/AndroidManifest.xml and the release equivalent. 这可以通过在src/debug/AndroidManifest.xml使用该声明和版本等效来完成。 Then, you can do whatever you want, including not declaring an application class in one version. 然后,您可以执行任何操作,包括不在一个版本中声明应用程序类。 This works well if you have several different parts in your class that are different between release and debug, or don't need an application class in one of them. 如果您的类中有多个不同的部分在发布和调试之间不同,或者在其中一个部分中不需要应用程序类,则此方法很有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android minifyEnabled true在调试和发布模式下不起作用 - Android minifyEnabled true is not working in Debug and Release mode 混淆(minifyEnabled为true)在Debug and Release中不起作用 - Obfuscation (minifyEnabled true) not working in Debug and Release 在用于调试版本的Application和用于发行版本的MultiDexApplication之间切换 - Toggle between Application for debug build and MultiDexApplication for release build 在调试模式下设置minifyEnabled后,api调用无法正常工作 - After set minifyEnabled true in debug mode api call not working properly minifyEnabled true 导致发布应用程序崩溃 - minifyEnabled true causes release app crash Flutter:使用 minifyEnabled 在发布模式下 Firestore 不可用错误 - Flutter: Firestore unavailable error on release mode with minifyEnabled minifyEnabled 导致应用程序在发布模式下崩溃 - minifyEnabled causing app to crash on release mode 当minifyEnabled为true时,如何在发布版本中修复“NoSuchFieldException:No field producerIndex” - android? - How to fix "NoSuchFieldException: No field producerIndex" in release build when minifyEnabled is true - android? 当minifyenabled在android中为true时,如何在制作发布apk后关闭log cat输出 - how to turn off log cat output after making release apk when minifyenabled is true in android 发布版本改造 api 中的 minifyEnabled true 和 shrinkResources true 不起作用 - minifyEnabled true and shrinkResources true in release build retrofit api's are not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM