PackageManager.installPackage() ?

3140 views
Skip to first unread message

Ben Dodson

unread,
Mar 18, 2008, 9:14:35 AM3/18/08
to Android Developers
I'm trying to write an app that can install other applications. I came
across the installPackage() method and tried to give it the URI of
a .apk file on the web, but I'm getting a permission denied error. I
have what I think should be the correct permissions in my manifest:

</uses-permission><uses-permission
android:name="android.permission.INSTALL_PACKAGES">
</uses-permission><uses-permission
android:name="android.permission.DELETE_PACKAGES">

is there anything else I need? should this method work?

Also, I want my application to be the launch point of these other
apps, and I would prefer them to not show up in the main listing of
programs. Is this possible?


Thanks,
Ben

hackbod

unread,
Mar 18, 2008, 1:06:15 PM3/18/08
to Android Developers
Hi Ben,

The package install API is still under development, and may not be
working. I think right now the install package permission is also
only allowed for built-in applications, since it will allow you to
circumvent the permission system by deciding which permissions to
allow: this API is really intended for use by a graphical installer
that is included with a shipping device, and when third party apps
want to install something we will probably have an activity they can
launch to do so (to ensure the user is always involved in this
process).

I don't understand what you mean by your app being the launch point of
other applications. Do you mean you want to make your own private
world of applications that others don't see? If so, I really don't
think you want to use this API: this is for installing top-level
applications in to the system. If you want to manage applications
yourself, you can just put their code into your private file area, use
the function to parse the manifest if you want, and load that code
directly yourself.

Ben Dodson

unread,
Mar 18, 2008, 2:57:37 PM3/18/08
to Android Developers
That sounds like exactly what we're hoping to do. Can you point my
towards any references for how to load the code from the filesystem?

Thanks,
Ben

hackbod

unread,
Mar 18, 2008, 4:46:40 PM3/18/08
to Android Developers
You should be able to use android.lang.PathClassLoader to create a
class loader for .apk. Just make an instance of the object with the
full path the .apk and, if desired, a parent class loader that it
links with or ClassLoader.getSystemClassLoader().getParent() for just
the frameworks.

You can also load the resources from the .apk with code like this:

AssetManager assets = new AssetManager();
assets.addAssetPath(apkPath);
Resources r = new Resources(assets, myMetrics,
myConfiguration);

Ben Dodson

unread,
Mar 19, 2008, 10:14:00 AM3/19/08
to Android Developers
Thanks a ton, I'll play with this tonight and see how it goes.

Ben

Peli

unread,
Mar 19, 2008, 11:24:37 AM3/19/08
to Android Developers
This sounds interesting. Can an apk file be put into the lib or assets
folder? If yes, then this would be an alternative to lib/jar for
including widgets with graphics and code...

Peli

tu

unread,
Mar 19, 2008, 12:33:56 PM3/19/08
to android-d...@googlegroups.com
Yes, an apk file can be put into assets folder or any accessible path! I have been working on it over 2 months. I can download an apk file by http and run code or use its resources dynamically, including version control features. It's a platfrom similar to OSGi or EclispeRCP. Now nearly everything goes smoothly.

2008/3/19, Peli <peli...@googlemail.com>:

Ben Dodson

unread,
Mar 22, 2008, 6:15:41 PM3/22/08
to Android Developers
Well thanks for the help, I made some good progress on this once I
realized I needed the full path to the apk file (/data/data/my.app/
files/test.apk)

Now I'm able to load classes dynamically using the PathClassLoader and
instantiate/run methods on them. Dependency classes also load with no
problem, including the R class.

I'm still stuck on the last step of running an activity from
another .apk. I've tried two main approaches. One is to use
startActivity(this, foreignClass), so the context is that of the
calling app. The result:

ERROR/lobos(796): java.lang.RuntimeException: Not supported in system
context
ERROR/lobos(796): at
android.app.ApplicationContext.getPackageName(ApplicationContext.java:
312)
ERROR/lobos(796): at
android.content.ComponentName.<init>(ComponentName.java:74)
ERROR/lobos(796): at android.content.Intent.setClass(Intent.java:
2740)
[...]

I also tried instantiating the foreignClass (extends Activity) and
using that as the context:

