Archived:Installing and uninstalling MIDlets programmatically
Article Metadata
Compatibility
Platform Security
Article
Description
The following code demonstrates how to install and uninstall a MIDlet programmatically using the Software Installer APIs.
Solution
Installing a MIDlet
A MIDlet can be installed using the SW installer APIs as below:
Capability requirements: TrustedUI
SwiUI::RSWInstSilentLauncher iLauncher;
SwiUI::TInstallOptions iOptions;
SwiUI::TInstallOptionsPckg iOptionsPckg;
// Connect to software installer server
User::LeaveIfError(iLauncher.Connect());
iOptions.iUpgrade = SwiUI::EPolicyNotAllowed;
iOptions.iOCSP = SwiUI::EPolicyAllowed;
iOptions.iDrive = 'C';
iOptionsPckg = iOptions;
iLauncher.SilentInstall( iStatus, KPathAndFileNameOfJarFile, iOptionsPckg );
SetActive();
The above code is meant to be run by an active object (derived from CActive) and uses the asynchronous version of SilentInstall() method.
It is also possible to install a MIDlet using the CDocumentHandler API. For more information on this API, refer to SDK Help.
Uninstalling a MIDlet
When a MIDlet suite (possibly containing several MIDlets) is installed, the system creates a Suite UID for it and stores it in the Java registry. However, the only way to uninstall a MIDlet is to uninstall the parent MIDlet Suite. The MIDlet Suite UID must be passed to SwiUI::RSWInstSilentLauncher. However, this UID cannot be retrieved with methods from RApaLsSession.
Every time a MIDlet is installed, a new UID is generated for the application. Installer also creates some files for each installed MIDlet:
C:\private\102033E6\MIDlets\[UID]\ (directory named with MIDlet Suite UID)
MIDlet .jar and .jad files are copied to this directory. It is possible to extract the MIDlet context (Suite) UID by searching the C:\System\install\registry\ directory for "<UID>.reg" files. If the MIDlet name appears inside the .reg file, it can be assumed that the registry file belongs to the correct MIDlet Suite. This UID can then be passed to SwiUI::RSWInstSilentLauncher in order to uninstall the Suite.
void CJarUninstallAppUi::UninstallMIDletL()
{
RFs fileSession;
CDir* dirList;
TInt i;
TFileName fullPath;
TUint contextUid;
TFileName fileName;
_LIT(KDirName1, "c:\\system\\install\\registry\\");
_LIT(KFileSpec1,"c:\\system\\install\\registry\\*.*");
User::LeaveIfError( fileSession.Connect() );
CleanupClosePushL( fileSession );
// Get the file list, sorted by name
User::LeaveIfError( fileSession.GetDir( KFileSpec1,
KEntryAttMaskSupported,
ESortByName, dirList ) );
CleanupStack::PushL(dirList);
for ( i=0; i < dirList->Count(); i++ )
{
fileName = (*dirList)[i].iName;
if( fileName.Compare(_L("temp")) == KErrNone )
{
continue;
}
fullPath = KDirName1;
fullPath.Append(fileName);
RFile lFile;
TInt err = lFile.Open( fileSession, fullPath, EFileShareAny | EFileRead );
if(err == KErrNone)
{
TInt lSize;
lFile.Size(lSize);
RBuf8 lBuf;
lBuf.Create(lSize);
lBuf.CleanupClosePushL();
lFile.Read(lBuf);//read contents into buffer
RBuf buff;
buff.Create(lSize);
buff.CleanupClosePushL();
buff.Copy(lBuf);//Copy into 16 bit buffer
lFile.Close();
if (err == KErrNone)
{
// Check for MIDlet name in the file
err = buff.Find(_L("somemidlet.jar"));
if (err != KErrNotFound)
{
TLex lex( fileName.Mid(0,8) );
lex.Val( contextUid, EHex );
SwiUI::RSWInstSilentLauncher inst;
inst.Connect();
SwiUI::TUninstallOptions options;
SwiUI::TUninstallOptionsPckg optionsPckg;
options.iKillApp = SwiUI::EPolicyAllowed;
options.iBreakDependency = SwiUI::EPolicyAllowed;
optionsPckg = options;
TInt resp = inst.SilentUninstall( TUid::Uid(contextUid),
optionsPckg,
SwiUI::KJavaMIMEType );
inst.Close();
}
}
CleanupStack::PopAndDestroy(2); // buff, lBuf
}
}
CleanupStack::PopAndDestroy(2); // dirList, fileSession
}


Contents
Joukovierumaki -
In Symbian3 installing JAR file gives -46 errorjoukovierumaki 12:32, 24 October 2011 (EEST)
Hamishwillee - @Joukovierumaki - do you have the right capabilities
Joukovierumaki - most likely you either haven't specified TrustedUI in the mmp file. Please confirm. Remember that when you use this capability you will need a DevCert to sign the file.hamishwillee 03:08, 27 October 2011 (EEST)
Madhavnarain - I am getting -1 when doing a silent uninstall
I am using the above APIs to do a silent uninstall.. I am getting -1 as return.. :(
I want to uninstall a sis package.
Below is the code I am using
SwiUI::TUninstallOptions options;
SwiUI::TUninstallOptionsPckg optionsPckg; options.iKillApp = SwiUI::EPolicyAllowed; options.iBreakDependency = SwiUI::EPolicyAllowed; optionsPckg = options; TUid kUid = {0x1004240E}; <<-- UID of the app package TInt resp = inst.SilentUninstall( kUid, optionsPckg, SwiUI::KSisMimeType );inst.Close();madhavnarain 12:37, 18 November 2011 (EET)
Hamishwillee - @Madhavnarain - KErrNotFound
Hi Madhavnarain
The error is KErrNotFound (search in developer library for e32err.h to get the full list of global error codes). I'm assuming that the return value you get this on is "resp"?. This isn't very helpful - I'd be checking that you've got the UID of the package file correct. If that doesn't work, ask on the Java forum.
Regards
Hamishhamishwillee 00:16, 21 November 2011 (EET)