Android多应用程序:如何在一个项目中创建两个应用程序?

huangapple go评论60阅读模式
英文:

Android multi app: how to create two apps within one project?

问题

我有一个几乎完成的Java应用程序,带有身份验证,并且需要向这个项目中添加另一个应用程序,以重用身份验证代码,例如。

据我所知,可以有某种带有不同图标的两个“主要活动”,可以分别启动它们。但我无法检查这个信息,因为不知道这叫什么名字,之前的任何尝试都使我走错了方向。

因此,问题是如何在Manifest中注册这些活动,以及如何配置“运行”菜单?
或者,如果我错了,还有哪些其他方法可以满足我的要求?

谢谢。

英文:

I have one almost completed Java application with authentication and need to add to this project another one app to reuse auth code, for example.

As I heard there could be some kind of two "main activities" with different icons to launch them separately. Also I cannot check this info, because don't know how this named and any tries before leads me in other way.

So the question is how to register those activities in Manifest and how to configure run menu?
Or if I'm wrong - what else ways exists which could fit for my requiremrnts?

Thanks.

答案1

得分: 1

你应该考虑为你的应用程序使用多个变体(flavors)。这使得你可以为每个变体设置不同的应用程序名称、图标和代码。

以下是在你的主模块的 build.gradle 中定义两个变体的示例:

buildTypes {
    debug {...}
    release {...}
}

flavorDimensions "main"

productFlavors {
    demo {
        dimension "main"
        applicationId "com.appdemo"
        versionNameSuffix "-demo"
    }
    full {
        dimension "main"
        applicationId "com.appfull"
        versionNameSuffix "-full"
    }
}

然后,你可以通过覆盖每个变体子目录中的默认文件(例如 yourmodule/demo/ 和 yourmodule/full/)来设置每个应用程序的资源(图片、代码、字符串等)。

英文:

You should consider using flavors for your apps. This allows you setting different app name, icons, code for each flavor.

Here is an example for defining two flavors in your main module's build.gradle:

buildTypes {
        debug{...}
        release{...}
    }
    // Specifies one flavor dimension.
    flavorDimensions "main"

productFlavors {
        demo {
            // Assigns this product flavor to the "main" flavor dimension.
            // If you are using only one dimension, this property is optional,
            // and the plugin automatically assigns all the module's flavors to
            // that dimension.
            dimension "main"
            applicationId "com.appdemo"
            versionNameSuffix "-demo"
        }
        full {
            dimension "main"
            applicationId "com.appfull"
            versionNameSuffix "-full"
        }
    }

You can then set the resources of each app (images, code, strings...) by overriding the default files in each flavor's subdirectory i.e. yourmodule/demo/ and yourmodule/full/

答案2

得分: 0

基本上需要使用活动创建两个入口点,并在其中添加图标。

因此,将其保留在这里以防万一。

<activity android:name=".MainActivity_1"
    android:icon="@mipmap/icon_1">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".MainActivity_2"
    android:icon="@mipmap/icon_2">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
英文:

Basically need to create two entrance points using activities and add icons inside them.

So left this here just in case.

    &lt;activity android:name=&quot;.MainActivity_1&quot;
        android:icon=&quot;@mipmap/icon_1&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;

    &lt;activity android:name=&quot;.MainActivity_2&quot;
        android:icon=&quot;@mipmap/icon_2&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;

huangapple
  • 本文由 发表于 2020年9月16日 19:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63919101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定