ERROR/lobos(794): at
android.app.Activity.startSubActivity(Activity.java:1935)
ERROR/lobos(794): at
android.app.Activity.startActivity(Activity.java:1978)
ERROR/lobos(794): at
com.lobos.app.LobosAPKApp.launch(LobosAPKApp.java:68)

I'm sure there's a pretty hefty piece I'm not handling, but any ideas
what?




I tried overriding the getPackageName method


On Mar 19, 12:33 pm, tu <tuchengy...@gmail.com> wrote:
> Yes, an apk file can be put into assets folder or any accessible path! I
> have been working on it over 2 months. I can download an apk file by http
> and run code or use its resources dynamically, including version control
> features. It's a platfrom similar to OSGi or EclispeRCP. Now nearly
> everything goes smoothly.
>
> 2008/3/19, Peli <peli0...@googlemail.com>:

Ben Dodson

unread,
Mar 22, 2008, 9:01:53 PM3/22/08
to Android Developers
For anyone following...

I figured it out, and it's not so hard at all. I had to use the
createPackageContext to get this working. Surprisingly easy :)

Thanks again for your pointers!

tu

unread,
Mar 23, 2008, 2:15:42 PM3/23/08
to android-d...@googlegroups.com
Can you launch an activity defined in "/data/data/my.app/files/test.apk" ?!
For start an acitivity in a installed apk, just use Intent.setClassName(packageName, foreignClassName)

 
2008/3/23, Ben Dodson <bjdo...@gmail.com>:

Ben Dodson

unread,
Mar 23, 2008, 6:17:54 PM3/23/08
to Android Developers
Well it looks like I jumped the gun.. no, I haven't yet been able to
launch an activity from my data folder. I can load classes from them
with the PathClassLoader, but can't call an activity. I think I need
to somehow create a new Context with my PathClassLoader as the
ClassLoader. Then use this context to run:

Context packageContext =
context.createPackageContext(mclassname.substring(mclassname.lastIndexOf(".")),
Context.CONTEXT_IGNORE_SECURITY);
intent = new Intent();
intent.setClass(packageContext, m_class);

no luck yet on creating that context.. I'm not positive this is the
way to go, but it's my best guess for now.

Is it possible to load the contents of a foreign .apk's manifest file
into the current context?


On Mar 23, 2:15 pm, tu <tuchengy...@gmail.com> wrote:
> Can you launch an activity defined in "/data/data/my.app/files/test.apk" ?!
> For start an acitivity in a installed apk, just use
> Intent.setClassName(packageName,
> foreignClassName)
>
> 2008/3/23, Ben Dodson <bjdod...@gmail.com>:

vladimir.sch...@gmail.com

unread,
Mar 23, 2008, 7:54:50 PM3/23/08
to Android Developers
http://code.google.com/android/devel/bblocks-manifest.html

"Every Activity must have an <activity> tag in the manifest whether it
is exposed to the world or intended for use only within its own
package. If an Activity has no matching tag in the manifest, you won't
be able to launch it."

Ben Dodson

unread,
Mar 23, 2008, 8:03:42 PM3/23/08
to Android Developers
Right, but is there a way to load these dynamically, given another
manifest file? Can I edit my manifest file on the fly and inject the
activities of the .apk I want to load? Or can I adjust the context I'm
running in so the downloaded .apk's manifest is used instead of my own
until it is removed from the application stack?

On Mar 23, 7:54 pm, "vladimir.schlott.andr...@gmail.com"

vladimir.sch...@gmail.com

unread,
Mar 23, 2008, 8:16:23 PM3/23/08
to Android Developers
I guess package manager API is what you want, hackbod said is still
under development.

tu

