The introduction

One scenario that is bound to occur during development is that Android packages APK in at least three types: Debug package, beta public test package and Release online package, each type of APK may have different background interface addresses for connection testing, so we should include the interface addresses required by the three packages in a tool class, select the corresponding address when packaging, and annotate other addresses, which is not a problem. It’s just messy and error-prone. To solve this problem, there are some solutions online:

  • BuildeConfigFiled = buildeConfigFiled = debud = Release = baseUrl = buildeConfigFiled = debud = Release = baseUrl

    buildTypes {
            debug{
                buildConfigField "String" "BASE_URL" ""www.test.com""
            }
            release {
                buildConfigField "String" "BASE_URL" ""www.base.com""
            }
        }
    Copy the code
  • Write the interface’s BaseUrl in a file and read different configurations depending on the buildConfig value.

The first way to add an address is to modify the build.gradle file and synchronize it, which is a waste of time. The second option adds some IO code, which is a bit too expensive. This article mainly combines Gradle configuration and Android buildConfig feature to achieve multi-mode packaging, basically once and for all. First, take a look at the basic structure of the project as shown below:

  1. Start by creating a new abstract class appConfig.java that gets baseUrl, that is, reminding subclasses to implement abstract methods. The diagram below:

  1. Create a new apPreleaseconfig. Java in main that inherits from AppConfig. The diagram below:

  1. Create a debug directory in the same directory as main and appDebugconfig.java. The diagram below:

  1. Do the following configuration in build.gradle:

  1. When you execute build, you can see that the different configuration classes have been automatically generated in the build directory of your project

  1. Create appConstans.java to provide baseUrl values for other classes. The diagram below:

See this, some students will say, this with the above said the first way is not very different ah, don’t panic, and then look down.

  1. Create a new beta directory in the same directory as main and create AppBetaconfig.java. This is because we just created a new folder and did not tell the compiler that this folder is also compiled as Java code. The following configuration needs to be done:

  1. Add one more configuration in build.gradle. The diagram below:

  1. Run the build and you’ll be surprised if there’s a beta in your project’s build folder

So when you compile the package APK, there will be different package modes for you to choose from, and so on, can we have 100 compile modes, each of which can define 100 other properties like the baseUrl attribute, so that with a simple configuration, we can have multiple compile modes.

Follow for more Android technology sharingMy Android tech circle, you can also join QQ group number :690347536 to learn and exchange Android development skills.