Home > Software design >  Android Studio showing error in build.gradle file after adding keystore
Android Studio showing error in build.gradle file after adding keystore

Time:08-06

Basically, I was adding keystore files to implement google authentication in my android app and after adding it and making keystore.properties file, the build.gradle:app file started showing some errors during the gradle sync. Following is the code for build.gradle:app file


          def localProperties = new Properties()
          def localPropertiesFile = rootProject.file('local.properties')
           if (localPropertiesFile.exists()) {
               localPropertiesFile.withReader('UTF-8') { reader ->
               localProperties.load(reader)
          }
        }

       def flutterRoot = localProperties.getProperty('flutter.sdk')
      if (flutterRoot == null) {
              throw new FileNotFoundException("Flutter SDK not found. Define location with 
           flutter.sdk in the local.properties file.")
          }

         def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
           if (flutterVersionCode == null) {
             flutterVersionCode = '1'
           }

            def flutterVersionName = localProperties.getProperty('flutter.versionName')
               if (flutterVersionName == null) {
                    flutterVersionName = '1.0'
               }

                apply plugin: 'com.android.application'
                apply plugin: 'kotlin-android'
                apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

             def keystoreProperties = new Properties()
             def keystorePropertiesFile = rootProject.file('key.properties')
                 if (keystorePropertiesFile.exists()) {
                 keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

    android {
        signingConfigs {
            debug {
                storeFile file('~\\user\\keystores\\upload-keystore.jks')
                keyAlias 'upload'
                storePassword <pass>
                keyPassword <pass>
            }
        }
        compileSdkVersion 32


        sourceSets {
            main.java.srcDirs  = 'src/main/kotlin'
        }

        defaultConfig {
            // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
            applicationId "com.example.flutter_application"
            minSdkVersion 16
            targetSdkVersion 32
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
        }


        buildTypes {
            release {
                // TODO: Add your own signing config for the release build.
                // Signing with the debug keys for now, so `flutter run --release` works.
                signingConfig signingConfigs.debug
            }
        }
    }

    flutter {
        source '../..'
    }

     }

And these are the errors that are being shown

errors

What can be done to resolve all these errors

CodePudding user response:

You can use this format for build.gradle in android/app/build.gradle

def localProperties = new Properties() 
 def localPropertiesFile = rootProject.file('local.properties') 
 if (localPropertiesFile.exists()) { 
     localPropertiesFile.withReader('UTF-8') { reader -> 
         localProperties.load(reader) 
     } 
 } 
  
 def flutterRoot = localProperties.getProperty('flutter.sdk') 
 if (flutterRoot == null) { 
     throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") 
 } 
  
 def flutterVersionCode = localProperties.getProperty('flutter.versionCode') 
 if (flutterVersionCode == null) { 
     flutterVersionCode = '1' 
 } 
  
 def flutterVersionName = localProperties.getProperty('flutter.versionName') 
 if (flutterVersionName == null) { 
     flutterVersionName = '1.0' 
 } 
  
 apply plugin: 'com.android.application' 
 apply plugin: 'com.google.gms.google-services' 
 apply plugin: 'kotlin-android' 
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" 
  
 def keystoreProperties = new Properties() 
 def keystorePropertiesFile = rootProject.file('key.properties') 
 if (keystorePropertiesFile.exists()) { 
     keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) 
 } 
  
 android { 
     compileSdkVersion 31 
  
     sourceSets { 
         main.java.srcDirs  = 'src/main/kotlin' 
     } 
     lintOptions { 
  
         disable 'InvalidPackage' 
     } 
     defaultConfig { 
         // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). 
         applicationId "your application package id here" 
         minSdkVersion 21 
         targetSdkVersion 31
         versionCode flutterVersionCode.toInteger() 
         versionName flutterVersionName 
     } 
  
     signingConfigs { 
         release { 
             keyAlias keystoreProperties['keyAlias'] 
             keyPassword keystoreProperties['keyPassword'] 
             storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null 
             storePassword keystoreProperties['storePassword'] 
         } 
     } 
     buildTypes { 
         release { 
             signingConfig signingConfigs.release 
         } 
     } 
 } 
  
 flutter { 
     source '../..' 
 } 
  
 dependencies { 
     implementation 'com.google.android.material:material:1.4.0' 
     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 

 }

Don't forget to replace your package id

  • Related