1

ANSWER

Ok. So a lot happened here. So let me assemble the answer.

First create the desired flavors:

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

    }
}

Second, get a list of all the path structures you may need by:

  • Click on the Gradle icon on the right of the IDE
  • under your project > Tasks > android, click sourceSet: This will show you all the directory structures you may need
  • right click sourceSet and click run to get a list of the directory structures
  • Do not remove the google-services.json file from /app to /main. Then make copies of google-services.json and place one under each flavor directory. So for me for example, I placed one under /training
  • IMPORTANT Go to developer console, and create credentials for each flavor: production and debug. And update each google-services.json file with the correct information.

QUESTION

I could tell you the business need, but that's a bit beside the point. I have a requirement to create two "production" APKs from a single source code. Nothing else to change. It's the exact same app, except there are two of it, each with a different launcher icon. I don't even know how to start doing it. Has anyone accomplished this and don't mind sharing? I am using Android Studio with Gradle, on a Mac.

UPDATE

Based on the feedback I was able to update my gradle file to this

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

Right now fullDebug and fullRelease compile fine. But when I change Build Variant to trainingDebug or trainingRelease, the code no longer compiles: R cannot be resolved.

EDIT:

I finally see what you guys were asking for. Here is the trace:

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
Nouvel Travay
  • 6,292
  • 13
  • 40
  • 65
  • Take a look in build flavors: https://developer.android.com/studio/build/build-variants.html – thyago stall Jan 09 '17 at 18:21
  • I already looked at the question marked as duplicate. For me, at least, it didn't help; leading me to ask this question. – Nouvel Travay Jan 09 '17 at 18:54
  • @thyagostall I followed the link and was able to add the flavors. But when I switch variant my app is not compiling. I will update the code to show. – Nouvel Travay Jan 09 '17 at 20:42

2 Answers2

1

Add product flavors to your build.gradle like so:

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

Then, put the changed icon for the prod2 flavor in this location:

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

Any resources you put under prod2 will override resources from main. You can of course also use prod1 if you wish.

You can read more here: https://developer.android.com/studio/build/build-variants.html

wojtek.kalicinski
  • 1,090
  • 7
  • 13
  • So I have followed the link (originally from @thyagoStall) and I have implemented the flavors. I keep prod1 empty, and I add suffixes to prod2. The problem is when I change the build to prod2Release, my code is not compiling. And the culprit seems to be the R file. I clean and reBuild, still nothing. – Nouvel Travay Jan 09 '17 at 20:35
  • Post the full error please – wojtek.kalicinski Jan 10 '17 at 00:39
  • There is no error trace. It's Android Studio. It says it cannot resolve the R. If you use IDEs there are red squiggly lines for things that do not compile. – Nouvel Travay Jan 10 '17 at 01:06
  • Although I mark this as accepted answer, you should read the original post to see full answer and potential bug fixes. – Nouvel Travay Jan 10 '17 at 01:21
1

A full example:

In your build.gradle (Module: app) file, create two difference product flavors like so:

android {
    ....
    productFlavors {
        flavor1 {
        }
        flavor2 {
        }
    }
}

Now go to File -> New -> Image Asset and choose your launcher icon for the first flavor, click Next, and where it says res directory click the drop down menu and choose flavor1, it should look like this:

Res Directory: src/flavor1/res

Click Finish and repeat the same thing but for flavor2 with the other launcher icon.

Now when you build your app, on the bottom left hand corner of the screen there is a vertical menu labelled "Build Variants". Click that and you can choose which build variant you would like to deploy. Different flavor will result in different launcher icons.

Bill
  • 4,506
  • 2
  • 18
  • 29