2015-06-24 153 views
13

添加屬性我創建科爾多瓦插件,我想編輯AndroidManifest在「應用程序」標籤添加屬性。我知道config-file添加一個新的標籤,但我不知道是否可以更新現有的標籤。在「應用程序」標籤中AndroidManifest從科爾多瓦插件

例如,我有這樣的AndroidManifest:

<?xml version='1.0' encoding='utf-8'?> 
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.test.testCordova" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="CordovaApp" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize"> 
      <intent-filter android:label="@string/launcher_name"> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" /> 
</manifest> 

,我想在<application>標籤添加android:isGame=true

如果我不能做到這一點從plugin.xml中,我將創建一個鉤由我自己來編輯AndroidManifest但我希望它不會是必要的。

回答

5

我結束了做我不知道存在的plugin hook。插件鉤子是在插件中定義的鉤子,可以在插件添加或刪除之前或之後調用(或者在運行cordova plugin cli命令時,或者當cordova使用cordova platform add命令將該插件添加到平臺時)。

我不想因爲我認爲鉤不得不被放置在config.xml中,不能用插件來連接到使用掛鉤。

在這裏,我在plugin.xml文件的Android平臺部分加入這一行(我的要求比OP的稍有不同,但可將樣品反正幫助):

<platform name="android"> 
     <hook type="before_plugin_install" src="scripts/androidBeforeInstall.js" /> 
... 
    </platform> 

然後我寫的androidBeforeInstall.js鉤子腳本:

module.exports = function(ctx) { 
    var fs = ctx.requireCordovaModule('fs'), 
     path = ctx.requireCordovaModule('path'), 
     xml = ctx.requireCordovaModule('cordova-common').xmlHelpers; 

    var manifestPath = path.join(ctx.opts.projectRoot, 'platforms/android/AndroidManifest.xml'); 
    var doc = xml.parseElementtreeSync(manifestPath); 
    if (doc.getroot().tag !== 'manifest') { 
     throw new Error(manifestPath + ' has incorrect root node name (expected "manifest")'); 
    } 

    //adds the tools namespace to the root node 
    doc.getroot().attrib['xmlns:tools'] = 'http://schemas.android.com/tools'; 
    //add tools:replace in the application node 
    doc.getroot().find('./application').attrib['tools:replace'] = 'android:label'; 

    //write the manifest file 
    fs.writeFileSync(manifestPath, doc.write({indent: 4}), 'utf-8'); 

}; 

這是一個有點比plugin.xml中只是增加配置文件中線條更加複雜,但一旦你有良好的語法,它可以是非常強大的。

編輯:

出於某種原因,只有在鉤before_plugin_install,在AndroidManifest.xml中正確添加到平臺中更新,但它已恢復在platdorm添加結束的默認狀態。

因爲我找不出原因,所以我在plugin.xml中添加了以下代碼,以便在平臺添加結束時啓動腳本(plugin.xml中定義的luckilly鉤子不僅可以運行當添加或刪除插件)。

<hook type="after_platform_add" src="scripts/androidBeforeInstall.js" /> 
+1

在我的情況下,我使用「before_compile」掛鉤來更新清單。 – Shimrra

相關問題