42

I am trying to test my ionic app in android studio. It is throwing the below error.

Gradle sync failed: Cause: compileSdkVersion is not specified.

Any solution for this ? What am I doing wrong.

Here is my build.gradle file

apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
    }
}

// Allow plugins to declare Maven dependencies via build-extras.gradle.

allprojects {
    repositories {
        mavenCentral();
        jcenter()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.1.0'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:+'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:+'
    implementation 'com.android.support:appcompat-v7:27.+'
}
Amit Anand
  • 957
  • 1
  • 16
  • 25
  • Please give compile sdk version.Like in app level gradle. – debo.stackoverflow May 25 '18 at 14:00
  • 1
    please share your gradle file code – Yogesh Borhade May 25 '18 at 14:01
  • 1
    `compileSdkVersion 25 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.pt.planner" minSdkVersion 21 targetSdkVersion 25 versionCode 24 versionName "2.4.0" multiDexEnabled true` – debo.stackoverflow May 25 '18 at 14:02
  • 1
    compilesdkversion should be same as targetsdkversion – debo.stackoverflow May 25 '18 at 14:02
  • 1
    @Yogesh Borhade - Please check, I have shared the file details – Amit Anand May 25 '18 at 14:11
  • "use this code in your gradle file or check below code " android { // compileSdkVersion 26 buildToolsVersion '27.0.3' defaultConfig { applicationId "com.sportden.proquize" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } – Yogesh Borhade May 25 '18 at 14:14
  • @YogeshBorhade - Could you please confirm to which gradle file I should I add this code. I can see multiple gradle files. 1. build.gradle - under android folder 2. build.gradle - under app folder 3. build.gradle - under cordovaLib folder – Amit Anand May 25 '18 at 14:16
  • 1 st way )instead of Project select android and then find GradleScript -> build.gradle -> (module:app) 2 nd way ) app(folder )-> builde.gradle – Yogesh Borhade May 25 '18 at 14:24

13 Answers13

28

You are using android support library of 27.+ so you will have to give sdk version 27 as compileSdkVersion and targetSdkVersion otherwise your project does not know for which platform your project should be built. These parameter should be given in android directory like this in build.gradle(app):

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "com.example.abc.test"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Just paste this code below apply plugin: 'com.android.application' this line

Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17
  • 1
    Thanks Ghulam Moinul Quadir, it works now. But still the build fails and gives an error 1. cannot find symbol class CallbackContext 2. cannot find symbol class CordovaPlugin 3. cannot find symbol class PluginResult 4. cannot find symbol class CordovaPlugin Please help – Amit Anand May 25 '18 at 14:57
  • @AmitAnand share you `build.gradle(ProjectName)` – Ghulam Moinul Quadir May 25 '18 at 15:14
5

In my case , I fixed it by setting the Android gradle plugin version and gradle version to the latest. enter image description here

Murat
  • 3,084
  • 37
  • 55
4

If you are working on a old project and using latest version of Android studio then simply change

compileSdk 27

to

compileSdkVersion 27

on your app level build.gradle file. it will fix this error

3

In my case, I resolved the issue by replacing the old plugin applying syntax:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

with the new plugins DSL in all my build.gradle files:

plugins {
    id "com.android.application"
    id "kotlin-android"
}
Louis CAD
  • 10,965
  • 2
  • 39
  • 58
3

if someone encounter this problem after upgrading android studio to (Flamingo AGP 8), they updated compileSdkVersion field to be compileSdk so you have to update this line in your gradle.build to be like this:

compileSdkVersion 33
Amer Alzibak
  • 1,489
  • 15
  • 16
1

