Permalink
Browse files

Android: put all non-drawable resources to `res/raw`

Summary: When we built packager asset system we were mostly concerned about images. However, this system can also be used to work with videos, animations and other binary resources. The code that sorts assets into Android resource folders currently just shoves all non-drawable resources under `drawable-mdpi`, which is not ideal. Talking to Android experts on the team, `raw` seems like a much better place for other resources.

Reviewed By: jeanlauliac

Differential Revision: D6026633

fbshipit-source-id: cc2199f60da411ea432972a02f52c459ff5c490a
  • Loading branch information...
frantic authored and facebook-github-bot committed Oct 11, 2017
1 parent 1f49801 commit cc86d12175cad69526b35211c5fdb24d23148e93
@@ -42,7 +42,7 @@ function getScaledAssetPath(asset): string {
*/
function getAssetPathInDrawableFolder(asset): string {
var scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get());
var drawbleFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale);
var drawbleFolder = assetPathUtils.getAndroidResourceFolderName(asset, scale);
var fileName = assetPathUtils.getAndroidResourceIdentifier(asset);
return drawbleFolder + '/' + fileName + '.' + asset.type;
}
@@ -58,4 +58,16 @@ describe('getAssetDestPathAndroid', () => {
getAssetDestPathAndroid(asset, 1).startsWith('assets_')
).toBeFalsy();
});
it('should put non-drawable resources to `raw/`', () => {
const asset = {
name: 'video',
type: 'mp4',
httpServerLocation: '/assets/app/test',
};
expect(getAssetDestPathAndroid(asset, 1)).toBe(
'raw/app_test_video.mp4'
);
});
});
@@ -29,7 +29,21 @@ function getAndroidAssetSuffix(scale: number): string {
throw new Error('no such scale');
}
function getAndroidDrawableFolderName(asset: PackagerAsset, scale: number) {
// See https://developer.android.com/guide/topics/resources/drawable-resource.html
const drawableFileTypes = new Set([
'gif',
'jpeg',
'jpg',
'png',
'svg',
'webp',
'xml',
]);
function getAndroidResourceFolderName(asset: PackagerAsset, scale: number) {
if (!drawableFileTypes.has(asset.type)) {
return 'raw';
}
var suffix = getAndroidAssetSuffix(scale);
if (!suffix) {
throw new Error(
@@ -60,7 +74,7 @@ function getBasePath(asset: PackagerAsset) {
module.exports = {
getAndroidAssetSuffix: getAndroidAssetSuffix,
getAndroidDrawableFolderName: getAndroidDrawableFolderName,
getAndroidResourceFolderName: getAndroidResourceFolderName,
getAndroidResourceIdentifier: getAndroidResourceIdentifier,
getBasePath: getBasePath
};
@@ -17,7 +17,7 @@ const path = require('path');
import type {PackagerAsset} from '../../Libraries/Image/AssetRegistry';
function getAssetDestPathAndroid(asset: PackagerAsset, scale: number): string {
const androidFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale);
const androidFolder = assetPathUtils.getAndroidResourceFolderName(asset, scale);
const fileName = assetPathUtils.getAndroidResourceIdentifier(asset);
return path.join(androidFolder, fileName + '.' + asset.type);
}

0 comments on commit cc86d12

Please sign in to comment.