The problem described in your question can be solved by adding an after_plugin_install
hook to your plugin . I have programmed a hook to modify my Activity called SketchActivity.java
as shown below. Change the package name to your plugin as required.
#!/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's 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! :(");
}
});
place it inside /hooks/after_plugin_install/
directory in your plugin
And add the following line between your <platform name="android"> ... </platform>
tag:
<hook type="after_plugin_install" src="hooks/after_plugin_install/hook-add-r-import.js" />
The code will be executed every time when someone adds the plugin using cordova plugin add
command and writes the R.java
import right below the package declaration