4

If i tap the Debug Icon, Android Studio sometimes automatically creates new BuildConfig files and then complains that it should be only one named Buildconfig.java...

enter image description here

A Clean Project fixes it but it is so annoying that there must be a reliable solution.

Thanks in advance

EDIT:

My build.gradle file:

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

buildscript {
    ext.kotlin_version = '1.3.72'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

My build.gradle(Module: app):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "de.my.app.id"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

// To inline the bytecode built with JVM target 1.8 into
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.github.siyamed:android-shape-imageview:0.9.3@aar'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.github.RobertApikyan:SegmentedControl:1.2.0'
    implementation 'androidx.viewpager2:viewpager2:1.0.0'
    implementation 'com.h6ah4i.android.materialshadowninepatch:materialshadowninepatch:1.0.0'
}

EDIT: I get this error warning in my console when i build & debug:

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.2/userguide/command_line_interface.html#sec:command_line_warnings

SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
  • can't understand – IntelliJ Amiya May 06 '20 at 13:42
  • @IntelliJAmiya what concrete? If i want to debug my app, android studio complains about the BuildConfig file. I haven't touched it. It creates new one itself and complains then that it should be only one – SwiftiSwift May 06 '20 at 13:44
  • using `buildConfigField` ? Check this https://stackoverflow.com/questions/59438157/buildconfig-is-public-should-be-declared-in-a-file-named-buildconfig-java – IntelliJ Amiya May 06 '20 at 13:48
  • @IntelliJAmiya This doesn't help. On every new project this happens – SwiftiSwift May 06 '20 at 13:55
  • There's something in your environment that's causing this, or locking the `BuildConfig.java` file so that it can't be overwritten and a `BuildConfig 3` ends up being generated. Regardless of debugging/building to deploy on a device/emulator. It can't be just when you want to debug, so that piece of info is most probably irrelevant. – Shark May 11 '20 at 15:18
  • 2
    can you show your build gradle files? In case there is some misconfiguration there that causes multiple BuildConfigs to be generated? `BuildConfig 3` shouldn't be generated in normal builds. – auspicious99 May 13 '20 at 10:38
  • @auspicious99 check the question again please – SwiftiSwift May 14 '20 at 11:41
  • @Shark check the question again please – SwiftiSwift May 14 '20 at 11:41
  • Nothing out of the ordinary there, so the answer is not in the `build.gradle` files, but the environment. – Shark May 14 '20 at 11:51
  • agreed, at least we can rule out a problem in `build.gradle` – auspicious99 May 14 '20 at 12:13
  • so what can i do then? @Shark – SwiftiSwift May 14 '20 at 14:59
  • if using Windows, kill the `javac` process from time to time since it's known to "lock" files like this - although it never happened to me that BuildConfig gets locked. try using a newer JVM. try out different things and when you find a satisfactory solution - post it as an answer. – Shark May 14 '20 at 15:21
  • @Shark nothing seems to help... I have a mac. Only a clean project helps. This BuildConfig issue also appears to my brothers mac... This can't be my issue only – SwiftiSwift May 14 '20 at 17:24
  • does this answer your question? https://stackoverflow.com/questions/58365295/android-studio-duplicate-generated-files (use https://www. before .jitpack.io)) – Dmitri May 14 '20 at 20:21
  • @Dmitri it doesnt help – SwiftiSwift May 17 '20 at 10:51

1 Answers1

1

Try following ways in order of listing:

  • Select Make Project from Build menu.
  • Go to File menu and select Invalidate Cache/restart.
  • Change the distributionUrl in gradle-wrapper.properties to some other versions (higher one preferred).
    distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
  • Update Android studio.
Sonu Sourav
  • 2,926
  • 2
  • 12
  • 25