unread,
Mar 23, 2008, 9:17:38 PM3/23/08
to android-d...@googlegroups.com
I do think so! I think there is no way to register a manifest other than package manager API now. The nearest approach I got is the following:
     ActivityThread thread = ActivityThread.currentActivityThread();
     PackageManager manager = this.getPackageManager();
     PackageInfo info = manager.getPackageArchiveInfo("/data/data/my.test/install/testHello.apk", 0xffff);
     ApplicationInfo ai = info.applicationInfo;
     ai.dataDir = "/data/data/my.test";
     ai.processName = "my.test";
     ai.uid = 10008;  //uid of the running package
     ActivityThread.PackageInfo actPackageInfo = thread.getPackageInfo(ai, 
                Context.CONTEXT_IGNORE_SECURITY|Context.CONTEXT_INCLUDE_CODE);  
     thread.getApplicationThread().scheduleLaunchActivity(intent, null, ai.packageName, "/data/data/my.test/", 
       "/data/data/my.test/", ai.theme, ai.nonLocalizedLabel, ai.labelRes, null, null, null, false);
 
It would work if ApplicationThread class is public.

 

Ben Dodson

unread,
Mar 24, 2008, 9:02:32 AM3/24/08
to Android Developers
what does that snippet do now?

I was also thinking on my approach of creating my own Context with my
own ClassLoader. I haven't thought it out completely but it may be a
security risk, as you could override "com.google.os.securePackage" to
do something else, and it may be possible to skirt around permissions.
This is of course not what I'm trying to do =) But it may mean that
method is not possible and/or should be examined as a possible
security risk.

I'm going to try something later today- I can define my own
Context.getResources() method, so rather than launching activities,
I'll have an alternate entry point called MyActivity.load(), called
from within a Context with getResources overwritten to use that apk's
resources.

So many paths, so many dead ends ; (


On Mar 23, 9:17 pm, tu <tuchengy...@gmail.com> wrote:
> I do think so! I think there is no way to register a manifest other than
> package manager API now. The nearest approach I got is the following:
> ActivityThread thread = ActivityThread.currentActivityThread();
> PackageManager manager = this.getPackageManager();
> PackageInfo info =
> manager.getPackageArchiveInfo("/data/data/my.test/install/testHello.apk",
> 0xffff);
> ApplicationInfo ai = info.applicationInfo;
> ai.dataDir = "/data/data/my.test";
> ai.processName = "my.test";
> ai.uid = 10008; //uid of the running package
> ActivityThread.PackageInfo actPackageInfo = thread.getPackageInfo(ai,
> Context.CONTEXT_IGNORE_SECURITY|Context.CONTEXT_INCLUDE_CODE
> );
> thread.getApplicationThread().scheduleLaunchActivity(intent, null,
> ai.packageName, "/data/data/my.test/",
> "/data/data/my.test/", ai.theme, ai.nonLocalizedLabel, ai.labelRes,
> null, null, null, false);
>
> It would work if ApplicationThread class is public.
>
> 2008/3/24, vladimir.schlott.andr...@gmail.com <
> vladimir.schlott.andr...@gmail.com>:
> > > > > > > > > > > You should be able to use android.lang.PathClassLoaderto create a
> ...
>
> read more »

vladimir.sch...@gmail.com

unread,
Mar 24, 2008, 9:49:05 AM3/24/08
to Android Developers
If you think API has security risk, please report bug to Google. We
all want secure Android when first device ship.
> ...
>
> read more »

tu

unread,
Mar 24, 2008, 11:52:20 AM3/24/08
to android-d...@googlegroups.com
As far as I know, launching an activity in an uninstalled apk is impossible. The problem is not the correct context enviroment, but the manifest file not be registered. That means you can use resouces and codes in dynamic loaded apk, but can not use activities, services directly.

2008/3/24, Ben Dodson <bjdo...@gmail.com>:

hackbod

unread,
Mar 24, 2008, 6:21:21 PM3/24/08
to Android Developers
Correct, for something to run as a top-level application (with its
components accessible to others), it needs to be registered with the
system through the package manager. The way to do this is with
PackageManager.installPackage(). I'm not sure if that is currently
available to apps, and anyway it is not yet completely implemented;
this part of the system is still under construction.

On Mar 24, 8:52 am, tu <tuchengy...@gmail.com> wrote:
> As far as I know, launching an activity in an uninstalled apk is impossible.
> The problem is not the correct context enviroment, but the manifest file not
> be registered. That means you can use resouces and codes in dynamic loaded
> apk, but can not use activities, services directly.
>
> 2008/3/24, Ben Dodson <bjdod...@gmail.com>:
> ...
>
> read more »

Ben Dodson

unread,
Mar 24, 2008, 7:47:44 PM3/24/08
to Android Developers
And only an activity must be defined in a top-level application?
> ...
>
> read more »

hackbod

unread,
Mar 24, 2008, 9:20:56 PM3/24/08
to Android Developers
On Mar 24, 4:47 pm, Ben Dodson <bjdod...@gmail.com> wrote:
> And only an activity must be defined in a top-level application?

Hm, not sure I understand. Applications have everything defined by
AndroidManifest.xml -- activities, services, receivers, etc.

Ben Dodson

unread,
Mar 25, 2008, 8:58:58 AM3/25/08
to Android Developers
sorry, mistyped a bit :)

