Import phonegap's R.java to my plugin?

让人想犯罪 __ 提交于 2019-12-10 03:09:22

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/24227029/import-phonegaps-r-java-to-my-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!