Import R.java handset into my plugin?

I am trying to make a phonegap plugin to open an action to play videos using the android videoView (because it allows it to watch how the Android browser cannot play html video). Everything works for me, but I have to include R.java from the phonegap package in my plugin for work / build and troubleshooting "R cannot be resolved to a variable".

my plugin is at https://github.com/mikeRead/videoview if you read "important!". you can find out what i have to do to fix the problem of R ....

Basically, the user should change the import statement in my plugin to his phonegap package name, so R.id and R.layout work.

I am a web developer and far from Android encoder or phone, so any help / hints on this (except for eclipse fixes) are welcome

THANKS!

+7
java android r.java-file cordova
source share
1 answer

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

+2
source share

All Articles