【问题标题】:Could not find method compile() for arguments [com.android.support:appcompat-v7:25.0.0]找不到参数 [com.android.support:appcompat-v7:25.0.0] 的方法 compile()
【发布时间】:2020-02-23 22:44:53
【问题描述】:

试图构建一个 react native 项目,但无法编译 appcompat

在 org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 类型的对象上找不到参数 [com.android.support:appcompat-v7:25.0.0] 的方法 compile()。

build.gradle

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

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 21
        compileSdkVersion = 28
        targetSdkVersion = 27
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:support-annotations:25.0.0'
        compile 'com.android.support:design:25.0.0'
    }
}

allprojects {
    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"
            url "$rootDir/../node_modules/expo-camera/android/maven"
        }
    }
}


task wrapper(type: Wrapper) {
    gradleVersion = '4.7'
    distributionUrl = distributionUrl.replace("bin", "all")
}

更新 更改为实现收益

ERROR: Could not find method implementation() for arguments [com.android.support:appcompat-v7:25.0.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

【问题讨论】:

    标签: android react-native android-studio react-native-android


    【解决方案1】:

    使用实现而不是编译。 gradle 文件现在不推荐编译。

    实现使用 反编译。

    testImplementation 使用 反对 testcompile。

    runtimeOnly 使用 反对运行时。

     implementation 'com.android.support:appcompat-v7:25.0.0'
        implementation 'com.android.support:support-annotations:25.0.0'
        implementation 'com.android.support:design:25.0.0'
    

    这些行不会进入 build.gradle(Project:Projectname) 这些行将在 build.gradle(Module:app)

       apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 28
        buildToolsVersion "29.0.2"
        defaultConfig {
            applicationId "com.waltonbd.myapplication"
            minSdkVersion 15
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/raw'] } }
    }
    
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:design:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    

    另一个是 build.gradle(Project:Projectname)。不要进入这里。

    buildscript {
        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
    
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
    
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    【讨论】:

    • 你能解释一下吗?
    • build.gradle文件有2个。一个是for (Project: projectname)。第二个是for (Module.app)。
    【解决方案2】:

    顶级 build.gradle 文件是依赖项的错误位置。它甚至在您的代码中的注释中说明:

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

    您应该将这些行移至另一个 build.gradle 文件(在您的模块中)

    【讨论】:

    • 谢谢,把它们放在哪里,对不起,我是个菜鸟
    • 我正在尝试构建一个 react-native 应用程序,所以我不知道 gradle 是如何工作的
    • @OmarS。到另一个 build.gradle 文件,在 Android 项目中,它位于 app/build.gradle 之类的地方。不了解 React Native
    • 是的,这是 app/build.gradle,我应该创建另一个文件吗,在哪里放置它,如果是,名称应该是什么
    【解决方案3】:

    gradle 中的compile 关键字已被弃用,请改为implementation

    【讨论】:

    • 尝试使用实现代替错误:在 org.gradle.api.internal 类型的对象上找不到参数 [com.android.support:appcompat-v7:25.0.0] 的方法实现()。 artifacts.dsl.dependencies.DefaultDependencyHandler.
    【解决方案4】:

    尝试将compile 替换为implementation

    implementation 'com.android.support:appcompat-v7:25.0.0'
    implementation 'com.android.support:support-annotations:25.0.0'
    implementation 'com.android.support:design:25.0.0'
    

    compile 方法已弃用:https://docs.gradle.org/current/userguide/java_library_plugin.html

    从 Java 插件继承的 compile、testCompile、runtime 和 testRuntime 配置仍然可用,但已弃用。您应该避免使用它们,因为它们只是为了向后兼容而保留的。

    它们现在可能实际上已从您正在使用的 gradle 版本中删除。

    【讨论】:

    • 尝试并出现错误:在 org.gradle.api.internal.artifacts 类型的对象上找不到参数 [com.android.support:appcompat-v7:25.0.0] 的方法 implementation()。 dsl.dependencies.DefaultDependencyHandler.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2017-10-28
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多