How do I Install external APKs necessary for my application in Android Studio?

253 views Asked by At

I have downloaded CSipSimple . Now for video call of this , I need to install CSipSimple-Codec-Pack and CSipSimple-Video-plugin apks . I need to install these two external apks with my Android application . These apks are necessary for installation of my application .

How can I install these apks with my Android application by programming ?

1

There are 1 answers

0
Bhargav On BEST ANSWER

there are 2 ways you could try this

  • Since the code of your dependent apps are open source, get the apks put them in your application's assets directory, then when your app is run copy the apks of the other apps to the external storage and initiate install

  • if both those apps are using gradle build then you can git clone the source of the apps, build them, then import them as modules to your application, that way when your app is installed these other apps are also installed along with yours

The 1st one only works when phones have sdcards, the 2nd one is simpler its more straight forward There is no code for this I assume you know how to get the source code of the applications you need right? just copy paste them into your application's root folder, then in android studio right click your project mouse over new-> click on module -> import gradle project -> then select the required application.

For the 1st method take a look at this thread

Code Snippet:

AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;

try {
    in = assetManager.open("myapk.apk");
    out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/myapk.apk");

    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()+"/myapk.apk")),
        "application/vnd.android.package-archive");

    startActivity(intent);

} catch(Exception e) { }

The above code copies apk from your assests folder to sdcard, then installs it on the phone.