使用gradle从单一来源

时间:2017-01-09 18:18:17

标签: android android-studio gradle apk

ANSWER

确定。所以这里发生了很多事。那么让我来回答一下。

首先创建所需的口味:

productFlavors{
    training{
        applicationIdSuffix ".training"
        versionNameSuffix "-training"
    }
    full{

    }
}

其次,获取您可能需要的所有路径结构的列表:

  • 单击IDE右侧的Gradle图标
  • 在您的项目下>任务> android,单击sourceSet:这将显示您可能需要的所有目录结构
  • 右键单击sourceSet,然后单击“运行”以获取目录结构列表
  • 请勿将/ {app中的google-services.json文件移除到/ main。然后复制google-services.json并在每个flavor目录下放置一个。{1}}。所以对我来说,我在/培训
  • 下放了一个
  • 重要转到开发人员控制台,为每种风格创建凭据:生产和调试。并使用正确的信息更新每个google-services.json文件。

问题

我可以告诉你业务需求,但这有点不重要。我需要从单个源代码创建两个“生产”APK。没有什么可以改变的。这是完全相同的应用程序,除了它有两个,每个都有一个不同的启动器图标。我甚至不知道如何开始这样做。有没有人完成这个,不介意分享?我在Mac上使用带有Gradle的Android Studio。

更新

根据反馈,我能够将我的gradle文件更新为

productFlavors{
    training{
        applicationIdSuffix ".training"
        versionNameSuffix "-training"
    }
    full{
        applicationIdSuffix ""
        versionNameSuffix ""
    }
}

现在fullDebug和fullRelease编译正常。但是当我将Build Variant更改为trainingDebug或trainingRelease时,代码不再编译:R无法解析。

修改

我终于看到了你们要求的东西。这是跟踪:

Executing tasks: [:app:generateTrainingDebugSources, :app:prepareTrainingDebugUnitTestDependencies, :app:mockableAndroidJar, :app:generateTrainingDebugAndroidTestSources]

Configuration on demand is an incubating feature.
WARNING: The `android.dexOptions.incremental` property is deprecated and it has no effect on the build process.
Incremental java compilation is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preTrainingDebugBuild UP-TO-DATE
:app:checkTrainingDebugManifest
:app:preFullDebugBuild UP-TO-DATE
:app:preFullReleaseBuild UP-TO-DATE
:app:preTrainingReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2420Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72420Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72420Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2420Library UP-TO-DATE
:app:prepareComAndroidSupportPaletteV72420Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72420Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCompat2420Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUi2420Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUtils2420Library UP-TO-DATE
:app:prepareComAndroidSupportSupportFragment2420Library UP-TO-DATE
:app:prepareComAndroidSupportSupportMediaCompat2420Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42420Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2420Library UP-TO-DATE
:app:prepareComAndroidVolleyVolley100Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidAnswers136Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidBeta114Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlytics255Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlyticsCore238Library UP-TO-DATE
:app:prepareComDavemorrisseyLabsSubsamplingScaleImageView350Library UP-TO-DATE
:app:prepareComEgosventuresCar360LibraryCaptureCar360libraryCapture1012Library UP-TO-DATE
:app:prepareComGetbaseFloatingactionbutton1101Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppinvite940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAuth940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAuthBase940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesGcm940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesIid940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesLocation940Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesTasks940Library UP-TO-DATE
:app:prepareComGoogleFirebaseFirebaseAnalytics940Library UP-TO-DATE
:app:prepareComGoogleFirebaseFirebaseAnalyticsImpl940Library UP-TO-DATE
:app:prepareComGoogleFirebaseFirebaseCommon940Library UP-TO-DATE
:app:prepareComGoogleFirebaseFirebaseCore940Library UP-TO-DATE
:app:prepareComGoogleFirebaseFirebaseIid940Library UP-TO-DATE
:app:prepareComGoogleFirebaseFirebaseInvites940Library UP-TO-DATE
:app:prepareComGoogleFirebaseFirebaseMessaging940Library UP-TO-DATE
:app:prepareComJakewhartonTimberTimber410Library UP-TO-DATE
:app:prepareComMelnykovFloatingactionbutton130Library UP-TO-DATE
:app:prepareComWritingmindsFFmpegAndroid032Library UP-TO-DATE
:app:prepareIoFabricSdkAndroidFabric1310Library UP-TO-DATE
:app:prepareJpWasabeefRicheditorAndroid120Library UP-TO-DATE
:app:prepareTrainingDebugDependencies
:app:compileTrainingDebugAidl UP-TO-DATE
:app:compileTrainingDebugRenderscript UP-TO-DATE
:app:generateTrainingDebugBuildConfig UP-TO-DATE
:app:mergeTrainingDebugShaders UP-TO-DATE
:app:compileTrainingDebugShaders UP-TO-DATE
:app:generateTrainingDebugAssets UP-TO-DATE
:app:mergeTrainingDebugAssets UP-TO-DATE
:app:processTrainingDebugManifest UP-TO-DATE
:app:fabricGenerateResourcesTrainingDebug
:app:generateTrainingDebugResValues UP-TO-DATE
:app:processTrainingDebugGoogleServices
Parsing json file: /Users/myname/StudioProjects/appname/app/google-services.json
:app:processTrainingDebugGoogleServices FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processTrainingDebugGoogleServices'.
> No matching client found for package name 'com.company.appname.training'

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 9.567 secs

2 个答案:

答案 0 :(得分:1)

将产品风格添加到build.gradle中,如下所示:

android {
    ...
    productFlavors {
        prod1 {}
        prod2 {}
    }
}

然后,将prod2风味的更改图标放在此位置:

<project-dir>/app/src/prod2/res

您在prod2下放置的所有资源都将覆盖main的资源。如果您愿意,您当然也可以使用prod1

您可以在此处阅读更多内容:https://developer.android.com/studio/build/build-variants.html

答案 1 :(得分:1)

一个完整的例子:

在build.gradle(Module:app)文件中,创建两个不同的产品风格,如下所示:

--paper-slider-knob-start-color

现在转到android { .... productFlavors { flavor1 { } flavor2 { } } } 并选择第一种风格的启动器图标,单击 Next ,然后显示res目录,单击下拉菜单并选择flavor1,它应该看起来像这样:

Res Directory:src / flavor1 / res

点击完成并重复同样的事情,但对于带有另一个启动器图标的flavor2。

现在,当您构建应用时,屏幕左下角有一个标有&#34; Build Variants&#34;的垂直菜单。单击它,您可以选择要部署的构建变体。不同的味道将导致不同的发射器图标。