Yes, it is possible to do. Two possible approaches are:
Approach 1: Configuration via Gradle
You can create fields in Gradle and use them in the app. Even, these values can be passed as parameter via command line when building apk.
There are two types of values:
buildConfigValue
- It is compiled as a static attribute of the class BuildConfig
. Can be used in Java and Kotlin code.
resValue
- Is compiled as a resource
. Can be used in Java and Kotlin code and XML.
In the following Gradle script, they will be created
A resValue
of the kind string
, by name hello
and standard value HELLO, WORLD!
A buildConfigField
of the kind String
, by name hello2
and standard value HELLO, WORLD2!
A function getPropertyValue
that attempts 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
}
It will now be possible to access these values in your code:
message.text = resources.getString(R.string.hello)
message2.text = BuildConfig.hello2
The resValue
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 the parameters via command line, simply 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 for 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 snippets of code, files from Resources, among others. It would involve a little more work to interpret files, but it’s pretty powerful too.
At the end of the accounts would also be generated an apk with the assemble
, to be made available.
This approach can be used in conjunction with the Configuration via Gradle.
Has a very cool presentation by Heloisa Simon, where it tells how mobLee does to manage over 450 apps almost fully automated. They use a mix of the two approaches presented here.