[RN Android]多渠道打包,如何配置gradle?



  • 加入多渠道sdk后(如友盟),默认react.gradle配置下会报错0_1451980019563_q1.png
    使用了别人的配置 https://gist.github.com/AndrewJack/3b4f3a7b5f1102abd6c4
    可以打包了,但是一些资源(如assets)还是没有一并打进包中,有这方面经验的大神吗?表示对gradle不通



  • 最近比较忘,都快忘了自己在社区发过贴,竟然没人回答,自己就送出解决后的react.gradle文件内容
    有兴趣的同学可以去看看gradle语法

    //下面全是

    import org.apache.tools.ant.taskdefs.condition.Os

    def config = project.hasProperty("react") ? project.react : [];

    def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
    def entryFile = config.entryFile ?: "index.android.js"

    // because elvis operator
    def elvisFile(thing) {
    return thing ? file(thing) : null;
    }

    def reactRoot = elvisFile(config.root) ?: file("../../")
    def inputExcludes = config.inputExcludes ?: ["android/", "ios/"]

    gradle.projectsEvaluated {
    // Grab all build types and product flavors
    def buildTypes = android.buildTypes.collect { type -> type.name }
    def productFlavors = android.productFlavors.collect { flavor -> flavor.name }

    // When no product flavors defined, use empty
    if (!productFlavors) productFlavors.add('')
    
    productFlavors.each { productFlavorName ->
        buildTypes.each { buildTypeName ->
            def sourceName = "${buildTypeName}"
            if (productFlavorName) {
                sourceName = "${productFlavorName}${sourceName.capitalize()}"
            }
            def targetName = "${sourceName.capitalize()}"
    
            // Tasks
            def mergeResourcesTaskName = "merge${sourceName.capitalize()}Resources"
            def mergeAssetsTaskName = "merge${sourceName.capitalize()}Assets"
            def processResourcesTaskName = "process${sourceName.capitalize()}Resources"
    
            def bundleJsAndAssetsTaskName = "bundle${sourceName.capitalize()}JsAndAssets"
    
            // React
            def jsBundleDirConfigName = "jsBundleDir${targetName}"
            def jsBundleDir = elvisFile(config."$jsBundleDirConfigName") ?:
                    file("$buildDir/intermediates/assets/${productFlavorName}/${buildTypeName}")
            def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
    
            def resourcesDirConfigName = "jsBundleDir${targetName}"
            def resourcesDir = elvisFile(config."${resourcesDirConfigName}") ?:
                    file("$buildDir/intermediates/res/merged/${productFlavorName}/${buildTypeName}")
    
    
            tasks.create(
                    name: "$bundleJsAndAssetsTaskName",
                    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.mkdirs()
                    resourcesDir.mkdirs()
                }
    
                // set up inputs and outputs so gradle can cache the result
                inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
                outputs.dir jsBundleDir
                outputs.dir resourcesDir
    
                // set up the call to the react-native cli
                workingDir reactRoot
                if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                    commandLine "cmd","/c", "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file",
                            entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir
                } else {
                    commandLine "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file",
                            entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir
                }
    
                def bundleInVariantName = "bundleIn${targetName}"
                enabled config."$bundleInVariantName" ?: true
            }
    
            // hook bundle<Variant>JsAndAssets into the android build process
            tasks["$bundleJsAndAssetsTaskName"].dependsOn("$mergeResourcesTaskName")
            tasks["$bundleJsAndAssetsTaskName"].dependsOn("$mergeAssetsTaskName")
            tasks["$processResourcesTaskName"].dependsOn("$bundleJsAndAssetsTaskName")
        }
    }
    

    }



  • 嗯,多谢。多渠道打包的配置还是挺重要的。


登录后回复