【问题标题】:Installing multiple android apps with 1 APK使用 1 个 APK 安装多个 Android 应用
【发布时间】:2017-11-23 11:12:59
【问题描述】:

我有两个独立的应用程序。目前我可以单独安装它们。我想了解如何使用单个 APK 安装它们。

我希望能够让两个应用程序仍然彼此独立运行,但我希望能够将它们打包在一起。

这样,当用户访问 google play 商店时,他们会看到一个应用程序,但实际上安装了这两个应用程序。

谢谢, 杰夫

【问题讨论】:

  • “我希望两个应用程序仍然能够相互独立运行”——请详细说明从技术角度来看这意味着什么,以及有什么区别如果所有这些功能都在一个应用程序中。
  • 第一个应用程序是将文档转换为 pdf 的应用程序。第二个应用程序是打印服务。两者都彼此独立运行。它们是独立的应用程序。在 Google Play 商店中,我希望它们显示为第一个应用程序的名称。下载并安装第一个应用程序后,我希望安装这两个应用程序。
  • “它们是独立的应用程序”——为什么?同样,分离的技术原因是什么?由于显而易见的原因,您想要的东西是不可能的(在您的场景中,将“打印服务”替换为“恶意软件”)。事实上,IIRC,出于这个原因,您想要的是违反 Play 商店的服务条款。因此,虽然从技术上讲,您可以在 APK 中包含一个 APK,然后提取并安装它(在用户授予您的应用程序安装其他应用程序的权利后,然后继续执行第二个 APK 的安装屏幕),但尚不清楚为什么会这样为了您或您的用户的最大利益。
  • 我看到另一个应用程序表现出这种行为。我安装了他们的单个应用程序,它安装了应用程序和打印服务。该应用程序只进行一次下载和安装,并且不会显示作为该过程的一部分正在安装的第二个应用程序。安装应用程序后,应用程序有一个图标,当我进入打印服务设置时,我看到他们也安装了打印服务。我希望能够遵循同样的约定。
  • 您的描述中没有任何内容暗示两个应用程序。您所描述的将是碰巧同时具有启动器活动和打印服务的单个应用程序的预期结果。

标签: android apk


【解决方案1】:

您可以在清单文件中定义 2 个午餐活动,如下所示,结果就是您想要的:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
        android:icon="@mipmap/ic_launcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

【讨论】:

  • 这对我有用。我创建了两个应用程序,并在将第二个应用程序上的第一个应用程序作为库迁移后。接下来从库的AndroidMenifest.xml 文件中删除intent-filter,问题就解决了。谢谢。
【解决方案2】:

您不能在不向用户显示的情况下直接安装两个应用程序,您可以做的是创建一个新应用程序,将您的 2 个 apk 文件放入 build 文件夹内的“raw”文件夹中。并推动它们一起打开,这些应用程序将向用户显示一个对话框询问权限,如果用户允许,将安装。

这是执行此操作的代码:

 for (int a=0;a<names.length;a++)
    {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = getBaseContext().getResources().openRawResource(array[a]);  // Here "array" is actually and array holding the reference of apk files (eg. R.raw.firstApp)
            out = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+names[a]+".apk");  // Here "names" is an array holding the names of your apk files (eg. "firstApp")
            Log.e("Path" ,Environment.getExternalStorageDirectory().getPath());
            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/"+names[a]+".apk")), "application/vnd.android.package-archive");
            startActivity(intent);
        }catch(Exception e){
            // deal with copying problem
        }
    }

【讨论】:

    【解决方案3】:

    感谢您的建议。 我能够通过以下方式解决该问题,请注意,由于无法发布链接,因此我省略了 https 部分: 创建一个主要活动项目。为服务创建另一个项目。将服务应用程序转换为库: developer.android.com/studio/projects/android-library.html#AddDependency

    修复任何合并清单问题: developer.android.com/studio/build/manifest-merge.html stackoverflow.com/questions/39178764/how-to-add-more-than-one-toolsreplace-in-android-manifest-application developer.android.com/studio/build/manifest-merge.html#inspect_the_merged_manifest_and_find_conflicts

    更新 proguard-rules.pro 文件: -keepattributes 封闭方法

    -Error in gradle build after updating Android Studio with log4j

    在 gradle 中添加对主活动应用程序的依赖: 编译项目(':myserviceapp')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多