googlesamples / google-services

A collection of quickstart samples demonstrating the Google APIs for Android and iOS
https://developers.google.com
Apache License 2.0
3.07k stars 2.53k forks source link

BuildType/Flavour Support #54

Closed chrisjenx closed 8 years ago

chrisjenx commented 9 years ago

I have had a look, I can't see another issue.

How do we enabled different configs per flavour/buildType? Most Common use case is where we have different analytics tracking and GCM senders per debug/release builds.

Favours would be an added bonus.

Thanks.

ianbarber commented 9 years ago

If you vary on package name you should be OK - the gradle plugin should pull config from the JSON based on the app's package name.

silvolu commented 9 years ago

It won't solve the GCM senders part though: you got one per app, so the different packages will share the same GCM sender ID.

ajfclark commented 8 years ago

@ianbarber Can you give an example of how to combine multiple google.services files together when the package name varies per flavour?

chrisjenx commented 8 years ago

You just keep adding them to the config on the website and it combines them all together in the JSON file.

On Tue, 24 Nov 2015, 14:28 Andrew Clark notifications@github.com wrote:

@ianbarber https://github.com/ianbarber Can you give an example of how to combine multiple google.services files together when the package name varies per flavour?

— Reply to this email directly or view it on GitHub https://github.com/googlesamples/google-services/issues/54#issuecomment-159426297 .

bigD666 commented 8 years ago

Stop emailing me On 25 Nov 2015 08:58, "Andrew Clark" notifications@github.com wrote:

@ianbarber https://github.com/ianbarber Can you give an example of how to combine multiple google.services files together when the package name varies per flavour?

— Reply to this email directly or view it on GitHub https://github.com/googlesamples/google-services/issues/54#issuecomment-159426297 .

ajfclark commented 8 years ago

@chrisjenx Where? I go to https://developers.google.com/mobile/add, pick android, Pick the app and package name, add analytics as a service and then the only option is to generate the config file.

[edit: oh, I see now. You have to go in, add a service, generate a config, go back to the original url, add another service, etc. That's horribly non-intuitive.

Is there anywhere you can see what's already been added to a project, modify existing services, etc?]

ajfclark commented 8 years ago

@bigD666 I'm not. Github is. You must be watching this project or issue or something?

chrisjenx commented 8 years ago

@ajfclark correct, pick the same "App Name" then add new package names:

If you pick the same package name and continue, you can add/see whats added to that config.

See example:

screen shot 2015-11-24 at 16 07 32
ajfclark commented 8 years ago

It'd be nice if there was an "add another service" or something button to make it obvious that you could do that.

samtstern commented 8 years ago

@ajfclark agreed, we are currently considering the best way to support build flavors in the UI.

yairkukielka commented 8 years ago

@samtstern yes, please. This would be great. I have another tricky case with our app:

We have 4 build variants: FreeDev, FreeProd, PaidDev and PaidProd. Each of these variants has a Google Services project, so we have 4 Google Services project ids: Free dev, Free prod, Paid dev and Paid prod.

Is there a way to get only one google-services.json for all these Google Services projects? If I had only one Google Services account with multiple flavors (package names) I see that I could apply what @chrisjenx is saying, but how do I achieve that for different Google Services projects?

If it's not possible, how should I use different google-services.json files in my local project?

This is what I see when I try to enable Google Services for my app: screen shot 2015-12-04 at 11 56 11 am

samtstern commented 8 years ago

@yairkukielka there is no way (and there probably never will be) to get one google-services.json file for multiple distinct projects. However with build flavor support you would just have four google-services iles and it would look something like this:

app/
  src/
    FreeDev/google-services.json
    FreeProd/google-services.json
    PaidDev/google-services.json
    PaidDev/google-services.json
yairkukielka commented 8 years ago

ok, Thank you Sam!

I thought the plugin only worked when the google-services.json file was placed on the /app root folder. It it works placing the google-services.json file in every flavour's folder, that's not a big deal :)

ajfclark commented 8 years ago

@yairkukielka That's what we're asking for, not what's currently supported.

yairkukielka commented 8 years ago

I see... In that case, +1 to this issue. Flavor support would be very interesting.

stsandro commented 8 years ago

Wow, implementing/including Google Play Services has really become complicated since the last time I've done some Android development! +1 for this issue! I need this to build multiple apps with different app-ids from the same code base!

chrisjenx commented 8 years ago

I think another work around for now would be to create a "core" module which is effectively a library project. Then create child projects which have their own google-services.json

core
|- freedev
|- freeprod
|- paiddev
|- paidprod

By no means a "good" solution but that would let you do what you need for now.

samtstern commented 8 years ago

There is a workaround for all of this, which involves not using the google-services plugin at all and just recreating what it would do. The plugin is meant to be a convenience and it doesn't do anything you can't do yourself.

