Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android, gradle and plugins

Im trying to add onesignal to my gradle project.

However I get the following error:

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

My gradle app build.gradle file looks like:

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' }
}

I have tried:

  • https://stackoverflow.com/a/26240341/265119
  • Putting the plugins declaration at the top of the file

Neither of these has worked.

Where exactly should I add the plugin statement? Also, is the my_app/android/app/build.gradle the correct file to add the plugin statement into?

like image 613
Magick Avatar asked Sep 04 '26 08:09

Magick


1 Answers

If you use the plugins script block you need to put it as first block in the build.gradle file.
The only exceptions from this rule are other plugins or the buildScript block.

Then put at the top:

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'

//...
like image 87
Gabriele Mariotti Avatar answered Sep 05 '26 22:09

Gabriele Mariotti