2017-06-20 105 views
0

我有兩個獨立的應用程序。目前我可以單獨安裝它們。我想了解如何使用單個APK安裝它們。使用1 APK安裝多個android應用程序

我希望能夠讓兩個應用程序仍然獨立運行,但我希望能夠將它們打包在一起。

這種方式,當用戶去谷歌播放商店,他們看到一個應用程序,但它實際上安裝這兩個應用程序。

感謝, 傑夫

+2

「我希望能有這兩個應用仍然相互獨立運行」 - 請**詳細解釋**這是從技術角度來看的意思,以及如果所有這些功能都在一個應用程序中會有什麼不同。 – CommonsWare

+0

第一個應用程序是一個將文檔轉換爲pdf的應用程序。第二個應用程序是打印服務。兩者都獨立運行。它們是獨立應用程序。在Google Play商店中,我希望它們顯示爲第一個應用程序的名稱。當第一個應用程序被下載並安裝時,我希望安裝這兩個應用程序。 – Jeff

+1

「他們是獨立的應用程序」 - 爲什麼?再次,分離的技術原因是什麼?你想要的是不可能的,原因很明顯(在你的場景中,將「打印服務」替換爲「惡意軟件」)。事實上,IIRC,你想要的是違反Play商店的服務條款。所以,雖然技術上你可以在你的APK內部安裝一個APK,然後解壓並安裝它(在用戶授予你的應用程序安裝其他應用程序的權利之後,然後繼續安裝第二個APK的安裝屏幕),但目前尚不清楚這是爲什麼符合您或您的用戶的最佳利益。 – CommonsWare

回答

0

您可以在清單文件中定義2 luncher活動像波紋管的結果是你想要什麼:

<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> 
1

你不能直接安裝兩個應用程序,而不顯示用戶,你可以做的事情就是創建一個新的應用程序,在這個應用程序中你將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 
     } 
    } 
0

謝謝你的建議。 我能夠通過以下方式解決問題,請注意我因爲無法發佈鏈接而遺漏了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 EnclosingMethod

- Error in gradle build after updating Android Studio with log4j

添加一個依賴項主要活動中應用的gradle: 編制項目(「:myserviceapp」)