找不到 Gradle DSL 方法:'implementation()' (Android React Native)

Posted

技术标签:

【中文标题】找不到 Gradle DSL 方法:\'implementation()\' (Android React Native)【英文标题】:Gradle DSL method not found: 'implementation()' (Android React Native)找不到 Gradle DSL 方法:'implementation()' (Android React Native) 【发布时间】:2020-04-16 22:39:35 【问题描述】:

不确定我做错了什么,但我的项目不会成功构建。这是我的*** settings.gradle 文件:

apply from: '../node_modules/react-native-unimodules/gradle.groovy' 
includeUnimodulesProjects()

rootProject.name = 'BlueHomeLab'
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, './node_modules/react-native-vector-icons/android')


include ':app'

我的*** build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

apply plugin: "com.android.application"
buildscript 
    ext 
        buildToolsVersion = "28.0.3"
        minSdkVersion = 21
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    
    repositories 
        google()
        jcenter()
        maven 
            url "https://maven.google.com"
        

    
    dependencies 


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    


allprojects 
    buildscript 
    repositories 
        google()
        jcenter()
    
   


    repositories 
        mavenLocal()
        google()
        jcenter()
        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")
        
    



wrapper 
    gradleVersion = '5.4.1'
    distributionUrl = distributionUrl.replace("bin", "all")

和我的应用级 build.gradle 文件:

implementation "com.facebook.react:react-native:0.61.4"
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",
 *
 *   // 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
]

apply from: '../../node_modules/react-native-unimodules/gradle.groovy'
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

android 

    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig 

        applicationId "com.bluehomelab"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        compileSdkVersion rootProject.ext.compileSdkVersion
        versionCode 1
        versionName "1.0"
    
    splits 
        abi 
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        
    
    buildTypes 
        release 
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        
    
    // applicationVariants are e.g. debug, release
    applicationVariants.all  variant ->
        variant.outputs.each  output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null)   // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            
        
    
    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    


def jscFlavor = 'org.webkit:android-jsc:+'

def enableHermes = project.ext.react.get("enableHermes", false);

dependencies 
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibVersion"
    implementation 'com.facebook.react:react-native'  // From node_modules
    addUnimodulesDependencies()

    if (enableHermes) 
        def hermesPath = "../../node_modules/hermes-engine/android/"
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
     else 
        implementation jscFlavor
    


// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) 
    from configurations.compile
    into 'libs'


buildscript 
    dependencies 
        classpath 'com.android.tools.build:gradle:3.5.3'
    
    repositories 
        google()
    

apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'application'
apply plugin: 'project-report'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'application'

运行 npx react-native 信息:

System:
    OS: Windows 10 10.0.18362
    CPU: (4) x64 Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
    Memory: 9.30 GB / 15.90 GB
  Binaries:
    Node: 13.2.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.13.1 - C:\Program Files\nodejs\npm.CMD
  IDEs:
    Android Studio: Version  3.5.0.0 AI-191.8026.42.35.6010548
  npmPackages:
    react: ~16.9.0 => 16.9.0
    react-native: ^0.61.5 => 0.61.5

和 gradle -v:

------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------

Build time:   2019-04-26 08:14:42 UTC
Revision:     261d171646b36a6a28d5a19a69676cd098a4c19d

Kotlin:       1.3.21
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          13 (Oracle Corporation 13+33)
OS:           Windows 10 10.0 amd64

这几天我一直在搞砸!如果您有解决方案,请告诉我。

【问题讨论】:

【参考方案1】:

应用级build.gradle 文件中的第一行定义了一个依赖项:

implementation "com.facebook.react:react-native:0.61.4"

=> 应该将它移到专用的dependencies 块中

【讨论】:

以上是关于找不到 Gradle DSL 方法:'implementation()' (Android React Native)的主要内容,如果未能解决你的问题,请参考以下文章

错误:(26, 0) 找不到 Gradle DSL 方法:'runProguard()'

找不到 Gradle DSL 方法

找不到 Gradle DSL 方法:'implementation()' (Android React Native)

更新应用程序版本时找不到 Gradle DSL 方法

找不到对象上的参数 [project ':model'] 的方法 api()

Jenkins 管道错误:java.lang.NoSuchMethodError:找不到这样的 DSL 方法“withSonarQubeEnv”