• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

invertase/react-native-gradle-plugin: A gradle plugin for React Native Android t ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

invertase/react-native-gradle-plugin

开源软件地址(OpenSource Url):

https://github.com/invertase/react-native-gradle-plugin

开源编程语言(OpenSource Language):

Groovy 100.0%

开源软件介绍(OpenSource Introduction):


React Native Gradle Plugin

License Chat Follow on Twitter


A gradle plugin for React Native Android that simplifies and standardises build configurations (including versioning) for both Projects & React Native modules.

Current Features

  • Android app / React Native module auto-versioning using the version defined in your package.json
  • Standardise overriding android/gradle/tools SDK versions of React Native Modules
  • Standardise overriding dependency versions of React Native Modules, e.g. Firebase Android SDKs
  • Built-in inspections (warning logs & suggested solutions) for common build mistakes/errors, e.g. duplicate dex

Installation

Add the plugin to your build.gradle;

Groovy:

plugins {
  id "io.invertase.gradle.build" version "1.3"
}

Kotlin:

plugins {
  id("io.invertase.gradle.build") version "1.3"
}

This must be added after your buildscript block.

Usage

For Projects & React Native Modules

Versioning Overrides

Add a versions configuration to your app build gradle, for example;

project.ext {
  set('react-native', [
    versions: [
      // Android version section can be automatically applied by calling 
      // ReactNative.module.applyAndroidVersions()
      //    or
      // ReactNative.project.applyAndroidVersions()
      // android version section supports legacy behavior e.g.:
      //    project.ext.compileSdkVersion
      android           : [
        minSdk    : 16,
        targetSdk : 28,
        compileSdk: 28,
        // optional as gradle.buildTools comes with one by default
        // buildTools: "28.0.3"
      ],

      googlePlayServices: [
        base: "16.0.1",
        // additional version sub sections as defined by native module authors
        // e.g maps:
        maps: "16.0.1",
      ],

      // additional version sections can be defined by
      // native module authors, e.g. React Native Firebase:
      firebase          : [
        functions: "16.1.3"
      ],
    ],
  ])
}

These additionally form the default versions when used in a react native module.

Example: Using a version

dependencies {
  api project(':@react-native-firebase/app')
  // getVersion() first looks for the overridden version defined in the consumers project
  // if non is found; it falls back to the default version defined in the React Native modules' build.gradle, as above
  implementation "com.google.firebase:firebase-functions:${ReactNative.ext.getVersion("firebase", "functions")}"
  implementation "com.google.android.gms:play-services-base:${ReactNative.ext.getVersion("googlePlayServices", "base")}"
}

Android build versioning using package.json version

Android builds can specify a build code and version name, e.g.:

android {
  defaultConfig {
    versionCode 1
    versionName "1.0"
  }
}

Updating this every time you need to publish your app can be avoided by having it default to using the version defined in your projects package.json file, the process is explained here in-depth by @AndrewJack.

This can be automated with this gradle plugin by adding the following to your app (or RN module) build.gradle:

ReactNative.shared.applyPackageVersion()

Excluding common META-INF files

META-INF files can cause 'duplicate file' build failures if they're not excluded, this plugin allows excluding the most common types of these files by adding the following to your app (or RN module) build.gradle:

ReactNative.shared.applyDefaultExcludes()

For reference this will inject the following config:

android {
  packagingOptions {
    exclude 'META-INF/-no-jdk.kotlin_module'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
  }
}

For React Native Modules

Auto apply Android SDK versions

ReactNative.module.applyAndroidVersions()

This will apply the default versions or the user overridden versions of the android SDK versions specified in react-native -> versions -> android { }.

This removes the need for the following (that most RN modules are currently doing):

android {
  compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
  buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
  defaultConfig {
    minSdkVersion rootProject.hasProperty('minSdkVersion') ? rootProject.minSdkVersion : DEFAULT_MIN_SDK_VERSION
    targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
  }
}

applyAndroidVersions() is backwards compatible with the old format as above and internally checks for these as well as the new format.


Auto locate and add React Native as a dependency

This adds the React Native custom maven repository like normal, but; with support for various scenarios whilst developing your android module locally without an app;

  • automatically finds the right location of node_modules/react-android when opening your React Native Android module standalone in Android Studio
  • mono-repo support for the above, e.g. React Native at the root package as a dev dependency
  • specifying an exact location via options (reactNativeAndroidDir) - useful when testing your module against a fork of React Native located outside of your project or building React Native from sources
// accepts "api" or "implementation" as dependency type arg
ReactNative.module.applyReactNativeDependency("api")

Example: Specify exact location via options:

project.ext {
  set('react-native', [
    // ... other configs e.g. versions
    options: [
      reactNativeAndroidDir: "/../../../../my-react-native-fork/android"
    ]
  ])
}

Planned Features

  • Support for injecting Java constants (buildConfigField) into your app from a JS script, package.json or app.json
  • Support for injecting Android Resources (resValue) into app from a JS script, package.json or app.json

React Native modules using this plugin

Submit a PR to add your React Native module(s) here.

License


Built and maintained with


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
RedPillAnalytics/gradle-confluent发布时间:2022-06-14
下一篇:
zeroc-ice/ice-builder-gradle: Gradle plug-in to automate the compilation of Slic ...发布时间:2022-06-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap