2014-10-28 65 views
2

我有一個應用程序,例如。 com.app。我也有一個可選模塊com.app.module.m1。 可選模塊使用主應用程序中已有的大量資源,主要是字符串和drawable。使用主應用程序資源的附加APK模塊APK

該模塊本身是毫無意義的,不會運行(它只是一個只能由主應用程序啓動的組件集合)。沒有啓動器圖標,所有入口點需要signature級別權限。

如果可能,我寧願不重複這些資源,只使用主應用程序中的資源。

我可以以某種方式訪問​​com.app中從com.app.module.m1定義的資源嗎?

回答

0

使用這些方法:
public Resources getResourcesForApplication(String pkgName);PackageManagerResources
public int getIdentifier(String name, String defType, String defPackage);

例如,如果要在包「com.app」中獲取名爲「app_name」的字符串資源。

try{ 
    Resources res = pm.getResourcesForApplication("com.app"); 
    int resId = res.getIdentifier("app_name", "string", "com.app"); 
    if(resId > 0) 
     String mainAppName = getResources().getText(resId); 
} catch (NameNotFoundException e) { 
    e.printStackTrace(); 
} 
+0

XML怎麼樣?我期待使用@ com.app:string/string_id,但它會突出顯示爲錯誤。 – copolii 2014-11-04 01:33:29

+0

@copolii看看這個答案http://stackoverflow.com/a/16565454/2640554 – 2014-11-04 02:52:06