【问题标题】:How to Run Different Product Flavors in Android Studio如何在 Android Studio 中运行不同的产品风格
【发布时间】:2017-08-17 22:52:41
【问题描述】:

我正在编写我的第一个 Android 应用程序,而我刚刚开始使用产品风格。我有一个支持广告的应用程序处于测试阶段,我正在编写一个没有广告的付费版本。我想我可以编译这两种口味。当我打开 gradle 窗口时,我看到诸如“编译 AdsDebugSources”和“编译 PremiumDebugSources”之类的目标。

现在,如果我双击其中任何一个,构建将运行完成而不会出现错误,但我不知道如何运行结果。如果我点击屏幕顶部的绿色“运行”箭头,我将永远无法运行高级应用程序。

只有一个条目“app”会导致在连接的设备上安装和运行 apk,它是 AdsDebug 版本。我想我需要添加一个新配置,但我找不到任何文档甚至提到“风味”这个词。

我已尝试添加配置,但我不明白问题的含义。我查看了默认应用程序的设置,但它们似乎意义不大。如何告诉它我想要我的应用的高级版本?

或者我的问题与配置无关?我注意到,当我查看Build/Edit Flavors 时,列出了两种风格,但没有填写任何数据字段。我原以为这些会从清单中复制。我忽略了什么吗?

我设置风味所做的只是将此代码添加到应用程序级别的 build.gradle 文件中:

flavorDimensions "dummy"

productFlavors {
    ads {
        dimension "dummy"
        applicationId 'com.example.myApp.ads'
    }

    premium {
        dimension "dummy"
        applicationId 'com.example.myApp.premium'
    }

}

我还需要做什么?

【问题讨论】:

  • 看看我关于构建变体的文章。它详细讨论了构建变体。 How to create Android Product Flavors and Build Variants
  • 这是一篇好文章。如果它解释了有关各种口味的清单会更好。我发现那个相当不透明的文档。
  • 我将很快在我的文章中为不同的清单添加一个部分。

标签: android


【解决方案1】:

在 Android Studio 中打开 Build Variants 工具。默认情况下,它停靠在左侧。

它将显示项目中的模块列表,每个模块都有一个下拉菜单,指示“运行”按钮将使用的构建变体。

【讨论】:

  • 它什么也没显示。我看到“模块”和“构建变体”标题,但没有列出任何模块。
  • @saulspatz:这……很奇怪。您可以尝试从主菜单中的 Tools > Android > Sync Project with Gradle Files,看看现在是否会导致在 Build Variants 工具中显示内容。您也可以考虑在 Android Studio 中创建一个废弃项目并查看其中的 Build Variants 工具,您应该会在其中看到一个 app 条目,其中包含两个构建变体(debug 和 release)。
  • 做到了。我有所有 4 个变体。谢谢一百万。
  • 我在“工具”或“构建”下没有看到任何称为“构建变体”的东西。我知道在那些地方看我很傻。
  • @johnrubythecat 现在在文件中 > 使用 Gradle 文件同步项目
【解决方案2】:

解决方案

  1. 如果您想创建不同类型的产品风格(例如不同的 url:我们应用程序的开发 url、质量 url 和生产 url)

所以你要按照这个代码正常工作。

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.premsinghdaksha"
        minSdkVersion 17
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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


    flavorDimensions "client"
    productFlavors {
// use for production debug and release build
        production {
            dimension "client"
            buildConfigField("String", "BASE_URL", "YourURL")
            //if you want to use string anywhere of app show you define
            // buildConfigField and get value where you want to use.
            buildConfigField("String", "Shop_", "\"Shop1\"")
            // production app name

            //if you want change application name  and app icon so you define
            // resValue, manifestPlaceholders and get these value in Manifests file.
            resValue "string", "app_name", "Your production name"
            //production  app icon
            manifestPlaceholders = [
                    appIcon     : "@mipmap/app_icon",
                    appIconRound: "@mipmap/app_icon_round"
            ]
            signingConfig signingConfigs.release

        }
        // use for quality debug and release build
        quality {
            dimension "client"
            buildConfigField("String", "BASE_URL", "YourQualityurl")
            buildConfigField("String", "Shop_", "\"Shop2\"")
            //use for quality app name
            resValue "string", "app_name", "quality App name"
            // use for quality app icon
            manifestPlaceholders = [
                    appIcon     : "@mipmap/quality_app_icon",
                    appIconRound: "@mipmap/quality_app_icon_round",
            ]
        }
// use for development debug and release build
        development {
            dimension "client"
            buildConfigField("String", "BASE_URL", "development url")
            buildConfigField("String", "Shop_", "\"Shop3\"")
            //use for dev app name
            resValue "string", "app_name", "developer app name"
            //use for dev app icon
            manifestPlaceholders = [
                    appIcon     : "@mipmap/developer_icon",
                    appIconRound: "@mipmap/developer_icon_round"
            ]
        }

    }
}
  1. 如果您想为实时应用创建signingApp,那么您必须在android{}中的build.gradle(:app) 中遵循此代码。

    signingConfigs {
            // use for signed apk
            release {
                storeFile file("../premsingh.jks")
                storePassword "app@app"
                keyAlias "key0"
                keyPassword "app@app"
                v1SigningEnabled true
                v2SigningEnabled true
            }
        }

结果将显示在构建变体上并单击下拉按钮使用变体的单击。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 2016-07-11
    • 2023-03-06
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多