Monday, 23 December 2013

Android Extracting apk from the device sample code


The following code explains you how to extract apk which is installed in the device.


Permissions Required :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent" >



    <ListView

        android:id="@+id/list"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />



</RelativeLayout>



MainActivity.class :


package com.example.simpleapkextractor;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.nio.channels.FileChannel;

import java.util.ArrayList;

import java.util.Iterator;



import android.app.Activity;

import android.app.ProgressDialog;

import android.content.pm.ApplicationInfo;

import android.content.pm.PackageManager;

import android.graphics.drawable.Drawable;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;



public class MainActivity extends Activity implements OnItemClickListener {

PackageManager manager;

private ArrayAdapter<String> mAdapter = null;

ArrayList<String> mPkgs = new ArrayList<String>();

ListView mListView;



@Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  mListView = (ListView) findViewById(R.id.list);

  mListView.setOnItemClickListener(this);

  new LoadMainContent().execute();

 }



 class LoadMainContent extends AsyncTask<Void, Void, Void> {

  private static final String TAG = "123456";

  ProgressDialog mProgressDialog = null;



  protected Void doInBackground(Void[] paramArrayOfVoid) {

   MainActivity.this.manager = getPackageManager();

   Iterator<?> localIterator = manager.getInstalledApplications(128).iterator();

   try {

    while (localIterator.hasNext()) {

     ApplicationInfo localApplicationInfo = (ApplicationInfo) localIterator.next();

     String pkgName = localApplicationInfo.packageName;

     String appLabel = manager.getApplicationLabel(localApplicationInfo).toString();

     Drawable icon = manager.getApplicationIcon(localApplicationInfo.packageName);

     Log.i(TAG, "pkg: " + pkgName);

     mPkgs.add(pkgName);

    }

   } catch (Exception localException) {

    localException.printStackTrace();

   }

   return null;

  }

  protected void onPostExecute(Void paramVoid) {

   super.onPostExecute(paramVoid);

   this.mProgressDialog.dismiss();

   mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, mPkgs);

   mListView.setAdapter(mAdapter);

  }

  protected void onPreExecute() {

   super.onPreExecute();

   this.mProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...", "Loading Apps");

  }

 }

 @Override

 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

  letsExtract(arg2);

 }



 private void letsExtract(int paramInt) {

  String str = (mPkgs.get(paramInt));

  ApplicationInfo localApplicationInfo = getAppInfo(str);

  File localFile1;

  File localFile2;

  if (localApplicationInfo.packageName.equalsIgnoreCase(str)) {

   localFile1 = new File(localApplicationInfo.publicSourceDir);

   localFile2 = new File(Environment.getExternalStorageDirectory() + "/Meher-ExtractAPKs/" + str + ".apk");

   File localFile3 = new File(Environment.getExternalStorageDirectory() + "/Meher-samples/ExtractAPK/");

   if (!localFile3.exists()) {

    localFile3.mkdirs();

   }

   try {

    FileChannel localFileChannel1 = new FileInputStream(localFile1).getChannel();

    FileChannel localFileChannel2 = new FileOutputStream(localFile2).getChannel();

    localFileChannel2.transferFrom(localFileChannel1, 0L, localFileChannel1.size());

    localFileChannel1.close();

    localFileChannel2.close();

    Toast.makeText(this, "" + mPkgs.get(paramInt), 1).show();

    Log.d("123456", "success");

   } catch (Exception e) {

    e.printStackTrace();

   }
  }
 }

 ApplicationInfo getAppInfo(String paramString) {

  Iterator<?> localIterator = this.manager.getInstalledApplications(128).iterator();

  ApplicationInfo localApplicationInfo = null;

  while (localIterator.hasNext()) {

   localApplicationInfo = (ApplicationInfo) localIterator.next();

   if (localApplicationInfo.packageName.equalsIgnoreCase(paramString)) {

    return localApplicationInfo;

   }
  }

  return localApplicationInfo;

 }
}

Note : you can extend this app by using custom adapter to show proper app name with app icon.

1 comment: