android 自动化构建_我如何自动化android应用程序构建过程的第1部分

android 自动化构建

How I Automated Android App Building Process (Part 1)
icon collected from www.iconfinder.com (Author: https://www.iconfinder.com/yash11)
www.iconfinder.com收集的图标(作者: https : //www.iconfinder.com/yash11 )

Sample project link: https://github.com/Ehsanul-Hoque/App-Automate/tree/master

示例项目链接: https : //github.com/Ehsanul-Hoque/App-Automate/tree/master

My company provides android app service to many other companies. When we come up with an agreement with a company, we have to replace the properties in the app, such as the package name, app name, app icon, database url, the theme color etc. Then we generate a new keystore file and then publish the apk file. It becomes a lot of hassle. A new company arrives and it takes us a few hours just to repeat the whole process again.

我的公司为许多其他公司提供android应用服务。 当我们与公司达成协议时,我们必须替换应用程序中的属性,例如包名称,应用程序名称,应用程序图标,数据库url,主题颜色等。然后我们生成一个新的密钥库文件,然后发布apk文件。 这变得很麻烦。 一家新公司到了,我们花了几个小时才再次重复整个过程。

Life is so much tough for a developer.
Background image: https://wallpapercave.com/w/wp2474663 背景图片:https://wallpapercave.com/w/wp2474663

But hey, why don’t we just automate the build process? We can acquire the necessary properties to change from an api. Then the properties will be changed in the app and published automatically. Sounds great, right? Well, then how are we going to do it? Let’s see what we can do.

但是,为什么我们不只是自动化构建过程呢? 我们可以获取必要的属性以通过api进行更改。 然后,属性将在应用程序中更改并自动发布。 听起来不错,对吗? 好吧,那我们该怎么做呢? 让我们看看我们能做什么。

For this blog, let’s say we have 2 companies that we have agreement with, Google and Facebook. So we need 2 different apps for them, with different properties.

对于此博客,假设我们有两家与我们达成协议的公司,分别是Google和Facebook。 因此,我们需要两个具有不同属性的不同应用程序。

In this part, I will show what modifications are needed in the android app. In the next part, I’ll show how to modify the properties with a python code.

在这一部分中,我将展示android应用中需要进行哪些修改。 在下一部分中,我将展示如何使用python代码修改属性。

确定属性: (Determine the properties:)

First, we need to determine what properties do we need to change every time. In this sample app, we are going to change the followings:

首先,我们需要确定每次需要更改哪些属性。 在此示例应用程序中,我们将更改以下内容:

  1. Package name

    包裹名字
  2. App name

    应用名称
  3. Theme color

    主题色
  4. App icon

    应用程式图示
  5. Keystore file

    密钥库文件

在进行操作之前,先计划如何做: (Plan how to do it before doing it:)

Image for post
It’s planning time!
正在计划时间!

如何更改包装名称? (How to change the package name?)

Now, first think of why we need to change the package name. In an Android app, the app considers the application id as the package name of the project. You can check the output of the method context.getPackageName(); in any java file to be sure. Change the application id in the module level build.gradle file, then build and run the project, and the output of context.getPackageName(); will also change. Every app in Google Play Store needs to have separate package name. It’s how they separate the apps. That’s why we need to change the application id for every app to change the package name easily. Thanks to Google, if they didn’t give us the ability to change package name just by changing the application id, we would have to change the package folders names and the package name in every java files to change the package name of the whole project. That would be a huge painful task. But now we don’t have to do that.

现在,首先考虑为什么我们需要更改软件包名称。 在Android应用程序中,该应用程序将应用程序ID视为项目的包名称。 您可以检查context.getPackageName()方法的输出 确保在任何Java文件中。 在模块级别的build.gradle文件中更改应用程序ID,然后构建并运行项目,以及context.getPackageName();的输出 也会改变。 Google Play商店中的每个应用都需要具有单独的程序包名称。 这就是他们分离应用程序的方式。 因此,我们需要为每个应用程序更改应用程序ID,以轻松更改软件包名称。 多亏了Google,如果他们不能让我们仅通过更改应用程序ID来更改软件包名称,我们就必须更改软件包文件夹名称和每个Java文件中的软件包名称,以更改整个项目的软件包名称。 。 那将是一项艰巨的任务。 但是现在我们不必这样做了。

如何修改资源和其他属性? (How to modify resources and other properties?)

Application id is set in the module level build.gradle file, and we can get that application id from anywhere in the project using context.getPackageName(); method. So we don’t need to store the application id anywhere else. But we have to put the app name in the strings.xml file (a.k.a string resource file), because we set the app name in the manifest from the string resource file. Again, we have to store the theme color in the colors.xml file (a.k.a color resource file). See, we have to change so many files. :(

应用程序ID在模块级别的build.gradle文件中设置,我们可以使用context.getPackageName();从项目的任何地方获取该应用程序ID 方法。 因此,我们不需要在其他任何地方存储应用程序ID。 但是我们必须将应用程序名称放置在strings.xml文件(也就是字符串资源文件)中,因为我们在字符串资源文件的清单中设置了应用程序名称。 同样,我们必须将主题颜色存储在colors.xml文件(又称为颜色资源文件)中。 看,我们必须更改这么多文件。 :(

But what if we store all these things in a single place and then use the properties from that file? Is it possible to create string and color resources dynamically from another file? Yes, it is! We will store the properties in the gradle.properties file and generate build variables and resources from them. If you don’t understand what I’m talking about, don’t worry. You’ll get it when you see it.

但是,如果我们将所有这些东西都存储在一个地方然后使用该文件中的属性该怎么办? 是否可以从另一个文件动态创建字符串和颜色资源? 是的! 我们将属性存储在gradle.properties文件中,并从中生成构建变量和资源。 如果您不了解我在说什么,请不要担心。 看到它就会得到它。

如何更改图标? (How to change the icon?)

For app icon, we don’t need to store it as a field. We will override the icon file with our new icon. That’ll do just fine.

对于应用程序图标,我们不需要将其存储为字段。 我们将使用新图标覆盖图标文件。 这样就可以了。

如何更新密钥库信息? (How to update keystore information?)

And lastly, we will store the keystore information in a separate <keystore>.properties file, and set the signing config in the module level build.gradle file.

最后,我们将密钥库信息存储在单独的<keystore> .properties文件中,并在模块级别的build.gradle文件中设置签名配置。

This is the changes needed in the app. We will write some python code in the next part to modify the properties from outside. I’ll talk about the plan of the python code later.

这是应用程序中所需的更改。 我们将在下一部分中编写一些python代码,以从外部修改属性。 稍后我将讨论python代码的计划。

修改应用代码: (Modify app code:)

Image for post

We will keep most of the properties information in the gradle.properties file and app level build.gradle file.

我们会将大多数属性信息保留在gradle.properties文件和应用程序级别的build.gradle文件中。

Add the following lines in the gradle.properties file.

gradle.properties文件中添加以下行。

Image for post

We have set the default values for the properties. Notice that we haven’t set the default value for app icon. It’s not needed actually. We will overwrite the previous app icon directly.

我们已经设置了属性的默认值。 请注意,我们尚未设置应用程序图标的默认值。 实际上并不需要。 我们将直接覆盖之前的应用程序图标。

Next, open the app level build.gradle file. Make the following changes.

接下来,打开应用程序级别的build.gradle文件。 进行以下更改。

app level build.gradle file
应用程序级别的build.gradle文件

Notice that we used resValue to create resource value. resValue "string", "app_name", APP_NAME means this will create a string resource field with name app_name , and it’s value will be the value of the APP_NAME field from gradle.properties file. Similarly, resValue "color", "colorPrimary", THEME_COLOR creates a color resource field with name colorPrimary and value of THEME_COLOR field value.

请注意,我们使用resValue创建资源值。 resValue "string", "app_name", APP_NAME表示这将创建一个名为app_name的字符串资源字段,其值将是gradle.properties文件中APP_NAME字段的值。 同样, resValue "color", "colorPrimary", THEME_COLOR创建名称为colorPrimary且值为THEME_COLOR字段值的颜色资源字段。

And look at the buildConfigField "String", "APP_NAME", "\"$APP_NAME\"" creates a BuildConfig constant field. We will talk about this in a moment.

并查看buildConfigField "String", "APP_NAME", "\"$APP_NAME\""创建一个BuildConfig常量字段。 我们将在稍后讨论。

So, resValue creates a new resource field. So you have to remove any resource field with the same name from the resource xmls. Remove the following lines from the strings.xml and colors.xml. If you don’t, you’ll get Duplicate resources error when building the app.

因此, resValue创建一个新的资源字段。 因此,您必须从资源xml中删除任何具有相同名称的资源字段。 从strings.xmlcolors.xml中删除以下几行。 如果不这样做,则在构建应用程序时会出现Duplicate resources错误。

Image for post
strings.xml
strings.xml
Image for post
colors.xml
colors.xml

Now build the project from Build > Rebuild Project or Build > Make Project.

现在,从Build> Rebuild Project或Build> Make Project来构建项目

Open AndroidManifest.xml and change the icon reference.

打开AndroidManifest.xml并更改图标引用。

android:icon="@drawable/app_icon"android:roundIcon="@drawable/app_icon"

Actually, our main modifications in the app are done. Now you have to use the build variables and resources that are generated. How do you use? You know how to use resource variables. If you can’t remember, R.color.colorPrimary is the way to get the colorPrimary resource. You deleted the colorPrimary resource field in the colors.xml file. but gradle auto generated the colorPrimary field, because you told so in the build.gradle file. And remember that I talked about BuildConfig constant field? See the following snippet:

实际上,我们在应用程序中的主要修改已完成。 现在,您必须使用生成的构建变量和资源。 你怎么用? 您知道如何使用资源变量。 如果您不记得, R.color.colorPrimary是获取colorPrimary资源的方法。 您删除了colors.xml文件中的colorPrimary资源字段。 但gradle会自动生成colorPrimary字段,因为您在build.gradle文件中已进行了告知 。 还记得我刚才讲过BuildConfig常量字段吗? 请参见以下代码段:

Image for post

Here, we used BuildConfig.APP_NAME; to access the APP_NAME field. Gradle created this field because we added buildConfigField "String", "APP_NAME", "\"$APP_NAME\"" in the app level build.gradle file. Here is the BuildConfig.java file that is auto generated by gradle:

在这里,我们使用BuildConfig.APP_NAME; 来访问APP_NAME字段。 Gradle创建此字段是因为我们在应用程序级别的build.gradle文件中添加了buildConfigField "String", "APP_NAME", "\"$APP_NAME\"" 。 这是gradle自动生成的BuildConfig.java文件:

Image for post

That APP_NAME constant field, along with other fields, is auto generated.

APP_NAME常数字段以及其他字段是自动生成的。

So now basically our app modifications are done, let’s go to part 2 to see how can we change the properties from outside the app, and then build the apk.

因此,现在基本上我们的应用程序修改已完成,让我们进入第2部分,了解如何从应用程序外部更改属性,然后构建apk。

Part 2: https://medium.com/@ehsanulfahad/how-i-automated-android-app-building-process-part-2-7b095469db5e

第2部分: https //medium.com/@ehsanulfahad/how-i-automated-android-app-building-process-part-2-7b095469db5e

翻译自: https://medium.com/@ehsanulfahad/how-i-automated-android-app-building-process-part-1-f010e4c4c644

android 自动化构建

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值