9

In project I have several flavors, several buildTypes and several folders for combinations

Build combinations:

enter image description here

Folder structure

enter image description here

In each folder I have custom Application class + AndroidManifest to register this Application class

enter image description here

Sometime, but just sometimes build fails with cannot find symbol error:

\app\src\main\java\ru\my\package\app\environment\MyApp.java:35: error: cannot find symbol
            .core(new CrashlyticsCore.Builder().disabled(!BuildConfig.ANALYTICS).build())

or App class independent but the same error

\app\src\main\java\ru\my\package\presentation\presenter\impl\MainPresenterImpl.java:104: error: cannot find symbol
    if (!BuildConfig.ANALYTICS) return;

But the very next build could be successful, so the possible flow is

  1. Build (successful)
  2. Some chagnes in the code
  3. Build (error)
  4. Build-retry (successful) <-- without any changes in code

Note

  • It's not a sync problem and sync/invalidate cache is not the answer
  • It's not a wrong import problem

Disclaimer: there are questions that looks like this one, but they're different, e.g.

Gradle buildConfigField BuildConfig cannot resolve symbol

UPDATE

Gradle dependencies:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(path: ':OVSALibrary')
    implementation project(path: ':YaMapProject')
    implementation 'com.jakewharton:butterknife:8.8.1'
    implementation 'com.google.dagger:dagger:2.14.1'
    kapt 'com.google.dagger:dagger-compiler:2.14.1'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'eu.davidea:flexible-adapter:5.0.0-rc3'
    kapt 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation "com.android.support:appcompat-v7:$support_lib_version"
    implementation "com.android.support:design:$support_lib_version"
    implementation "com.android.support:support-v4:$support_lib_version"
    implementation "com.android.support:recyclerview-v7:$support_lib_version"
    implementation "com.android.support:support-emoji:$support_lib_version"
    implementation "com.android.support:support-emoji-appcompat:$support_lib_version"
    implementation "com.android.support:support-emoji-bundled:$support_lib_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
    implementation 'ru.egslava:MaskedEditText:1.0.5'
    implementation 'nl.big-o:liqp:0.7.3'
    implementation 'org.jsoup:jsoup:1.7.2'
    implementation 'me.everything:overscroll-decor-android:1.0.4'
    implementation 'com.squareup.picasso:picasso:2.6.0-SNAPSHOT'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile ('com.squareup.retrofit2:converter-simplexml:2.3.0') {
        exclude group: 'stax', module: 'stax-api'
        exclude group: 'stax', module: 'stax'
        exclude group: 'xpp3', module: 'xpp3'
    }
    implementation 'com.makeramen:roundedimageview:2.3.0'
    implementation ('com.github.revern:socket.io-client-java:master-SNAPSHOT') {
        exclude group: 'org.json', module: 'json'
    }
    implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar'
    implementation 'com.luckycatlabs:SunriseSunsetCalculator:1.2'
    implementation ('com.github.developer-shivam:Crescento:1.2.1') {
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'palette-v7'
    }
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar'
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.0@aar') {
        transitive = true;
    }
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

    implementation 'com.malinskiy:superrecyclerview:1.1.4'

    // Yandex.Map things
    implementation 'com.yandex.android:mapkit:3.0.0'

    // Yandex ads. Adfox.
    rcImplementation 'com.yandex.android:mobmetricalib:3.0.0'
    debugWithAnalyticsImplementation 'com.yandex.android:mobmetricalib:3.0.0'
    releaseImplementation 'com.yandex.android:mobmetricalib:3.0.0'
    implementation 'com.yandex.android:mobileads:2.71'


    // Waiting for https://github.com/SundeepK/CompactCalendarView/pull/287
    // Waiting for https://github.com/SundeepK/CompactCalendarView/pull/288
    implementation 'com.github.dryaz:CompactCalendarView:master-SNAPSHOT'

    // Waiting for https://github.com/opacapp/multiline-collapsingtoolbar/pull/57
    implementation 'com.github.dryaz:multiline-collapsingtoolbar:master-SNAPSHOT'

    implementation 'com.github.clans:fab:1.6.4'

    implementation 'org.greenrobot:eventbus:3.1.1'

    testImplementation "org.robolectric:robolectric:3.8"
    testImplementation 'junit:junit:4.12'
    testImplementation "com.android.support.test:monitor:1.0.2"
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // PAY SDK
    implementation(name:'mos-pay-sdk-1.0.9', ext:'aar')
    implementation 'io.card:android-sdk:5.5.1'
    implementation ('eu.livotov.labs.android:3DSView:1.1.2@aar') {transitive=true}

    // Android Architecture Components
    implementation "android.arch.lifecycle:runtime:$androidArch"
    implementation "android.arch.lifecycle:extensions:$androidArch"
    annotationProcessor "android.arch.lifecycle:compiler:$androidArch"

    compile 'com.google.firebase:firebase-core:16.0.0'
    compile 'com.google.firebase:firebase-messaging:17.0.0'
}