The main thing it does is create R.string.google_app_id using the value of the "mobilesdk_app_id" field from the JSON file.

The other things it does are optional and depend on the APIs you're using. If you're using GCM it creates R.string.gcm_default_senderId and if you're using Ads it creates R.xml.global_tracker. If you need these resources, you can simply create them yourselves.

Most of the other data in the JSON file is not currently used by the plugin so you can get away with just creating the resources you need in each build flavor you need them. The plugin does not affect any of your project's Java code and your app will run fine without it.

On Wed, Dec 9, 2015, 7:08 AM Christopher Jenkins notifications@github.com wrote:

I think another work around for now would be to create a "core" module which is effectively a library project. Then create child projects which have their own google-services.json

/ core |- / freedev |- / freeprod |- / paiddev |- / paidprod

By no means a "good" solution but that would let you do what you need for now.

— Reply to this email directly or view it on GitHub https://github.com/googlesamples/google-services/issues/54#issuecomment-163284112 .

chrisjenx commented 8 years ago

@samtstern OK great, thats good to know, thanks for that!

samtstern commented 8 years ago

Good News Everyone

As of the build com.google.gms:google-services:2.0.0-alpha3 you should be able to put your google-services.json file inside your build flavors directories like this:

app/src/
    flavor1/google-services.json
    flavor2/google-services.json

I will leave this issue open so that we can collect feedback on whether or not this solves your problems. Thanks for your patience thus far.

sregg commented 8 years ago

This is still not working for me. I have app/src/debug/google-services.json and app/src/release/google-services.json and the plugin is still complaining that google-services.json is missing in the root folder. (using com.google.gms:google-services:2.0.0-alpha3)

m-i-k-e-e commented 8 years ago

@sregg probably because you're not using flavors but buildTypes

renejahn commented 8 years ago

So I understand that this could help me with flavors. But in our gradle configuration the dev buildType adds ".dev" to the package name which does not match the package name of google-services.json. Also I have no place for buildType specific google-services.json files. So how do I handle flavors and buildTypes which both append something to the package name?

samtstern commented 8 years ago

Here is the more official google-services document, as promised: https://developers.google.com/android/guides/google-services-plugin

I hope that answers many of the questions on this thread. If it does not, feedback appreciated.

(Comment edited 1/8/2016 at 9:31am)

sregg commented 8 years ago

Thanks Sam but that link looks like a private Google link (it asks for username/password)...

sregg commented 8 years ago

I'm guessing the correct URL is: https://developers.google.com/android/guides/google-services-plugin

@samtstern so there is no way to use buildTypes (debug/release) instead of flavors?

samtstern commented 8 years ago

Whoops yes: https://developers.google.com/android/guides/google-services-plugin

I accidentally linked to the internal server, sorry about that! I am going to edit the original comment as well, for clarity.

samtstern commented 8 years ago

I think the terminology in this thread has been incorrect the whole time (which is partially my fault, I had been saying build type/build variant/product flavor interchangeably).

I am getting my terminology from here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

If you check out the linked document, you will see that build types are supported (and probably what I meant to be saying all along when I said build flavor).

sregg commented 8 years ago

I see, does that mean that you guys changed something recently? Because 15 days ago it wasn't working: https://github.com/googlesamples/google-services/issues/54#issuecomment-167166704

samtstern commented 8 years ago

Ah I see. Can you try putting google-services.json in the app/ folder as well? I think if you have it in all three locations (app/src/release, app/src/debug and app/) the plugin will actually always ignore the root one, but it may just be complaining that one does not exist there.

