react native android打包

第一步:react-native bundle --entry-file index.js --platform android --dev false --bundle-output ./android/app/src/main/assets/index.android.bundle --assets-dest ./android/app/src/main/res/
第二步:
error: uncompiled PNG file passed as argument. Must be compiled first into .flat file..
error: failed parsing overlays.
解决办法:在你android文件夹gradle.properties文件里加上这句话:android.enableAapt2=false

第三步:

android studio 3.0之后,Gradle版本也同样升级了,但是升级之后打包出现node_modules_reactnavigation_src_views_assets_backicon.png图片重复问题,这个原因我在Github上面找了好久,才发现是因为Gradle2.3之后,离线打包的路径都会在drawable-xxx-v4中,原版的离线路径在drawable-xxx中,所以导致图片重复问题,怎么解决这个问题呢:

1,修改assetPathUtils.js

assetPathUtils.js文件路径:node_modules\react-native\local-cli\bundle\assetPathUtils.js

修改:getAndroidAssetSuffix方法

修改前:

function getAndroidAssetSuffix(scale) {
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';
}
}

修改后:

function getAndroidAssetSuffix(scale) {
switch (scale) {
case 0.75: return 'ldpi-v4';
case 1: return 'mdpi-v4';
case 1.5: return 'hdpi-v4';
case 2: return 'xhdpi-v4';
case 3: return 'xxhdpi-v4';
case 4: return 'xxxhdpi-v4';
}
}

修改完之后 把之前的drawable-xxx文件夹删掉,然后就可以正常打包了

drawable-xxx文件路径:YourProject\android\app\src\main\res

第四步:

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

cd android && ./gradlew assembleRelease

如果还不行就试试增加doLast模块,就在node_modules/react-native/react.gradle文件,位置如下

def currentBundleTask = tasks.create(
            name: "bundle${targetName}JsAndAssets",
            type: Exec) {
            group = "react"
            description = "bundle JS and assets for ${targetName}."

            // Create dirs if they are not there (e.g. the "clean" task just ran)
            doFirst {
                jsBundleDir.deleteDir()
                jsBundleDir.mkdirs()
                resourcesDir.deleteDir()
                resourcesDir.mkdirs()
            }


             doLast {
                def moveFunc = { resSuffix ->
                    File originalDir = file("${resourcesDir}/drawable-${resSuffix}")
                    if (originalDir.exists()) {
                        File destDir = file("${resourcesDir}/drawable-${resSuffix}-v4")
                        ant.move(file: originalDir, tofile: destDir)
                    }
                }
                moveFunc.curry("ldpi").call()
                moveFunc.curry("mdpi").call()
                moveFunc.curry("hdpi").call()
                moveFunc.curry("xhdpi").call()
                moveFunc.curry("xxhdpi").call()
                moveFunc.curry("xxxhdpi").call()
            }

            // Set up inputs and outputs so gradle can cache the result
            inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
            outputs.dir jsBundleDir
            outputs.dir resourcesDir

你可能感兴趣的:(react native android打包)