In my case, the error was coming from my own plugin and I fixed it by adding the following line to my plugin.xml file: (which I found simpler than updating the plugin's gradle file)

    <platform name="android">
        <preference name="android-compileSdkVersion" value="30" />
         ...
MIWMIB
  • 1,407
  • 1
  • 14
  • 24
  • It stopped working again. Don't know why cordova is being bit intermittent for me. – MIWMIB Sep 03 '21 at 11:47
  • cordova platform add android was throwing error but build was still running. Updating the plugin's gradle file to `dependencies { implementation "androidx.biometric:biometric:1.1.0" }` filed my issue – MIWMIB Sep 03 '21 at 12:21
1

In my case, with Capacitor and the error "compileSdkVersion is not specified. Please add it to build.gradle", I had to edit the file android/app/build.gradle by adding compileSdk 33 in defaultConfig:

apply plugin: 'com.android.application'

android {
    signingConfigs {
        debug {
            […]
        }
        release {
            […]
        }
    }
    compileSdk compileSdkVersion
    defaultConfig {
        applicationId "[…]"
        minSdk 25
        targetSdk 33
        compileSdk 33
        versionCode 33
        versionName '33'
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        signingConfig signingConfigs.release
    }
    buildTypes {
        release {
            […]
        }
    }
}
AymKdn
  • 3,327
  • 23
  • 27
1

in file variables.gradle change compileSdkVersion and targetSdkVersion this file found in android/variables.gradle

ext {
    minSdkVersion = 22
    compileSdkVersion = 32
    targetSdkVersion = 32
    androidxActivityVersion = '1.4.0'
    androidxAppCompatVersion = '1.4.2'
    androidxCoordinatorLayoutVersion = '1.2.0'
    androidxCoreVersion = '1.8.0'
    androidxFragmentVersion = '1.4.1'
    coreSplashScreenVersion = '1.0.0-rc01'
    androidxWebkitVersion = '1.4.0'
    junitVersion = '4.13.2'
    androidxJunitVersion = '1.1.3'
    androidxEspressoCoreVersion = '3.4.0'
    cordovaAndroidVersion = '10.1.1'
}
0

Please Add Below Line in your gradle file

  compileSdkVersion 26

please check below code for reference

android {
        compileSdkVersion 26
        buildToolsVersion '27.0.3'

        defaultConfig {
            applicationId ""
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }

    }
Yogesh Borhade
  • 694
  • 1
  • 10
  • 24
0

Note: There was an error in my app/build.gradle, hence it was not reading compileSDKVersion property. When I commented such a line, the error went away:

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

Firstly I got to follow this link:

ReactNative: Building from source

Then this:

ReactNative: android-development-environment

Then you can have this added in your settings.gradle file:

//
include ':ReactAndroid'
//
project(':ReactAndroid').projectDir = new File(
    rootProject.projectDir, '../node_modules/react-native/ReactAndroid')

For my ReactViro Sample project I also had to add dependencies from react-native node_modules directory:

    implementation project(':arcore_client') // remove this if AR not required
    implementation project(':gvr_common')
    implementation project(path: ':viro_renderer')
    implementation project(path: ':react_viro')

and

in my settings.gradle:

//
include ':react_viro', ':arcore_client', ':gvr_common', ':viro_renderer'
project(':arcore_client').projectDir = new File('../node_modules/react-viro/android/arcore_client')
project(':gvr_common').projectDir = new File('../node_modules/react-viro/android/gvr_common')
project(':viro_renderer').projectDir = new File('../node_modules/react-viro/android/viro_renderer')
project(':react_viro').projectDir = new File('../node_modules/react-viro/android/react_viro')
//
Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
0

Updating required Package.json libraries worked for me try it out. Most of the error will get resolved.

Remove Jcenter() from build.gradle.

Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52
0

Add this below mention code to your android/build.gradle

  1. First, Save your code and uninstall your Android Studio and install again.

  2. Add this line in your android/build.gradle .

    def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

     allprojects {
         configurations.all {
             resolutionStrategy {
                 // Remove this override in 0.66, as a proper fix is included in react-native itself.
                 force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
             }
         }
    
0

You don't need to specify android.buildToolsVersion from Build Tools For buildToolsVersion in version 27.0.3 or higher there's no need to specify buildToolsVersion.

Build Tools 27.0.3 or higher. Keep in mind, you no longer need to specify a version for the build tools using the android.buildToolsVersion property—the plugin uses the minimum required version by default.

Source (developer.android.com)

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94