One day in April, I landed a freelance gig to batch-generate Android APK packages. The requirement was that upon opening the app, it would display different background images to showcase the main character’s formidable professional capabilities.
Since I had never written native Android before—at most using React Native for cross-platform apps—manually creating a bunch of projects and modifying them by hand would be pure manual labor, which was definitely out of the question.
LLM First
Lately, I’ve been using LLMs to solve repetitive tasks, so I directly asked an LLM how to write this thing.
Although GPT-4 wrote a lot of Bash script content, errors still popped up in the minor details, so I fed the error messages back to the LLM to resolve them.
However, keep in mind that longer conversations aren’t necessarily better. Sometimes, when the raw error messages and code get too long, severe hallucinations occur. Therefore, after writing a bit, running a bit, and verifying a bit, I cleared the context and asked anew.
Learning Android and Bash Together
Finally, after relentless debugging by the LLM and human effort (just about 2 hours), a runnable Bash script was produced:
# Inside the loop, setting PACKAGE_NAME and PROJECT_NAME
# This goes in the project-level build.gradle creation step cat > "./$PROJECT_NAME/build.gradle" <<EOF buildscript { repositories { google() mavenCentral() } dependencies { // Make sure to use a compatible version classpath 'com.android.tools.build:gradle:7.4.2' } }
echo"Project $PROJECT_NAME has been created and packaged." done
echo"Finished creating and packaging $PROJECTS_COUNT projects."
Actually, I haven’t written scripts with Bash in a long time, so I used this project as an opportunity to learn~
1
#!/bin/bash
The first line specifies the location of the bash interpreter, which is generally read when executing ./xxx.sh.
1
PROJECTS_COUNT=1
The second line sets a variable to specify the number of projects to generate.
1 2 3
for i in $(seq 1 $PROJECTS_COUNT); do ………… done
Next, a for statement is written. seq 1 \$PROJECTS_COUNT generates a sequence from 1 to PROJECTS_COUNT. The $ symbol in front packages the expression inside the parentheses into a variable, and finally, the specific logic is written between do and done.
Since each package name cannot be repeated, openssl rand -hex 3 is used to generate a string of random characters, and the result is appended directly to the end of the string.
Android or Java projects generally need to follow fixed project directory conventions, so mkdir -p is required in advance to directly create the directory hierarchy. After creation, project information is written into the settings.gradle project configuration.
# This goes in the project-level build.gradle creation step cat > "./$PROJECT_NAME/build.gradle" <<EOF buildscript { repositories { google() mavenCentral() } dependencies { // Make sure to use a compatible version classpath 'com.android.tools.build:gradle:7.4.2' } }
The submodule’s build.gradle configures the Android compile version and dependency information; a simple application like this won’t depend on too much.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 初始化Gradle Wrapper cd"$PROJECT_NAME" gradle wrapper --gradle-version=7.5 cd ..
# 构建项目 cd"$PROJECT_NAME" ./gradlew assembleDebug cd ..
echo"Project $PROJECT_NAME has been created and packaged."
Finally, enter the directory, use Gradle to package and build the project, and copy the APK to the Bash execution directory for convenient collection, upload, and deployment later.
At this point, a Bash script capable of batch-generating Android APK packages was complete. By scripting it, I had LLMs and machines working for me, saving a ton of time.
To outsiders, the effort and scale look huge, so you can ask for a higher price. Tool automation really gives side hustles a major boost.