Wednesday, January 12, 2011

How to access resources from other application (.apk)




We might be interested in accessing the resources of other application. Or inflate a particular view from other applications layout xml file.In this post, we will discuss more about how to achieve this.

1 - What ever we need to access (resources or layout), we need two main things from other application .
       
1 - R.java.
Required for you during compilation time.

2 - Application's context.
Resources needs to be inflated with the correct application's 'Context' instance.Else at runtime the prog will crash.

2 - The app1(.apk) needs to be installed on the device in order for you to create app1's context instance and then inflate the resources / layouts.

app1 :
------
Create a an android application with package (com.android.app1 ) which contains resources you want to expose to others.

AndroidManifest.xml:

Manifest file doesnt have any entry for activity or application tags. it just says, under which package the resources will be present.


package="com.android.app1"
android:versionCode="1"
android:versionName="1.0">




Keep the resources you want to expose in drawables. In this case i have kept the picture named 'pic1.jpg' .

app2:

1 - Create an android application with package name com.android.app2.

we need to include the com.android.app1.R.class into app2.

2 - Select project Right click -> Properties -> Java Build Path -> Libraries -> Add External class folder -> "Choose the app1/bin folder"
Now app2 can reference 'com.android.app1.R'

And create the context for app1.

Context otherAppContext = getApplicatoinContext().createPackageContext("com.android.app1", Context.CONTEXT_IGNORE_SECURITY);
Bitmap b1 = BitmapFactory.decodeResource(otherAppContext.getResources(),com.android.app1.R.drawable.pic1);


Use the bitmap from 'app1' to apply to a Imageview.

Layout main.xml
------------------


android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/rajini"
/>
android:id="@+id/image2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>



app2.java:
-----------

package com.android.app2;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;


public class app2 extends Activity {
ImageView imgView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgView = (ImageView)findViewById(R.id.image2);
try
{
Context otherAppContext = getApplicationContext().createPackageContext("com.android.app1", Context.CONTEXT_IGNORE_SECURITY);
Bitmap b1 = BitmapFactory.decodeResource(otherAppContext.getResources(),com.android.app1.R.drawable.pic1);
BitmapDrawable bitmapDrawable = new BitmapDrawable(b1);
imgView.setBackgroundDrawable(bitmapDrawable);
}
catch(Exception e)
{

}
}
}


How to start an activity if it is not see in launcher application list.

$ am start -n com.android.app2/com.android.app2.app2
Starting: Intent { cmp=com.android.app2/.app2 }

8 comments:

  1. not able to import R file from second apk..ie (com.android.app1.R)..pls help

    ReplyDelete
  2. You have to do this step.

    project Right click -> Properties -> Java Build Path -> Libraries -> Add External class folder -> "Choose the app1/bin folder"

    Did u do this ??

    ReplyDelete
    Replies
    1. after doing above method also, still not able to import R file from second apk

      Delete
  3. Please help, i really need to resolve this problem ASAP

    ReplyDelete
  4. Hey buddy same unable to referance com.android.app1.R.drawable.pic1 from app2
    I already did project Right click -> Properties -> Java Build Path -> Libraries -> Add External class folder -> "Choose the app1/bin folder"

    please help

    ReplyDelete
  5. Hi...
    com.example.app1 cannot be resolved to a variable .....I got this error message...how to solve...please reply

    ReplyDelete
  6. Sir , what i want to do is inflate my service or the layout defined in my service over any app.......i mean if any of the app present in the phone starts i want my service to run before that........i have been searching for this from a long long time.........please help me

    ReplyDelete
  7. could you update this with android studio

    ReplyDelete