DebugApplication

在AndroidStudio中我们可以把一些只有在debug包下生效的代码不引入到release版本中,比如接入LeakCanary,下面是一个demo

包结构

DebugApplication_第1张图片
image.png

关键类

build.gradle只引入LeakCanary的debug版本

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'

}

主包的Application类

public class AppDemoApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        debugConfig();
    }

    /**
     * 只在debug包下生效的配置
     */
    protected void debugConfig() {
        Toast.makeText(this, " AppDemoApplication debugConfig", Toast.LENGTH_LONG).show();
    }
}

debug包的Application类

public class AppDemoDebugApplication extends AppDemoApplication {
    private static final String TAG = "AppDemoDebugApplication";

    @Override
    protected void debugConfig() {
        super.debugConfig();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
        Toast.makeText(this, " LeakCanary.install", Toast.LENGTH_LONG).show();
    }
}

debug包的AndroidManifest.xml




    
    


你可能感兴趣的:(DebugApplication)