The App Id is a part of the application metadata. It has the name "RIM_APP_WORLD_ID".
// If you are targeting 4.3+, use this:
String myAppName = "My Amazing App"; //Name of your app
CodeModuleGroup group = CodeModuleGroupManager.load( myAppName );
String appid = group.getProperty("RIM_APP_WORLD_ID");
Check out this sample code Getting Info from the App World