android studio - 如何制作两个自定义应用程序 类

android studio - How to make two custom application classes

我想为我的 android 项目设计两个自定义应用程序。自定义应用程序是扩展应用程序的应用程序。我希望我的发布版本有一个自定义应用程序,而调试版本有另一个。 LeakCanary 站点在标记为“自定义和使用无操作依赖项”的部分中讨论了这个 here

我似乎无法弄清楚如何在 androidManifest 中声明另一个应用程序。现在我将其设置为使用这样的单个自定义应用程序:

public class MyFirstCustomApplication extends Application {

@Override public void onCreate() {
    super.onCreate();
    LeakCanary.install(this);
}
}

如何使上述代码仅用于调试?

并且 android 清单覆盖如下所示:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="org.somesite.cool.MyFirstCustomApplication">

如何发布另一个 MySecondCustomApplication?

更新:关于在 gradle 中查看 bulidTypes 的建议想象我为我的调试应用程序覆盖做了以下结构:

[Project Root]
  -[Module]
    -src
      -main
        -ExampleApplication 
        -res

      -debug
        -DebugExampleApplication
        -res

所以如果我正确理解 buildTypes 那么如果我只有一个用于发布和调试的主文件夹(它的 default/fall 后面)但是如果我指定一个调试文件夹然后如果我 运行 我可能有的任何调试风格,如 mockDebug、qaDebug 等,然后它将首先在调试文件夹中查找文件,如果找不到则转到 main,对吗?

此外,我将如何在我的清单中声明使用哪一个? gradle 文件会是什么样子?

I cant seem to figure out how i would declare another application in the androidManifest.

清单中只有一个。

so if i understand buildTypes correctly then if i only have a main folder its used for both release and debug (its the default/fall back) but if i specify a debug folder then if i run any debug flavors i might have like mockDebug,qaDebug, etc then it will first look in the debug folder for the file and then go to main if not found, is that correct ?

假设 mockDebugmock 风格和 debug 构建类型(qaDebug 也是如此),那么是的。

因此,您的 Application class 在 debugrelease 构建类型中的同一个 Java 包中。清单和使用您的 Application class 的 Java 代码将在那个 Java 包中引用它。您获得的 Application class 的哪个版本取决于您进行的是 debug 构建还是 release 构建。

Also, then how would i declare in my manifest which one to use ?

和正常一样。

What would the gradle file look like ?

不需要为此更改任何 Gradle 构建文件。

on the suggestions of looking into bulidTypes in gradle imagine i did the following structure for my debug application override

不,至少我会如何查看项目树。你会:

[Project Root]
  -[Module]
    -src
      -main
        -java
          - your.package.here
            - Things.java
            - ThatAre.java
            - NotYourCustomApplication.java
            - ButOtherwiseAreCommon.java
            - ForAllBuildTypes.java
        -res

      -debug
        -java
          - your.package.here
            - YourCustomApplication.java
        -res

      -release
        -java
          - your.package.here
            - YourCustomApplication.java
        -res

请注意,在每个构建类型特定的源集中,它是相同的 your.package.here 和相同的 YourCustomApplication.java

如果您需要使用两个不同的 classes,您始终可以利用清单合并。在调试文件夹中,添加您的第二个应用程序 class 和仅包含新应用程序名称的清单

<manifest package="your.package"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:name=".DebugExampleApplication"
        tools:replace="android:name"/>
</manifest>