Either way I will file a bug to get something fixed. If it all works for you, then the bug is simple (don't require a root json file when there are type-specific ones). If it does not all work for you, then we will have to look for a root cause.

sregg commented 8 years ago

Awesome! I'll try again and let you know how it goes. I'll also try with proper flavors and se if that works fine.

viralypatel commented 8 years ago

I'm using four different build types as follows:

buildTypes {
    releasefree.initWith(buildTypes.release)
    releasefree {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    releasepro.initWith(buildTypes.release)
    releasepro {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        applicationIdSuffix ".pro"
    }
    debugfree.initWith(buildTypes.debug)
    debugfree {
        shrinkResources true
        applicationIdSuffix ".debug"
        debuggable true
    }
    debugpro.initWith(buildTypes.debug)
    debugpro {
        shrinkResources true
        applicationIdSuffix ".pro.debug"
        debuggable true
    }
}

With build variant for ReleaseFree (for one of the product flavors) selected, I Tried adding the google-services.json at

src\release as well as at src\releasefree

Still complains about the json missing from the root module folder. What am i missing?

And can i also put it directly into src\flavor1 or src\flavor2? each build variant has a unique applicationId. tried and wasn't working.

samtstern commented 8 years ago

@viralypatel this is a known issue, just make a copy of google-services.json and put it in the app/ folder (that's what the plugin means by the module root folder).

We will release a version soon that fixes this issue.

datrixo commented 8 years ago

One step closer. But what in the situation when I have for example GA tracking_id per localization folder. For example:

Unfortunetly google-services.json have "analytics_service" object, where the tracking_id is set per flavor, not per localization.

Any solutions?

samtstern commented 8 years ago

@datrixo I don't think the plugin will be able to support per-region configuration as that information is not (and likely will not be) contained in the JSON file.

In your case I would recommend not using the google-services plugin and just continuing manually as you would have before. If there is any service other than Analytics that you use that will require the google services plugin, you can read this guide to figure out how to recreate the work manually.

bdiegel commented 8 years ago

I read the guide but I am still confused. My release and debug buildTypes have the same package name. And the generated google-services.json has an oauth_client for each SHA-1. I split them into two files (app/src/debug & release) thinking this was cleaner and also because I plan to support a new product flavor soon. I'm getting the "missing root file" error and saw the recommendation is to put a JSON file in the root folder. Exactly what should that file contain?

[Update] I did some build testing (release and debug) with one app/google-services.json file with both oath_clients. This seems to work, so it is probably the easiest thing to do for now.

samtstern commented 8 years ago

@bdiegel the solution you figured out is correct.

RobertoAlvarezCeballos commented 8 years ago

Hi all, I have the json file on the root and on the debug flavor. When I try to run debug I get the following error "No matching client found for package name XXX.XXXX.XXXX" I have crated the json file for that variant of the package name. I have checked that the name is correct. Does anyone know what may be happening?

fmeneuhe commented 8 years ago

I have the same problem with the debug build. I get rid of the error removing it from the build.gradle: applicationIdSuffix '.debug'

When is going to be fixed this bug in the Google Services plugin?

RobertoAlvarezCeballos commented 8 years ago

Hi @fmeneuhe yes you can get rid of the error by removing the changes on the package but that's not a solution I guess.

What I've done and it's working so far is to include both package names on the same json file and that' seems to be working. I tried to put different files per build type but it was always using the one on the root not sure why.

fmeneuhe commented 8 years ago

Hi @RobertoIDL , do you mean to modify the json file?

RobertoAlvarezCeballos commented 8 years ago

Hi @fmeneuhe yes I do.

What I've done is copy and paste the block of the JSON that includes package name and add the debug package name as well.

Under the client part of the JSON you will see client info, I have duplicated that one and oauth_client, I have also duplicated that one. And that's it everything else stays the same and it's working is able to find both the debug and the live package name.

Hope it helps.

samtstern commented 8 years ago

@fmeneuhe @RobertoIDL I think there has been some confusion about how the JSON files work. Every time you get a new JSON file (by adding a new Android client, for example) the JSON contains all of the previous information as well. The google-services plugin then scans the file for the appropriate block to match the package name for your build variant.

It's actually a good idea to put identical JSON files in each location, each one containing all of the information for all of your package names, etc. The plugin will be intelligent and only use the correct piece of the file, no need to do manual JSON editing.

RobertoAlvarezCeballos commented 8 years ago

@samtstern I understand that, thanks for clarification.

The problem I had is that if I put the file on each build type location, each file containing only the package name of that build type this didn't work.

fmeneuhe commented 8 years ago

@samtstern this is very confusing. So, what do we have to do to use the plugin with the debug build and not get the error message "Error:Execution failed for task ... > No matching client found for package name 'mypackagename.debug'"?

samtstern commented 8 years ago

You need to go to developers.google.com/mobile/add and register that package name (mypackagename.debug). Then take the JSON file, and put it in the appropriate folder:

condesales commented 8 years ago

In my case, I just have different app prefixes for each one of the flavours I use. The google-services.json file has a format like:

{
  "project_info": {
    "project_id": "<project_id>",
    "project_number": "<project_number>",
    "name": "<project_name>"
  },
  "client": [],
  "client_info": [],
  "ARTIFACT_VERSION": "1"
}

As we can see, client is an array. What i've done was add all different clients I have to that array, changing the package_name and any other configuration to match each one of the flavours setup in my build.gradle app file.

adauvalter commented 8 years ago

I'm facing the same problem. I have all google-service.json for each build version from "Get a configuration file" in folders:

/app
/app/src/beta
/app/src/debug
/app/src/integration
/app/src/main

And I'm still receiving an Error:

Execution failed for task ':app:processDebugGoogleServices'. No matching client found for package name 'XXX.XXX.XXX.dev'

RobertoAlvarezCeballos commented 8 years ago

Hi, my recommendation is to do this:

1.) Forget about having multiple files (I know many people say this works but not in my case)

2.) Create 1 file and put it on /app

3.) Make sure the file has an entry for each of your package names

Run the app, it should work.

Hope it helps.

adauvalter commented 8 years ago

@RobertoIDL do you mean, that I have to create for each package name some client and add it under the section "client" in json file?