Android res文件在哪里进入Cordova?

时间:2014-01-23 19:48:57

标签: android cordova

从我读过的内容来看,应该永远不要将平台文件夹添加到版本控制中。但是...... Condova网站上的splashscreen / icon文档声明将图像放在res文件夹中......

那我该怎么处理呢?我想使用merge文件夹,但这只适用于www替换。

任何想法都会很棒。 (如果我添加平台文件夹,它会导致下一个人出现问题,即使我尝试gitignore在编译时创建的文件。

2 个答案:

答案 0 :(得分:3)

我能够弄清楚目前看来最好的做法。我也想为其他人记录这一点。

我为我需要的资源创建了一个文件夹,然后编写了一个自定义钩子来复制文件。以下是我在http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/中使用的一个很好的示例:

#!/usr/bin/env node

// This hook copies various resource files 
// from our version control system directories 
// into the appropriate platform specific location
//


// configure all the files to copy.  
// Key of object is the source file, 
// value is the destination location.  
// It's fine to put all platforms' icons 
// and splash screen files here, even if 
// we don't build for all platforms 
// on each developer's box.

var filestocopy = [{
    "config/android/res/drawable/icon.png": 
    "platforms/android/res/drawable/icon.png"
}, {
    "config/android/res/drawable-hdpi/icon.png": 
    "platforms/android/res/drawable-hdpi/icon.png"
}, {
    "config/android/res/drawable-ldpi/icon.png": 
    "platforms/android/res/drawable-ldpi/icon.png"
}, {
    "config/android/res/drawable-mdpi/icon.png": 
    "platforms/android/res/drawable-mdpi/icon.png"
}, {
    "config/android/res/drawable-xhdpi/icon.png": 
    "platforms/android/res/drawable-xhdpi/icon.png"
}, {
    "config/android/res/drawable/splash.png": 
    "platforms/android/res/drawable/splash.png"
}, {
    "config/android/res/drawable-hdpi/splash.png": 
    "platforms/android/res/drawable-hdpi/splash.png"
}, {
    "config/android/res/drawable-ldpi/splash.png": 
    "platforms/android/res/drawable-ldpi/splash.png"
}, {
    "config/android/res/drawable-mdpi/splash.png": 
    "platforms/android/res/drawable-mdpi/splash.png"
}, {
    "config/android/res/drawable-xhdpi/splash.png": 
    "platforms/android/res/drawable-xhdpi/splash.png"
}, {
    "config/ios/Resources/icons/icon-72.png": 
    "platforms/ios/YourAppName/Resources/icons/icon-72.png"
}, {
    "config/ios/Resources/icons/icon.png": 
    "platforms/ios/YourAppName/Resources/icons/icon.png"
}, {
    "config/ios/Resources/icons/icon@2x.png": 
    "platforms/ios/YourAppName/Resources/icons/icon@2x.png"
}, {
    "config/ios/Resources/icons/icon-72@2x.png": 
    "platforms/ios/YourAppName/Resources/icons/icon-72@2x.png"
}, {
    "config/ios/Resources/splash/Default@2x~iphone.png": 
    "platforms/ios/YourAppName/Resources/splash/Default@2x~iphone.png"
}, {
    "config/ios/Resources/splash/Default-568h@2x~iphone.png": 
    "platforms/ios/YourAppName/Resources/splash/Default-568h@2x~iphone.png"
}, {
    "config/ios/Resources/splash/Default~iphone.png":
    "platforms/ios/YourAppName/Resources/splash/Default~iphone.png"
}, {
    "config/ios/Resources/splash/Default-Portrait~ipad.png": 
     "platforms/ios/YourAppName/Resources/splash/Default-Portrait~ipad.png"
}, {
    "config/ios/Resources/splash/Default-Portrait@2x~ipad.png": 
    "platforms/ios/YourAppName/Resources/splash/Default-Portrait@2x~ipad.png
}, ];

var fs = require('fs');
var path = require('path');

// no need to configure below
var rootdir = process.argv[2];

filestocopy.forEach(function(obj) {
    Object.keys(obj).forEach(function(key) {
        var val = obj[key];
        var srcfile = path.join(rootdir, key);
        var destfile = path.join(rootdir, val);
        //console.log("copying "+srcfile+" to "+destfile);
        var destdir = path.dirname(destfile);
        if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {
            fs.createReadStream(srcfile).pipe(
               fs.createWriteStream(destfile));
        }
    });
});

答案 1 :(得分:0)

我想要注意,现在可以通过config.xml正确地复制启动文件,您只需要项目根目录下的所有图标和启动资源的本地文件夹。但是,如果您的应用程序需要将其他内容复制到res文件夹中,则当前接受的答案是很好的方法。

在我的项目中,项目文件夹的根目录是另一个名为“res”的文件夹。在里面,我有一个"react"文件夹和一个android文件夹,用于组织我需要的所有资产。

然后,您只需引用这样的文件,cordova将自动将它们复制到正确的位置。

ios