Android、Gradle和插件

3

我正在尝试将OneSignal添加到我的Gradle项目中。

然而,我遇到了以下错误:

build file '/tmp/workspace/myapp-develop/My_app/android/app/build.gradle': 196: only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

我的gradle应用的build.gradle文件如下:

apply plugin: "com.android.application"

import com.android.build.OutputFile


project.ext.react = [
    entryFile: "index.js"
]



apply from: "../../node_modules/react-native/react.gradle"


def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

configurations.all {
  resolutionStrategy.force 'com.google.android.gms:play-services-gcm:11.8.0'
  resolutionStrategy.force 'com.google.android.gms:play-services-analytics:11.8.0'
  resolutionStrategy.force 'com.google.android.gms:play-services-location:11.8.0'
  resolutionStrategy.force 'com.facebook.android:facebook-android-sdk:4.18.0'
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        applicationId "my.android.mobileapp"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 24
        versionName "0.0.24"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        manifestPlaceholders = [
            'onesignal_app_id': "xxxxx-xxxxx-xxx-xxxxx",
            'onesignal_google_project_number': "12345676789"
        ]
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    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]
            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
            }
        }
    }
}

dependencies {
    compile project(':react-native-fabric')
    compile project(':react-native-spinkit')
    compile project(':react-native-push-notification')
    compile project(':react-native-google-analytics-bridge')
    compile project(':react-native-svg')
    compile project(':react-native-onesignal')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile "com.facebook.react:react-native:+"  // From node_modules
    compile project(':react-native-fabric')
    compile('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
      transitive = true;
    }
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

buildscript {
  repositories {
    maven { url 'https://maven.fabric.io/public' }
  }    
      dependencies {
changelog:
        // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
        classpath 'io.fabric.tools:gradle:1.+'
      }
    }


apply plugin: 'io.fabric'

plugins {
    id 'com.onesignal.androidsdk.onesignal-gradle-plugin' version '0.10.2' apply false 
}

repositories {
  maven { url 'https://maven.fabric.io/public' }
}

我已经尝试过以下方法: 但这两种方法都没有起作用。
请问我应该在哪里添加plugin语句? 同时,my_app/android/app/build.gradle是正确的文件吗?
1个回答

1
如果您使用plugins脚本块,则需要将其作为build.gradle文件中的第一个块。
唯一的例外情况是其他pluginsbuildScript块。

然后放在顶部:

buildscript {
   repositories {
    maven { url 'https://maven.fabric.io/public' }
   }    
   dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
   }
}

plugins {
    id 'com.onesignal.androidsdk.onesignal-gradle-plugin' version '0.10.2' apply false 
}

apply plugin: 'io.fabric'
apply plugin: 'com.android.application'

//...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接