From a159a33c02e0c0d7aa245adfd540a066ec065362 Mon Sep 17 00:00:00 2001 From: Bruno Nallis Villanova Date: Mon, 28 Jan 2019 09:06:48 -0800 Subject: [PATCH] =?UTF-8?q?Added:=20informational=20error=20message=20on?= =?UTF-8?q?=20getting=20Android=20drawable=20folder=E2=80=A6=20(#17751)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: … suffix for asset Better informational error message on getting Android drawable folder suffix error using the asset name scale. I've got an not well described error when trying to bundle my React Native project package. You can test the React Native bundle command like this: node node_modules/react-native/local-cli/cli.js bundle --platform android --dev false --reset-cache --entry-file index.android.js --bundle-output /project/android/app/build/intermediates/assets/release/index.android.bundle --assets-dest /project/android/app/build (If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/react-native-website, and link to your PR here.) Pull Request resolved: https://github.com/facebook/react-native/pull/17751 Differential Revision: D13840597 Pulled By: cpojer fbshipit-source-id: f755ef665b76ce3dd9c96e575fbc71e9aaf43a44 --- Libraries/Image/assetPathUtils.js | 35 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Libraries/Image/assetPathUtils.js b/Libraries/Image/assetPathUtils.js index 0b49343518..402680cef7 100644 --- a/Libraries/Image/assetPathUtils.js +++ b/Libraries/Image/assetPathUtils.js @@ -12,26 +12,25 @@ import type {PackagerAsset} from './AssetRegistry'; +const androidScaleSuffix = { + '0.75': 'ldpi', + '1': 'mdpi', + '1.5': 'hdpi', + '2': 'xhdpi', + '3': 'xxhdpi', + '4': 'xxxhdpi', +}; + /** * FIXME: using number to represent discrete scale numbers is fragile in essence because of * floating point numbers imprecision. */ function getAndroidAssetSuffix(scale: number): string { - switch (scale) { - case 0.75: - return 'ldpi'; - case 1: - return 'mdpi'; - case 1.5: - return 'hdpi'; - case 2: - return 'xhdpi'; - case 3: - return 'xxhdpi'; - case 4: - return 'xxxhdpi'; + if (scale.toString() in androidScaleSuffix) { + return androidScaleSuffix[scale.toString()]; } - throw new Error('no such scale'); + + throw new Error('no such scale ' + scale.toString()); } // See https://developer.android.com/guide/topics/resources/drawable-resource.html @@ -52,8 +51,12 @@ function getAndroidResourceFolderName(asset: PackagerAsset, scale: number) { var suffix = getAndroidAssetSuffix(scale); if (!suffix) { throw new Error( - "Don't know which android drawable suffix to use for asset: " + - JSON.stringify(asset), + "Don't know which android drawable suffix to use for scale: " + + scale + + '\nAsset: ' + + JSON.stringify(asset, null, '\t') + + '\nPossible scales are:' + + JSON.stringify(androidScaleSuffix, null, '\t'), ); } const androidFolder = 'drawable-' + suffix;