Mik*_*ead 8 java android r.java-file cordova
我正在尝试制作一个phonegap插件来打开活动,通过android的videoView播放视频(因为让我们面对它的android的webview无法播放HTML视频).我得到了一切工作,但我必须将来自phonegap的包中的R.java包含到我的插件中工作/构建并消除"R无法解析为变量"错误.
如果您阅读"重要内容",我的插件将在https://github.com/mikeRead/videoview上显示.你可以找到解决R ...问题的方法.
基本上用户必须将我的插件中的import语句更改为他们的phonegap包名称,因此R.id和R.layout可以工作.
我是一名网络开发人员,远离Android或手机间隙编码器,所以欢迎任何帮助/暗示(除了日食修复)
谢谢!
您的问题中描述的问题可以通过在您的插件中添加一个after_plugin_install 钩子来解决。我编写了一个钩子来修改我的活动,SketchActivity.java如下所示。根据需要将包名称更改为您的插件。
#!/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! :(");
}
});
Run Code Online (Sandbox Code Playgroud)
将其放在/hooks/after_plugin_install/插件的目录中并在标签之间添加以下行<platform name="android"> ... </platform>:
<hook type="after_plugin_install" src="hooks/after_plugin_install/hook-add-r-import.js" />
Run Code Online (Sandbox Code Playgroud)
每当有人使用cordova plugin add命令添加插件并将导入R.java写入包声明下方时,都会执行该代码