ReactNative进阶(九)build

一个project里面可以有多个modulemodule可以是application,也可以是library

在这里插入图片描述
RN项目中,关于gradle的文件目录列表如下:

在这里插入图片描述

Gradle scripts下面主要是工程的编译配置文件,主要有:

  • Bulid.gradle 该文件分为项目级和模块级两种,用于描述App工程的编译规则。
  • Proguard-rules.pro 该文件用于描述java代码的混淆规则。
  • Gradle.properties,该文件用于配置编译工程的命令行参数,一般无须改动。
  • Settings.gradle, 该文件主要配置了需要编译哪些模块。初始内容为include ‘:app’,表示只编译app模块。
  • Local.properties,项目的本地配置文件,在工程编译时自动生成,用于描述开发者电脑的环境配置,包括SDK、NDK的本地路径等。
  • Gradle中的编译语言为groovy

由文件结构可知,当我们创建一个Android项目时会包含两个Android build.gradle配置详解文件:Projectbuild.gradle文件和Modulebuild.gradle文件。

二、Project 的 build.gradle 文件

对应的build.gradle代码如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
// //这里是gradle脚本执行所需依赖,分别是对应的maven库和插件
buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 19
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
        kotlinVersion = '1.3.61'
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath('com.android.tools.build:gradle:3.4.1')

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

// 项目本身需要的依赖,比如项目所需的maven库
allprojects {
    repositories {
        mavenCentral()
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node\_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node\_modules/jsc-android/dist")
        }
        maven {
            // All of Detox' artifacts are provided via the npm module
            url "$rootDir/../node\_modules/detox/Detox-android"
        }
        maven { url "https://jitpack.io" }
        google()
        jcenter()
    }
}

def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

allprojects {
    configurations.all {
        resolutionStrategy {
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }
}


  • repositories{}闭包:配置远程仓库。
    该闭包中可声明jcenter()google()的配置,其中jcenter是一个代码托管仓库,上面托管了很多Android开源项目,在这里配置了jcenter后我们可以在项目中方便引用jcenter上的开源项目,从Android Studio3.0后新增了google()配置,可以引用google上的开源项目。
  • dependencies{}闭包:配置构建工具。
    该闭包使用classpath声明了一个Gradle插件,由于Gradle并不只是用来构建Android项目,因此此处引入相关插件来构建Android项目,其中’3.0.0’为该插件的版本号,可以根据最新的版本号来调整。

三、Module 的 build.gradle 文件

从文件内容可以看出,主要分为三大配置节点:apply pluginandroiddependencies。对应的build.gradle代码如下:

// 声明是Android程序,
// com.android.application 表示该模块为应用程序模块,可以直接运行,打包得到的是.apk文件
// com.android.library 表示该模块为库模块,只能作为代码库依附于别的应用程序模块来运行,打包得到的是.aar文件
// 两者区别:前者可以直接运行,后着是依附别的应用程序运行
apply plugin: "com.android.application"

import com.android.build.OutputFile

/\*\*
 \* The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 \* and bundleReleaseJsAndAssets).
 \* These basically call `react-native bundle` with the correct arguments during the Android build
 \* cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 \* bundle directly from the development server. Below you can see all the possible configurations
 \* and their defaults. If you decide to add a configuration block, make sure to add it before the
 \* `apply from: "../../node\_modules/react-native/react.gradle"` line.
 \*
 \* project.ext.react = [
 \* // the name of the generated asset file containing your JS bundle
 \* bundleAssetName: "index.android.bundle",
 \*
 \* // the entry file for bundle generation
 \* entryFile: "index.android.js",
 \*
 \* // https://facebook.github.io/react-native/docs/performance#enable-the-ram-format
 \* bundleCommand: "ram-bundle",
 \*
 \* // whether to bundle JS and assets in debug mode
 \* bundleInDebug: false,
 \*
 \* // whether to bundle JS and assets in release mode
 \* bundleInRelease: true,
 \*
 \* // whether to bundle JS and assets in another build variant (if configured).
 \* // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 \* // The configuration property can be in the following formats
 \* // 'bundleIn${productFlavor}${buildType}'
 \* // 'bundleIn${buildType}'
 \* // bundleInFreeDebug: true,
 \* // bundleInPaidRelease: true,
 \* // bundleInBeta: true,
 \*
 \* // whether to disable dev mode in custom build variants (by default only disabled in release)
 \* // for example: to disable dev mode in the staging build type (if configured)
 \* devDisabledInStaging: true,
 \* // The configuration property can be in the following formats
 \* // 'devDisabledIn${productFlavor}${buildType}'
 \* // 'devDisabledIn${buildType}'
 \*
 \* // the root of your project, i.e. where "package.json" lives
 \* root: "../../",
 \*
 \* // where to put the JS bundle asset in debug mode
 \* jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 \*
 \* // where to put the JS bundle asset in release mode
 \* jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 \*
 \* // where to put drawable resources / React Native assets, e.g. the ones you use via
 \* // require('./image.png')), in debug mode
 \* resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 \*
 \* // where to put drawable resources / React Native assets, e.g. the ones you use via
 \* // require('./image.png')), in release mode
 \* resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 \*
 \* // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 \* // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 \* // date; if you have any other folders that you want to ignore for performance reasons (gradle
 \* // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 \* // for example, you might want to remove it from here.
 \* inputExcludes: ["android/\*\*", "ios/\*\*"],
 \*
 \* // override which node gets called and with what additional arguments
 \* nodeExecutableAndArgs: ["node"],
 \*
 \* // supply additional arguments to the packager
 \* extraPackagerArgs: []
 \* ]
 \*/

