43

I've switched to release build variant and configured signingConfigs. Now when I try to check the debug build variant from the drop down menu it switches immediately back to the release build variant. So I'm not able to run my app in debug mode any more.

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

android {
    signingConfigs {
        config {
            ...
        }
    }

    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion

    defaultConfig {
        applicationId "com.kost.foo"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 2
        versionName "1.1"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }
        externalNativeBuild {
            cmake {
               ...
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable true
            signingConfig signingConfigs.config
        }
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        main {
            jniLibs.srcDirs = ['src/main/jni']
        }
    }

    externalNativeBuild {
        cmake {
            path 'src/main/jni/CMakeLists.txt'
        }
    }
}

kapt {
    generateStubs = true
}

repositories {
    maven { url 'https://github.com/linchaolong/stetho-realm/raw/master/maven-repo' }
    mavenCentral()

}

I've tried to revert all changes in build.gradle as it was before configuring, but with no luck.

Any ideas how to fix the issue?

Erik Ghonyan
  • 427
  • 4
  • 12
AlexKost
  • 2,792
  • 4
  • 22
  • 42

17 Answers17

32

I had a similar problem where most of the Build menu items were greyed out. greyed out

'Sync project with Gradle files' didn't fix.

I noticed a 'Build Variants' toggle button on the bottom left of Android Studio (v 3.1.2) and with this was finally able to choose the variant I needed.

Choose variant from here

Maybe this will work for you too.

paws
  • 1,263
  • 15
  • 23
14

Maybe you have got your solution to this, just in case, i provide my solution here.


For Android Studio 2.x

It may because that you compile your dependent project using:

compile project('module_a')

Above setting will force your project to compile the release version of your modules. Just change it to below:

releaseCompile project(path: ':module_a', configuration: 'release')
debugCompile project(path: ':module_a', configuration: 'debug')

For Android Studio 3.x

You don't need to explicitly specify the build variant for a "module project". Just using

implementation project(':library') 

will automatically help select the correct build variant.

Here is a detailed explanation: https://developer.android.com/studio/build/?utm_source=android-studio#variant_aware


For Android Studio 3.x Upgraded from 2.x

Delete the .idea folder under your project root directory and restart your Android Studio.

Below is the GUI screenshot:

enter image description here

Hope it helps!

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 1
    this is only for older gradle versions: [link](https://stackoverflow.com/questions/33718663/gradle-error-configuration-declares-dependency-which-is-not-declared), refer to post by @rockefelox for newer versions – Madura Anushanga Dec 09 '17 at 08:52
  • True. Since Android Studio 3.0, all the xxxCompile closures change to xxxImplementation. – shizhen Dec 11 '17 at 02:13
  • 1
    For Android Studio 3.1.2, you don't need to explicitly specify the build variant for a "module project". Just using implementation project(':library') will automatically help select the correct build variant. Here is a detailed explanation https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration?utm_source=android-studio#variant_aware . – shizhen Jun 18 '18 at 07:04
14

Just need to cut ".idea" Folder and paste it outside Project root folder( For Back up if you need).These files will be auto regenerated. On Opening the project it"ll ask add module (app) to your project. You can ignore the same.

It set set default build variant to be "debug". You can see build variant tab on left corner or hover on "Monitor" Symbol on left bottom to get build variant option.

deva11
  • 881
  • 10
  • 25
10

I just had the same issue. Solved it by closing Android Studio, removing the generated files and folders: .gradle, .idea, app/.externalNativeBuild, app/build, build, app/app.iml, ProjectName.iml, local.properties, then relaunching Android Studio and allowing it regenerate all these files from scratch.

Andrew Starostin
  • 514
  • 1
  • 4
  • 12
6

Had the same problem, solved creating a new temporary Build Type, Build > Edit Build Types, select Build Types and add a new one. Sync, then you can select the new build type, and then revert back to original Debug build type.

rockefelox
  • 61
  • 1
  • 3
2

Open your module setting. (Click F4)

Go to build types and create a new Build by clicking on plus(+) sign.

Name it anything, like "demo".

Duplicate all the data of debug build.

Now when you open Build Variant you will be able to switch to Debug as well as Demo.

1

For me I was unable to switch to our 'devDebug' variant but I could switch to another variant like 'devRelease' and then 'devDebug'. So try switching to another variant first.

checkmate711
  • 3,301
  • 2
  • 35
  • 45
1

I ran into a similar issue. My module level build.gradle reflected configurations specified for "debug" AND "release" accordingly. I was able to successfully run my app on emulator and device (LG) prior To Building and Signing My Release Version Of My App. After Building and Signing The Release Version and attempting to Run The Signed APK on My Phone and Device, i received an Error PM Session 'mobile': Error Launching activity....Error while Launching activity..So I started retracing my steps and realized that before deploying the signed release variant, i opened up the run/debug Configurations Dialogenter image description here

and mistakenly selected the the Value APK from app bundle value From The Deploy attribute, under the Installation category enter image description here

I Signed My APK In its ENTIRETY and NOT Via Bundle, so the option selected was attempting to Deploy an invalid App Bundle That Was Never Generated To Begin With, even if it was for the correct Build Variant.

1

For me, it was an issue with the gradle version. Make sure your plugin version and the required gradle version fit together, as defined here.

https://developer.android.com/studio/releases/gradle-plugin

You can find out which gradle versions you are using under:

File -> Project Structure... -> Project

vorwerg-ni
  • 157
  • 1
  • 5
1

I tried all the above to no effect. Eventually stumbled across File > Invalidate Caches/Restart... for other reasons and found it worked for me.

alman
  • 83
  • 6
0

I have this question too. My solution is checkout to the branch which i had changed the build variants. And at that branch, I can change from release back to debug. Then just checkout to current branch and everything is OK. It seems to be a Android Studio bug.

0

English is not my native language; please excuse typing errors.

I also meet this situation. i do this to solv .
Solv:
1. delete each module's impl suffix type file and build folder;
2. then click this button to sync project with gradle build file. button position

finally. and then this problem will be fix.

jdir.s
  • 97
  • 8
0

I fixed this issued by:

  1. Add a new build type named debug1 via edit build.gradle;Enable debug function of this build type;
  2. Sync and select the new build type debug1;
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

This issue seems to occur when opening the project in a symlinked location on linux. Opening the project directly fixed this issue for me.

relevant link: https://issuetracker.google.com/issues/156857164

  • While this might answer the question, you should [edit] your answer to include any important content from the link that's relevant to the question in the answer itself. Answers that consist primarily of a link to somewhere else can become invalid if the link stops working for whatever reason. – Hoppeduppeanut Jun 15 '20 at 23:33
0

in main root of app in build.gradle

replace it from:

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
}

to my Android Studio v3.0.1 in my case:

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
}
saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
0

March 26, 2021: Would not switch from Release to Debug, File | Invalidate Caches fixed it for me

Mick
  • 663
  • 1
  • 9
  • 18
-3

Unrelated to your build.gradle file,

Sharing hoping it might help someone else -

I was having similar issue it was because one of the line in build.gradle -

android {
    ...
    publishNonDefault true // remove this line and it should work!
}

Here you can get more detail about publishNonDefualt -


It is also possible to publish all variants of a library. We are planning to allow this while using a normal project-to-project dependency (like shown above), but this is not possible right now due to limitations in Gradle (we are working toward fixing those as well). Publishing of all variants are not enabled by default. The snippet below enables this feature:


Manisha
  • 813
  • 13
  • 24