Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import phonegap's R.java to my plugin?

I am trying to make a phonegap plugin to open up a activity to play a video through android's videoView (because lets face it android's webview can't play html video). I got everything working but I have to include the R.java from phonegap's package into mine for my plugin to work/build and eliminate the "R can not be resolved to a variable" errors.

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

basically the user has to change an import statement in my plugin to their phonegap package name, so R.id and R.layout works.

I'm a web developer and by far from an android or phone gap coder so any help/hints on this (other than the eclipse fixes) is welcome

THANKS!

like image 266
Mike Read Avatar asked Jun 15 '14 06:06

Mike Read


1 Answers

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

like image 58
insomniac Avatar answered Oct 13 '22 22:10

insomniac