简体   繁体   English

无法在 Android Studio 中切换到调试构建变体

[英]Unable to switch to debug build variant in Android Studio

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.我试图恢复 build.gradle 中的所有更改,就像配置之前一样,但没有运气。

Any ideas how to fix the issue?任何想法如何解决这个问题?

I had a similar problem where most of the Build menu items were greyed out.我有一个类似的问题,其中大部分 Build 菜单项都变灰了。 变灰

'Sync project with Gradle files' didn't fix. “将项目与 Gradle 文件同步”未解决。

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.我注意到 Android Studio (v 3.1.2) 左下角有一个“Build Variants”切换按钮,最终能够选择我需要的变体。

从这里选择变体

Maybe this will work for you too.也许这对你也有用。

Maybe you have got your solution to this, just in case, i provide my solution here.也许你已经有了解决方案,以防万一,我在这里提供我的解决方案。


For Android Studio 2.x对于 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对于 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这里有详细解释: https : //developer.android.com/studio/build/?utm_source=android-studio#variant_aware


For Android Studio 3.x Upgraded from 2.x对于从 2.x 升级的 Android Studio 3.x

Delete the .idea folder under your project root directory and restart your Android Studio.删除项目根目录下的.idea文件夹,重启Android Studio。

Below is the GUI screenshot:下面是图形用户界面截图:

在此处输入图片说明

Hope it helps!希望能帮助到你!

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.只需要剪切“.idea”文件夹并将其粘贴到项目根文件夹之外(如果需要,可以备份)。这些文件将自动重新生成。 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.您可以在左下角看到构建变体选项卡或将鼠标悬停在左下角的“监控”符号上以获取构建变体选项。

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.通过关闭 Android Studio,删除生成的文件和文件夹来解决它: .gradle.ideaapp/.externalNativeBuildapp/buildbuildapp/app.imlProjectName.imllocal.properties ,然后重新启动 Android Studio 和允许它从头开始重新生成所有这些文件。

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.同步,然后您可以选择新的构建类型,然后恢复到原始的 Debug 构建类型。

Open your module setting.打开您的模块设置。 (Click F4) (点击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.现在,当您打开 Build Variant 时,您将能够切换到 Debug 和 Demo。

For me I was unable to switch to our 'devDebug' variant but I could switch to another variant like 'devRelease' and then 'devDebug'.对我来说,我无法切换到我们的“devDebug”变体,但我可以切换到另一个变体,例如“devRelease”,然后是“devDebug”。 So try switching to another variant first.因此,请先尝试切换到另一个变体。

I ran into a similar issue.我遇到了类似的问题。 My module level build.gradle reflected configurations specified for "debug" AND "release" accordingly.我的模块级别 build.gradle 相应地反映了为“调试”和“发布”指定的配置。 I was able to successfully run my app on emulator and device (LG) prior To Building and Signing My Release Version Of My App.在构建和签署我的应用程序发布版本之前,我能够在模拟器和设备 (LG) 上成功运行我的应用程序。 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 Dialog在构建并签署发布版本并尝试在我的手机和设备上运行已签名的 APK 后,我收到一个错误PM Session 'mobile': Error Launching activity....Error while Launching activity ..所以我开始回溯我的步骤和意识到在部署签名发布变体之前,我打开了运行/调试配置对话框在此处输入图片说明

and mistakenly selected the the Value APK from app bundle value From The Deploy attribute, under the Installation category并错误地选择了来自应用程序包的值APK值来自部署属性,在安装类别下在此处输入图片说明

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 .我签署了我的 APK 完整而不是通过 Bundle ,因此选择的选项试图部署一个从未生成的无效 App Bundle,即使它是用于正确的Build Variant

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.这似乎是一个 Android Studio 错误。

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; 1.删​​除each module's impl后缀类型文件和build文件夹;
2. then click this button to sync project with gradle build file. 2.然后单击此按钮将项目与gradle构建文件同步。 button position按钮位置

finally.最后。 and then this problem will be fix.然后这个问题将得到解决。

I fixed this issued by:我修复了这个问题:

  1. Add a new build type named debug1 via edit build.gradle;Enable debug function of this build type;通过edit build.gradle添加一个名为debug1的新构建类型;启用该构建类型的调试功能;
  2. Sync and select the new build type debug1;同步并选择新的构建类型 debug1;

This issue seems to occur when opening the project in a symlinked location on linux.在 linux 上的符号链接位置打开项目时似乎会发生此问题。 Opening the project directly fixed this issue for me.打开项目直接为我解决了这个问题。

relevant link: https://issuetracker.google.com/issues/156857164相关链接: https : //issuetracker.google.com/issues/156857164

For me, it was an issue with the gradle version.对我来说,这是 gradle 版本的问题。 Make sure your plugin version and the required gradle version fit together, as defined here.确保您的插件版本和所需的 gradle 版本符合此处的定义。

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

You can find out which gradle versions you are using under:您可以在以下位置找到您正在使用的 gradle 版本:

File -> Project Structure... -> Project文件 -> 项目结构... -> 项目

I tried all the above to no effect.我尝试了以上所有方法都没有效果。 Eventually stumbled across File > Invalidate Caches/Restart... for other reasons and found it worked for me.最终偶然发现 File > Invalidate Caches/Restart... 出于其他原因,发现它对我有用。

in main root of app in build.gradle在 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:到我的 Android Studio v3.0.1 就我而言:

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

March 26, 2021: Would not switch from Release to Debug, File | 2021 年 3 月 26 日:不会从发布切换到调试,文件 | Invalidate Caches fixed it for me无效缓存为我修复了它

Unrelated to your build.gradle file,与您的 build.gradle 文件无关,

Sharing hoping it might help someone else -分享希望它可以帮助别人 -

I was having similar issue it was because one of the line in build.gradle -我遇到了类似的问题,这是因为 build.gradle 中的一行 -

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

Here you can get more detail about publishNonDefualt - 在这里您可以获得有关 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).我们计划在使用正常的项目到项目依赖项(如上所示)时允许这样做,但由于 Gradle 的限制(我们也正在努力修复这些限制),目前无法实现。 Publishing of all variants are not enabled by default.默认情况下不启用所有变体的发布。 The snippet below enables this feature:下面的代码段启用了此功能:


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM