Cordova插件,自动根据包名替换R资源描述

Posted zhangjianying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cordova插件,自动根据包名替换R资源描述相关的知识,希望对你有一定的参考价值。

Cordova插件,自动根据包名替换R资源描述

#!/usr/bin/env node

module.exports = function (context) 
    var path         = context.requireCordovaModule('path'),
        fs           = context.requireCordovaModule('fs'),
        shell        = context.requireCordovaModule('shelljs'),
        projectRoot  = context.opts.projectRoot,
        ConfigParser = context.requireCordovaModule('cordova-common').ConfigParser,
        config       = new ConfigParser(path.join(context.opts.projectRoot, "config.xml")),
        packageName = config.android_packageName() || config.packageName();

    if (!packageName) 
        console.error("Package name could not be found!");
        return ;
    

    if (context.opts.cordova.platforms.indexOf("android") === -1) 
        console.info("Android platform has not been added.");
        return ;
    

    var targetDir  = path.join(projectRoot, "platforms", "android", "src", "com", "jieweifu", "plugins", "barcode");
	var targetFiles = ["CaptureActivity.java", "decode/DecodeHandler.java", "decode/CaptureActivityHandler.java"];
	
    if (['after_plugin_add', 'after_plugin_install', 'after_platform_add'].indexOf(context.hook) === -1) 
        try 
			if(context.opts.plugins && context.opts.plugins.indexOf(context.opts.plugin.id) !== -1)
				targetFiles.forEach(function(file)
					var targetFile = path.join(targetDir, file);
					fs.unlinkSync(targetFile);
				);
			
         catch (err) 
     else 
		targetFiles.forEach(function(file)
			var targetFile = path.join(targetDir, file);
			fs.readFile(targetFile, encoding: 'utf-8', function (err, data) 
				if (err) 
					throw err;
				
				data = data.replace(/^import __ANDROID_PACKAGE__.R;/m, 'import ' + packageName + '.R;');
				fs.writeFileSync(targetFile, data);
			);
		);
    
;


以上是关于Cordova插件,自动根据包名替换R资源描述的主要内容,如果未能解决你的问题,请参考以下文章

如何让 Cordova 通过插件自动嵌入自定义 iOS 框架?

如何在推送插件 PhoneGap/Cordova 4.0 中替换 window.plugin(已弃用)?

Cordova 自动更新应用程序文件夹

xml命名空间声明:自动替换包名

xml命名空间声明:自动替换包名

Android Cordova 插件开发之创建项目