Generate an APK on a server

6

I was thinking of an architecture that would work as follows. The user would have a series of parameters to populate and automatically generate code there click in a Buttom, be it in web or mobile would generate an apk application with the passed parameters.

So, we would probably have a server responsible for compiling and generating the apk. Do you know if this is possible?

The application would have to be native android or Kotlin.

    
asked by anonymous 10.02.2018 / 12:03

1 answer

3

Yes, you can. Two possible approaches are:

Approach 1: Configuration via Gradle

You can create fields in Gradle and use them in the app. In addition, these values can be passed as a parameter via the command line at the time of apk build.

There are two types of values:

  • buildConfigValue - Is compiled as a static attribute of class BuildConfig . Can be used in Java and Kotlin code.
  • resValue - Is compiled as resource . It can be used in Java and Kotlin code and in XML.

In the following Gradle script,

  • A% of type resValue , with name string and default value hello

  • A% of type HELLO, WORLD! , with name buildConfigField and default value String

  • A hello2 function that tries to fetch a property in the project. If it is not found, it returns a default value.

android {
  ...
  defaultConfig {
    ...
    resValue "string", "hello", getPropertyValue("str_hello", "HELLO, WORLD!")
    buildConfigField "String", "hello2", getPropertyValue("str_hello2", "\"HELLO, WORLD 2!\"")
  }
}

def getPropertyValue(propertyName, defaultValue) {
  def value = project.getProperties().get(propertyName)
  return value != null ? value : defaultValue
}

Now you can access these values in your code:

message.text = resources.getString(R.string.hello)
message2.text = BuildConfig.hello2

HELLO, WORLD2! can also be used in XML:

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

And to override parameters via the command line, you would need to invoke:

./gradlew assembleDebug -Pstr_hello="HELLO, CMD!" -Pstr_hello2="\"HELLO, CMD2!\""

The next step would be to create a mechanism that maps parameters entered by a user into some type of interface to command line parameters, invoke the assemble with these parameters, and collect the generated apk.

Approach 2: Creating custom scripts

It consists of creating scripts capable of searching and changing code snippets, resources files, among others. It would involve a bit larger work to interpret files, but it's pretty powerful too.

In the end, an apk with getPropertyValue would also be generated to be made available.

This approach can be used in conjunction with Gradle Configuration .

It has a nice presentation of Heloisa Simon , where she tells how mobLee does to manage more than 450 apps in an almost fully automated way. They use a mix of the two approaches presented here.

    
28.03.2018 / 21:46