将google-services.json提交给GitHub [英] Commiting google-services.json to GitHub
问题描述
我正在创建一个公共Android项目,并且正在使用Google Sign-In服务。我正在根据本教程进行操作。正如它所说,我已经得到了google-services.json文件。我需要将上述文件提交给Github吗?其他开发人员(如果有人贡献)需要此文件吗?或者他们必须创建自己的?顺便说一下,我正在使用Travis-CI。这个文件会影响到CI构建吗?
您可以创建一个新的 build variant 并将模板 google-services.json
存储为用于在您的应用程序中在您的CI平台上构建 build.gradle
。
使用不同的 google-services。 json
为新的 dev
构建变体(请参阅这篇文章)。将以下 google-services.json
模板添加到 app / src / dev
文件夹:
{
project_info:{
project_number:,
project_id:
},
client:[
{
client_info:{
mobilesdk_app_id:1:123456789012:android:1234567890123456,
android_client_info:{
package_name:com.your.package
}
},
oauth_client:[
{
client_id: ,
client_type:3
},
{
client_id:,
client_type:1,
android_info: {
package_name:com.your.package,
certificate_hash:
}
}
],
a pi_key:[
{
current_key:
}
],
services:{
analytics_service:{
status:2,
analytics_property:{
tracking_id:
}
},
appinvite_service:{
status:1,
other_platform_oauth_client:[]
},
ads_service:{
status:1
}
}
}
],
configuration_version:1
}
请注意,如果您还使用Google Analytics或GCM服务,我已经扩展了此Google服务。 您可以进行以下配置:
/ p>
app /
├──src /
│├──main /
│└ ──dev /
│└──google-services.json
├──google-services.json
└──build.gradle
您可以使用以下任一方法:
- 新的生成类型
- a新产品的味道(如果您已有的话)
生成类型
添加以下构建类型:
buildTypes {
dev {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
我们不需要在常规构建中构建此dev构建变体,因此如果未指定参数,则可以排除此变体。将以下内容添加到您的应用中 build.gradle
:
def build_param =$ {build} ;
if(build_param!=dev){
//排除生产构建
android.variantFilter {variant - >
if(variant.buildType.name.equals('dev')){
variant.setIgnore(true);
}
}
} else {
//排除除生产版本之外的所有内容
android.variantFilter {variant - >
if(!variant.buildType.name.equals('dev')){
variant.setIgnore(true);
}
}
}
产品风味
在现有产品中加入 dev
产品风格:
productFlavors {
full {
}
dev {
}
}
从常规版本中删除 dev
产品风格:
def build_param =$ {build};
if(build_param!=dev){
// exclude dev
android.variantFilter {variant - >
if(variant.getFlavors()。get(0).name.equals('dev')){
variant.setIgnore(true);
}
}
} else {
//排除除开发者外的所有元素
android.variantFilter {variant - >
if(!variant.getFlavors()。get(0).name.equals('dev')){
variant.setIgnore(true);
code
$ b $ hr
最后,将您的应用模块 google-services.json
添加到 .gitignore
:
app / google-services.json
只有在指定参数 build = dev
时,才会使用 dev
变种
编辑 .travis.yml
来修改构建配置:
script:
- ./gradlew clean build -Pbuild = dev
-Pbuild = dev
将只使用位于 app / src / dev / google-services中的 google-services.json
来构建开发版本变体.json
查看此示例项目,该示例项目使用 google-services Google project
在 Travis日志中,您可以看到被解析的JSON文件是一个用于 dev
构建变体:
解析json文件:/ home /travis/build/bertrandmartel/android-googlesignin/app/src/dev/google-services.json
额外注意
请注意,此方法不限于CI,并且可以在您需要生产 google-services.json
或不同的 AndroidManifest.xml
(具有一些特定属性,如
检查此方法,以防止嵌入在AndroidManifest.xml中(并且不能从gradle导入)中使用不同构建变体并使用参数来启用生产构建。
I am creating a public android project and I am using Google Sign-In service. I am doing it according to this tutorial. As it says, I have got the google-services.json file. Do I need to commit the above file to Github? Do other developers (If someone contributes) need this file? Or do they have to create there own? By the way I am using Travis-CI. Will this file affect to CI build?
解决方案 You can create a new build variant and store a template google-services.json
to be used for your build on your CI platform in your app build.gradle
.
Use a different google-services.json
for the new dev
build variant (see this post). Add the following google-services.json
template to app/src/dev
folder :
{
"project_info": {
"project_number": "",
"project_id": ""
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:123456789012:android:1234567890123456",
"android_client_info": {
"package_name": "com.your.package"
}
},
"oauth_client": [
{
"client_id": "",
"client_type": 3
},
{
"client_id": "",
"client_type": 1,
"android_info": {
"package_name": "com.your.package",
"certificate_hash": ""
}
}
],
"api_key": [
{
"current_key": ""
}
],
"services": {
"analytics_service": {
"status": 2,
"analytics_property": {
"tracking_id": ""
}
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 1
}
}
}
],
"configuration_version": "1"
}
Note that I have extended this google-services in case you also use Google Analytics or GCM service.
You would have the following configuration :
app/
├── src/
│ ├── main/
│ └── dev/
│ └── google-services.json
├── google-services.json
└── build.gradle
You can use either :
- a new build type
- a new product flavor (if you already have existing ones)
Build Type
Add the following build type:
buildTypes {
dev {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
We don't need to build this "dev" build variant in regular build, so you can exclude this variant if a parameter is not specified. Add the following to your app build.gradle
:
def build_param = "${build}";
if (build_param != "dev") {
//exclude production build
android.variantFilter { variant ->
if (variant.buildType.name.equals('dev')) {
variant.setIgnore(true);
}
}
} else {
//exclude all except production build
android.variantFilter { variant ->
if (!variant.buildType.name.equals('dev')) {
variant.setIgnore(true);
}
}
}
Product flavor
Add the dev
product flavor to existing ones :
productFlavors {
full {
}
dev {
}
}
To remove this dev
product flavor from the regular build :
def build_param = "${build}";
if (build_param != "dev") {
//exclude dev
android.variantFilter { variant ->
if (variant.getFlavors().get(0).name.equals('dev')) {
variant.setIgnore(true);
}
}
} else {
//exclude all but dev
android.variantFilter { variant ->
if (!variant.getFlavors().get(0).name.equals('dev')) {
variant.setIgnore(true);
}
}
}
Eventually, add your app module google-services.json
to .gitignore
:
app/google-services.json
We have previously ensured that this dev
variant will only be used when parameter build=dev
is specified
Edit .travis.yml
to modify the build config :
script:
- ./gradlew clean build -Pbuild=dev
-Pbuild=dev
will only build dev build variant using google-services.json
located in app/src/dev/google-services.json
Take a look at this sample project which is using google-services Google project
In Travis log, you can see that the JSON file being parsed is the one for the dev
build variant :
Parsing json file: /home/travis/build/bertrandmartel/android-googlesignin/app/src/dev/google-services.json
Extra Note
Note that this method is not limited to CI and can be extended for your production build when you require a production google-services.json
or a different AndroidManifest.xml
(with some specific properties like fabric.io key)
Check this method to prevent commitment of fabric keys embedded in AndroidManifest.xml (and can't be imported from gradle) that is using a different build variant and using a parameter to enable the production build.
这篇关于将google-services.json提交给GitHub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!