if I'm correct, activities can only exist when defined by a top-level
application, which must be installed through the package manager.

If that's the case, would it be worthwhile to file a feature request?

hackbod

unread,
Mar 25, 2008, 12:31:40 PM3/25/08
to Android Developers
On Mar 25, 5:58 am, Ben Dodson <bjdod...@gmail.com> wrote:
> if I'm correct, activities can only exist when defined by a top-level
> application, which must be installed through the package manager.

Correct.

> If that's the case, would it be worthwhile to file a feature request?

You can, but it is extremely unlikely this is something we'd have time
to address for 1.0.

Ben Dodson

unread,
Mar 26, 2008, 8:34:14 PM3/26/08
to Android Developers
Extremely unlikely is better than no chance at all :) I posted a
request here if anyone's interested:
http://code.google.com/p/android/issues/detail?id=545

thanks for the help Diane.


Ben

Wesley Sagittarius

unread,
May 21, 2008, 1:34:32 AM5/21/08
to Android Developers
I also trying to install, uninstall, run apk using installPackage() it
got permission problem...

is there any one successfully found the solution???
can share out??? please...


Wesley.

Wesley Sagittarius

unread,
May 29, 2008, 1:23:25 AM5/29/08
to Android Developers
hi all,


I know onces the installation API ready... what I study/research now
means nothing... but I really wan to learn it...
I hope someone can guide me more...

regarding the "android.permission.INSTALL_PACKAGES" the system not
grant d permission for me...

so... so fast I only get this thing... but unable to start the
activity can you show me some way???

//FOLDER
data
|- data
|- com.google.a
|- com.google.android
|- AirStrike.apk
//CODING
Object invoke=null;

//LOAD CLASS
PathClassLoader loader = new PathClassLoader(
"/data/data/com.google.a/com.google.android/
AirStrike.apk",
ClassLoader.getSystemClassLoader().getParent());

Class calledClass=null;
calledClass = loader.loadClass("com.google.android.AirStrike");

//LOAD METHOD
Method method = calledClass.getMethod("onCreate",
new Class[] { Bundle.class });

Object calledActivity = calledClass.newInstance();


//TO LOAD THE RESOURCES.
AssetManager assetManager = new AssetManager();
assetManager.addAssetPath("/data/data/com.google.a/
com.google.android/");

DisplayMetrics metrics = new DisplayMetrics();
metrics.setTo(super.getResources().getDisplayMetrics());
Resources mResources = new Resources(assetManager, metrics, null);

//TO RUN THE CLASS, IT CAN RUN BUT CANNOT FIND RESOURCES.
invoke=method.invoke(calledActivity, new Object[] { icicle});

//TO START ACTIVITY, BUT SOMEHOW IT SAID MISSING
"AndroidManifest.xml".
Intent it=new Intent(this, calledClass);
it.setClassName("com.google.a", "com.google.android.AirStrike");
startActivity(it);

Question
-------------
1. How to RUN the apk??
2. Invoke call to run and startActivity which one is better??? is it
startActivity??? how can I do so???


can you help me to correct it??? Thanks.



Wesley.

On May 21, 1:34 pm, Wesley Sagittarius <sit06...@gmail.com> wrote:
> I also trying to install, uninstall, run apk usinginstallPackage() it

Wesley Sagittarius

unread,
May 29, 2008, 1:30:14 AM5/29/08
to Android Developers

Wesley Sagittarius

unread,
May 29, 2008, 4:19:16 AM5/29/08
to Android Developers
Reply all
Reply to author
Forward
0 new messages