将phonegap的R.java导入我的插件? [英] Import phonegap's R.java to my plugin?

查看:148
本文介绍了将phonegap的R.java导入我的插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个phonegap插件来打开活动,通过android的videoView播放视频(因为让我们面对它的android的webview无法播放HTML视频)。我得到了一切工作,但我必须将来自phonegap软件包的R.java包含在我的插件中工作/构建并消除R无法解析为变量错误。

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.

我的插件是 https://github.com/mikeRead/videoview 读重要!你可以找到解决R ...问题的方法。

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.

基本上用户必须将我的插件中的import语句更改为他们的phonegap包名称,因此R.id和R.layout可以工作。

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

我是一名网络开发人员,远离Android或手机间隙编码器,因此欢迎任何有关此问题的帮助/提示(除了日食修复)

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

感谢!

推荐答案

您的问题中描述的问题可以通过添加来解决after_plugin_install 挂钩您的插件。我编写了一个钩子来修改我的Activity,名为 SketchActivity.java ,如下所示。根据需要将包名更改为您的插件。

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! :(");
        }
    });

将它放在插件中的 / hooks / after_plugin_install / 目录中
并在< platform name =android之间添加以下行> ...< / platform> tag:

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" />

每当有人使用 cordova插件添加插件时,代码将被执行添加命令并在包声明下方写入 R.java 导入

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

这篇关于将phonegap的R.java导入我的插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