[英]How to debug application with “minifyEnabled true” and disable MultiDexApplication in release mode
When build.gradle
has lines: 当
build.gradle
有build.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/debug
或src/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/debug
和src/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.