Last active
August 31, 2024 14:05
-
-
Save FrankSpierings/cca4c16f6d191b473077ced480eb20d2 to your computer and use it in GitHub Desktop.
Create an Android application on the command line, without Android Studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From the current working directory... | |
mkdir -p app/src/main/res/values | |
mkdir -p app/src/main/res/layout | |
mkdir -p app/src/main/java/com/example/helloworld | |
cat > app/src/main/AndroidManifest.xml << _EOF | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.helloworld"> | |
<application | |
android:label="Hello World" | |
android:theme="@style/AppTheme"> <!-- Use the AppCompat theme here --> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> | |
_EOF | |
cat > app/src/main/java/com/example/helloworld/MainActivity.java << _EOF | |
package com.example.helloworld; | |
import android.os.Bundle; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
TextView textView = new TextView(this); | |
textView.setText("Hello, World!"); | |
setContentView(textView); | |
} | |
} | |
_EOF | |
cat > app/src/main/res/values/styles.xml << _EOF | |
<resources> | |
<!-- Base application theme --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<!-- Customize your theme here --> | |
</style> | |
</resources> | |
_EOF | |
cat > build.gradle << _EOF | |
buildscript { | |
repositories { | |
google() | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:8.0.0' | |
} | |
} | |
_EOF | |
cat > settings.gradle << _EOF | |
pluginManagement { | |
/** | |
* The pluginManagement.repositories block configures the | |
* repositories Gradle uses to search or download the Gradle plugins and | |
* their transitive dependencies. Gradle pre-configures support for remote | |
* repositories such as JCenter, Maven Central, and Ivy. You can also use | |
* local repositories or define your own remote repositories. The code below | |
* defines the Gradle Plugin Portal, Google's Maven repository, | |
* and the Maven Central Repository as the repositories Gradle should use to look for its | |
* dependencies. | |
*/ | |
repositories { | |
gradlePluginPortal() | |
google() | |
mavenCentral() | |
} | |
} | |
dependencyResolutionManagement { | |
/** | |
* The dependencyResolutionManagement.repositories | |
* block is where you configure the repositories and dependencies used by | |
* all modules in your project, such as libraries that you are using to | |
* create your application. However, you should configure module-specific | |
* dependencies in each module-level build.gradle file. For new projects, | |
* Android Studio includes Google's Maven repository and the Maven Central | |
* Repository by default, but it does not configure any dependencies (unless | |
* you select a template that requires some). | |
*/ | |
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) | |
repositories { | |
google() | |
mavenCentral() | |
} | |
} | |
rootProject.name = "android" | |
include ':app' | |
_EOF | |
cat > app/build.gradle << _EOF | |
apply plugin: 'com.android.application' | |
android { | |
namespace 'com.example.helloworld' | |
compileSdkVersion 30 | |
defaultConfig { | |
applicationId "com.example.helloworld" | |
minSdkVersion 21 | |
targetSdkVersion 30 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
implementation 'androidx.appcompat:appcompat:1.3.1' | |
} | |
_EOF | |
cat > gradle.properties << _EOF | |
android.useAndroidX=true | |
_EOF | |
# Install the sdk using the command line tools (sdkmanager) | |
echo y | sdkmanager "platforms;android-30" --sdk_root=/tmp/data/sdk | |
# Stupid license accepts.. | |
printf "y\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\n"| sdkmanager --licenses --sdk_root=/tmp/data/sdk | |
# Define the SDK location within gradle | |
cat > local.properties << _EOF | |
sdk.dir=/tmp/data/sdk | |
_EOF | |
# Initialize the wrapper | |
gradle wrapper | |
./gradlew assembleDebug | |
# Install on the device | |
adb install ./app/build/outputs/apk/debug/app-debug.apk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment