MPaaS user “Q-coder”

Meanwhile, more developers are welcome to contribute to mPaaS

Original text: blog.csdn.net/yqq577/article/details/116801705

preface

General access to mPaaS will not be described here.

Because it is relatively simple, according to the official document can be perfect access. Alternatively, you can use the mPaaS plug-in in Android Studio for faster and easier access.

After going through the above steps, I thought I saw the dawn of victory. All of a sudden, the CTO said to his iOS colleagues,

“Since Android is connected, iOS is also connected to mPaaS. However, the difficulty of iOS is that the company project uses multiple applications under one TARGETS, but only one mPaaS config file can be configured for one TARGETS.” ** (Editor’s note: this problem can be solved by manually replacing configuration files and images. For details, please search “33214637” for technical support students) **

After three seconds of hesitation, I realized that when I built Android, I used a similar approach — Build variants — to improve development efficiency.

At this point, we enter the “hard mode” of this article to solve the problem of “how to access mPaaS in multi-version mode”.

Hard mode

After a series of attempts (I can’t remember how long it took, but it caused me some pressure anyway), I found that I only need to import different App configurations under different Build Variants (different versions).

However, this is a hassle, and every time you call a different App, you have to import it again, which is really a bit of a chicken.

Then, use the mPaaS official documentation for configuring mPaaS in a multi-environment. Dynamically configure configuration files for different apps under Gradle.

Here comes the highlight – gradle in the main Module of the official demo

// Configure the mPaaS App development environment, Def setAppConfigEnv(String type){// Delete the original configuration File File configFile = file("${rootDir}/app").listFiles().find{File f -> f.name.endsWith(".config") } if(configFile ! = null && configfile.exists ()){delete(configFile)} // Copy files from different directories to shell projects. Copy {from "buildEnv/${type}" into "${rootDir}/app" include "**/*.Copy the code

The directory structure of the demo above is posted here to help you understand the code

Look at the above code, the idea is:

  • Check whether files with the suffix. Config exist. If so, delete them.

  • Then copy the new configuration file to the specified location.

But in the demo, it’s not dynamic, because setAppConfigEnv(‘dev’) is actually dead in the code.

So, we just need to get the Flavor value dynamically.

def getCurrentFlavor() { Gradle gradle = getGradle() String tskReqStr = gradle.getStartParameter().getTaskRequests().toString() Pattern pattern if (tskReqStr.contains("assemble")) pattern = Pattern.com running (" assemble (\ \ w +) (Release | Debug) ") / / Release and Debug the specific according to your own environment, to fill in the else pattern = Pattern.compile("generate(\\w+)(Release|Debug)") Matcher matcher = pattern.matcher(tskReqStr) if (matcher.find()) return  matcher.group(1).toLowerCase() else { println "NO MATCH FOUND" return "" } }Copy the code

The last thing you need to do is call buildTypes as follows

buildTypes {
    setAppConfigEnv(getCurrentFlavor())
    ...
}
Copy the code

All is well!

After this incident, I decided that the next step is to start learning Gradle. Being familiar with Gradle will help you cope with the later requirements.

It’s all there, if you have any questions about the details. Can leave a message or private message, I will be happy to answer for you.

notes

In the above journey, we used methods that might be common in multiple versions. recorded

  1. How do I obtain the current Flavor
def getCurrentFlavor() { Gradle gradle = getGradle() String tskReqStr = gradle.getStartParameter().getTaskRequests().toString() Pattern pattern; if( tskReqStr.contains( "assemble" ) ) pattern = Pattern.compile("assemble(\\w+)(Release|Debug)") else pattern = Pattern.compile("generate(\\w+)(Release|Debug)") Matcher matcher = pattern.matcher( tskReqStr ) if( matcher.find() ) return matcher.group(1).toLowerCase() else { println "NO MATCH FOUND" return ""; }}Copy the code
  1. How do I obtain the applicationId of the current Flavor
def getCurrentApplicationId() {
    def currFlavor = getCurrentFlavor()

    def outStr = ''
    android.productFlavors.all{ flavor ->

        if( flavor.name==currFlavor )
            outStr=flavor.applicationId
    }

    return outStr
}
Copy the code

Get a vote at the end of the year

MPaaS is participating in the “2021 Rare Earth Nuggets Annual Popular Creative Team List”. MPaaS Coder is welcome to scan the qr code below and cast your vote for mPaaS.