The problem described in your question can be solved by adding an after_plugin_install hook to your plugin. I programmed a hook to change my action called SketchActivity.java , as shown below. If necessary, change the package name to your plugin.
#!/usr/bin/env node /* A hook to add R.java to the draw activiy in Android platform. */ var fs = require('fs'); var path = require('path'); var rootdir = process.argv[2]; function replace_string_in_file(filename, to_replace, replace_with) { var data = fs.readFileSync(filename, 'utf8'); var result = data.replace(to_replace, replace_with); fs.writeFileSync(filename, result, 'utf8'); } var target = "stage"; if (process.env.TARGET) { target = process.env.TARGET; } var ourconfigfile = path.join( "plugins", "android.json"); var configobj = JSON.parse(fs.readFileSync(ourconfigfile, 'utf8')); // Add java files where you want to add R.java imports in the following array var filestoreplace = [ "platforms/android/src/in/co/geekninja/plugin/SketchActivity.java" ]; filestoreplace.forEach(function(val, index, array) { if (fs.existsSync(val)) { console.log("Android platform available !"); //Getting the package name from the android.json file,replace with your plugin id var packageName = configobj.installed_plugins["in.co.geekninja.Draw"]["PACKAGE_NAME"]; console.log("With the package name: "+packageName); console.log("Adding import for R.java"); replace_string_in_file(val,"package in.co.geekninja.plugin;","package in.co.geekninja.plugin;\n\nimport "+packageName+".R;"); } else { console.log("No android platform found! :("); } });
put it in the /hooks/after_plugin_install/ in your plugin And add the following line between the <platform name="android"> ... </platform> :
<hook type="after_plugin_install" src="hooks/after_plugin_install/hook-add-r-import.js" />
The code will be executed every time someone adds a plugin using the cordova plugin add command and writes the import name R.java right below the package declaration
insomniac
source share