** UPDATE ** Here's the list of buildTypes and productFlavors

flavorDimensions("devbeta", "mainmirror")
    productFlavors {
        // Beta API enviroment
        beta {
            dimension = "devbeta"
            buildConfigField "String", "SOCKET_REMOTE_PATH", "\"/api-beta/socket.io\""
        }

        // Dev API enviroment
        dev {
            dimension = "devbeta"
            buildConfigField "String", "SOCKET_REMOTE_PATH", "\"/api-dev/socket.io\""
        }

        // Prod API enviroment
        prod {
            dimension = "devbeta"
            buildConfigField "String", "SOCKET_REMOTE_PATH", "\"/socket.io\""
        }

        mainhost {
            dimension = "mainmirror"
            buildConfigField "String", "CONFIG_REMOTE_HOSTNAME", "\"some.host.url\""
        }

        mirrorhost {
            dimension = "mainmirror"
            buildConfigField "String", "CONFIG_REMOTE_HOSTNAME", "\"some.host.url\""
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            shrinkResources false
            buildConfigField "boolean", "ANALYTICS", "false"
            signingConfig signingConfigs.customDebug
            ext.enableCrashlytics = false
            manifestPlaceholders = [
                    firebaseAnalyticsDeactivated: "true",
                    appIcon: "@mipmap/ic_launcher_debug",
            ]
        }

        debugWithAnalytics.initWith(buildTypes.debug)
        debugWithAnalytics {
            ext.betaDistributionGroupAliases = "group-internal"
            ext.betaDistributionReleaseNotes = "$getReleaseNotes"
            ext.enableCrashlytics = true
            buildConfigField "boolean", "ANALYTICS", "true"
            matchingFallbacks = ['debug', 'release']
        }

        debugWithProguard.initWith(buildTypes.debug)
        debugWithProguard {
            minifyEnabled true
            shrinkResources true
            proguardFiles(file('./proguard').listFiles())
            matchingFallbacks = ['release', 'debug']
        }

        // RC build for inner testers that has analytics, signed with debug key.
        rc {
            minifyEnabled true
            shrinkResources true
            proguardFiles(file('./proguard').listFiles())
            buildConfigField "boolean", "ANALYTICS", "true"
            // Fields for fabric beta distribution
            ext.betaDistributionGroupAliases = "group-internal"
            ext.betaDistributionReleaseNotes = "$getReleaseNotes"

            signingConfig signingConfigs.customDebug

            manifestPlaceholders = [
                    firebaseAnalyticsDeactivated: "false",
                    appIcon: "@mipmap/ic_launcher_rc",
            ]
            matchingFallbacks = ['debug', 'release']
        }

        release.initWith(buildTypes.rc)
        release {
            buildConfigField "boolean", "ANALYTICS", "true"
            ext.betaDistributionGroupAliases = "group-customer"
            signingConfig signingConfigs.release
            manifestPlaceholders = [
                    firebaseAnalyticsDeactivated: "false",
                    appIcon: "@mipmap/ic_launcher",
            ]
        }
    }