project.ext.react = [
    entryFile: "index.js",
    enableHermes: false,  // clean and rebuild if changing
    bundleAssetName: "index.android.bundle",
    bundleInDebug: true,
    bundleInBeta: true,
    nodeExecutableAndArgs: ["/usr/local/bin/node"]
]

apply from: "../../node\_modules/react-native/react.gradle"

/\*\*
 \* Set this to true to create two separate APKs instead of one:
 \* - An APK that only works on ARM devices
 \* - An APK that only works on x86 devices
 \* The advantage is the size of the APK is reduced by about 4MB.
 \* Upload all the APKs to the Play Store and people will download
 \* the correct one based on the CPU architecture of their device.
 \*/
def enableSeparateBuildPerCPUArchitecture = false

/\*\*
 \* Run Proguard to shrink the Java bytecode in release builds.
 \*/
def enableProguardInReleaseBuilds = false

/\*\*
 \* The preferred build flavor of JavaScriptCore.
 \*
 \* For example, to use the international variant, you can use:
 \* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
 \*
 \* The international variant includes ICU i18n library and necessary data
 \* allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
 \* give correct results when using with locales other than en-US. Note that
 \* this variant is about 6MiB larger per architecture than default.
 \*/
def jscFlavor = 'org.webkit:android-jsc:+'

/\*\*
 \* Whether to enable the Hermes VM.
 \*
 \* This should be set on project.ext.react and mirrored here. If it is not set
 \* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
 \* and the benefits of using Hermes will therefore be sharply reduced.
 \*/
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    // 指定编译用的SDK版本号。比如30表示Android11.0编译
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // 指定该模块的应用编号,也就是app的包名 应用编号需要和AndroidManifest中package相同
        applicationId "com.shq5785"
        // 指定App适合运行的最小SDK版本号。
        minSdkVersion rootProject.ext.minSdkVersion
        /\*\*
 \* 指定目标设备的SDK版本号,表示在该目标版本上已经做过充分测试,系统会为该应用启动一些对应该目标系统的最新功能特性,
 \* Android系统平台的行为变更,只有targetSdkVersion的属性值被设置为大于或等于该系统平台的API版本时,才会生效。
 \* 例如,若指定targetSdkVersion值为22,则表示该程序最高只在Android5.1版本上做过充分测试,
 \* 在Android6.0系统上(对应targetSdkVersion为23)拥有的新特性如系统运行时权限等功能就不会被启用。
 \*/
        targetSdkVersion rootProject.ext.targetSdkVersion
        // 指定App的应用版本号,一般每次打包上线时该值只能增加,打包后看不见。
        versionCode 16872701
        // 指定App的应用版本名称,展示在应用市场上。
        versionName "2.2.6"
        multiDexEnabled true
        testBuildType System.getProperty('testBuildType', 'debug')
        // 表明要使用AndroidJUnitRunner进行单元测试。
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
        ndk {
            //设置支持的SO库架构
            abiFilters "armeabi", "armeabi-v7a", "x86\_64" //, "arm64-v8a"
        }
         missingDimensionStrategy 'react-native-camera', 'general'
    }
// 配置目录指向
// sourceSets {
// main.jniLibs.srcDirs = ['libs']
// }

    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK , "x86\_64" "arm64-v8a",
            include "armeabi-v7a", "x86",  "x86\_64"
        }
    }
    // 自动化打包配置
    signingConfigs {
// debug {
// storeFile file('debug.keystore')
// storePassword 'android'
// keyAlias 'androiddebugkey'
// keyPassword 'android'
// }
        // 开发环境
        debug {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值