在我的应用程序中运行不同的应用程序类(用于调试)

问题描述:

仅当我调试我的应用程序时,我想解决应用程序中的某些依赖项,例如我想仅使用Stetho进行调试,而不是最终版本,我该如何实现那?在我的应用程序中运行不同的应用程序类(用于调试)

我试图创建另一个文件夹名为调试和创建DebugApp从我的应用程序扩展,但我不知道怎么办好这个DebugApp,也许我应该在摇篮添加的东西?

public class DebugApp extends App { 
    @Override 
    protected void init() { 
     super.init(); 
     DebugAppInitializer.initStetho(this); 
     DebugAppInitializer.initCrashczyk(this); 
    } 
} 

这将是真棒,如果我可以将其链接到我的productFlavors

+1

与'src/main /'一起创建'src/debug /'。在'src/debug /'源代码集中有你自定义的'Application'。在src/debug /'sourceset中有一个'AndroidManifest.xml'文件,用于引入你自定义的'Application'。 – CommonsWare

+0

那么我应该将调试应用程序分离到另一个模块? – henorek

+0

否,同一模块内的单独源代码集。 [本示例应用](https://github.com/commonsguy/cw-omnibus/tree/master/Diagnostics/WebServer)显示的想法,尽管在这种情况下,它增加一个活性和服务到'debug'构建,而不是自定义的“应用程序”。 – CommonsWare

你可以把你的代码是这样的:

if(BuildConfig.DEBUG){ 
... //debug init 
} else { 
....//release init 
} 

OR

1)创建项目结构像这样:

enter image description here

2)摇篮口味:

productFlavors { 
    driver { 
     applicationId "android.com.driver" 
     versionName "1.0" 
     buildConfigField "Boolean", "DRIVER_SESSION", "true" 
     minSdkVersion 16 
     targetSdkVersion 23 
    } 
    passenger { 
     applicationId "android.com.passenger" 
     versionName "1.0" 
     buildConfigField "Boolean", "DRIVER_SESSION", "false" 
     minSdkVersion 16 
     targetSdkVersion 23 
    } 
} 
sourceSets { 
    passenger { 
     manifest.srcFile 'src/passenger/AndroidManifest.xml' 
    } 
    driver { 
     manifest.srcFile 'src/driver/AndroidManifest.xml' 
    } 
} 

3)需要创造型动物类,并把应用程序的名称为每个清单文件:

体现在驱动程序包 - >

<application 
     android:name=".application.YourFirstAppInDriverPackage" 
     ... 
     > 

在乘客包裹中显示 - >

<application 
     android:name=".application.YourSecondAppInPassengerPackage" 
     ... 
     > 
两个项目之间

4)交换机开发模式:

enter image description here

+1

是的,但我宁愿将其分隔到另一个文件夹 – henorek

我相信你正在寻找这样:https://www.littlerobots.nl/blog/stetho-for-android-debug-builds-only/

TL; DR:为了激活您的DebugApp你需要请按照以下步骤覆盖debug文件夹中的清单:

<manifest 
    package="com.mycompany" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"> 

    <application 
     tools:replace="android:name" 
     android:name=".DebugApp"/> 

</manifest>