dilix
  • 3,761
  • 3
  • 31
  • 55
  • why u added tools:replace="android:name" – Ashvin solanki Aug 27 '18 at 08:41
  • @Ashvinsolanki because I need to use different App classes in different flavors, it's used in order to replace App implementation with flavor's one. – dilix Aug 27 '18 at 08:42
  • can u please add gradle dependency – Ashvin solanki Aug 27 '18 at 08:49
  • @Ashvinsolanki updated my pos – dilix Aug 27 '18 at 10:21
  • @dilix build fails when you switch variant or building same variant after code changes ? – Qasim Aug 27 '18 at 10:42
  • @Qasim buildind the same variant after code changes fails – dilix Aug 27 '18 at 11:55
  • The class icon on the structure screenshot shows an error, can you show the class definition. Is it possible to put these different app classes in different source routes? (can you use a single app class with composition based on flavours, to simplify things?) – Nick Cardoso Aug 29 '18 at 13:23
  • @NickCardoso it showsh the error because current selected build variant is not Rc, it's ok. I can't use one APP class, because I have some dependencies that shouldn't be added in default config but are needed in other configs and they're used in 'APP' class, so I need to have an APP class for each flavor where I have this dependency. – dilix Aug 30 '18 at 09:41
  • That's exactly what I'm saying - use composition for those parts. Then the dependency is on the composite component and the App functions like an interface to them – Nick Cardoso Aug 30 '18 at 10:24
  • I can't even add dependency to the main part of the app because in this case lib will bring a service that lives in other thread and prevent instant run usage. So I use rcImplementation, releaseImplementation etc. but can't use just 'implementation' and in this case I can't import dependencies from the lib in any part of main code. – dilix Aug 30 '18 at 12:40
  • Have you try to disable "instant run"? – ivan.panasiuk Sep 01 '18 at 21:13
  • @ivan.panasiuk yes I do – dilix Sep 02 '18 at 08:42
  • Can you show buildTypes and productFlavors config from build.gradle? – Link182 Sep 02 '18 at 18:05
  • @Link182 you could check it in updated question – dilix Sep 03 '18 at 09:08
  • i think its a problem of resources, you are using firebaseAnalyticsDeactivated in debug mode and their is version mismatch in firebase library compile 'com.google.firebase:firebase-core:16.0.0' compile 'com.google.firebase:firebase-messaging:17.0.0', so my suggestion is : 1) use same version of firebase library in overall project 2) delete build folder of all module 3) And go to the File>Invalidate Caches/Restart>Invalidate and Restart hope it will help you. – Aniruddh Parihar Sep 03 '18 at 11:37

6 Answers6

2

Rebuild Project if you have still problem on importing Package Name and then .BuildConfig

1

Might be complete nonsense, but have you ever tested it with putting a default value directly in your defaultConfig?

defaultConfig {
    buildConfigField "boolean", "ANALYTICS", "false"
}
Norbert
  • 1,204
  • 1
  • 9
  • 13
  • Nice idea but it doesn't help :( – dilix Sep 03 '18 at 12:03
  • Sorry to hear that. In my projects all custom values are mostly in my `productFlavors` instead of in the `buildTypes`, but that won't help either I assume. – Norbert Sep 03 '18 at 14:36
1

Finally looks like I've found a reason, I have the library project exactly with the same package ID as the main app, so the inner BuildConfig for library project doesn't contain variable.

dilix
  • 3,761
  • 3
  • 31
  • 55
  • Another tip: I kept invalidating cache and cleaning project to see if I could import the right BuildConfig, but nothing worked. I even deleted .idea and .gradle folders to see if regenerating them would solve it, but still it wouldn't work. I also reimported the project, nothing. Then I decided to rebuild the project just for the sake of it and it worked. – FabioR Aug 17 '20 at 18:05
  • So what do you do to solve it? – Simran Sharma Feb 28 '23 at 12:59
  • At the first place I could suggest to avoid having the same package IDs for lib and app itself. – dilix Mar 15 '23 at 13:22
1

I also have flavors in my app and I got to know that if I use debugVariant than BuildConfig creates and works well but if I try to use releaseVariant to work on than it can't create BuildConfig file.

Harpreet
  • 2,990
  • 3
  • 38
  • 52
1

I have similar error, i.e. can't resolve BuildConfig.FOO , it's because of some other places has red mark error, e.g. you might unnoticed AndroidManifest.xml contains non-existent service class marked in red color. Fixed the service class first, then rebuild, the red color on BuildConfig.FOO are gone.

The lesson learned was don't focus at fixing specific red color which you don't see anything wrong, instead try look at other file and fix that first.

林果皞
  • 7,539
  • 3
  • 55
  • 70
1

In my case I had another component which included an extra import that interfered with my base BuildConfig file

Look for imports from external libraries or components such as

import com.dpizarro.uipicker.library.BuildConfig;

and just delete them and rebuild.

marius
  • 637
  • 7
  • 11