0

I am trying to write a minimal script to set up android SDK environment and build a minimal app.

This is what I've got, but it's failing with a gradle problem which I am unable to solve

FAILURE: Build failed with an exception.

* Where:
Build file '/home/user/my-app/build.gradle' line: 2

* What went wrong:
Plugin [id: 'com.android.application', version: '8.1.1', apply: false] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.android.application:com.android.application.gradle.plugin:8.1.1')
  Searched in the following repositories:
    Gradle Central Plugin Repository

The code is as follows

#!/bin/bash
#
#  ./create-minimal.sh app-name
#

set -e

APP_NAME="$1"
APP_DIR="$PWD/$APP_NAME"
ANDROID_SDK_ROOT="$APP_NAME/android-sdk"
ANDROID_CMD_TOOLS="$PWD/$ANDROID_SDK_ROOT/cmdline-tools/latest"
GRADLE_VERSION="8.5"
ANDROID_BUILD_TOOLS="34.0.0"
ANDROID_PLATFORM="android-34"

echo "==> Installing dependencies..."
mkdir -p "$ANDROID_SDK_ROOT"
cd "$ANDROID_SDK_ROOT"

# Download Android Command Line Tools
if [ ! -d "cmdline-tools" ]; then
    wget https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip -O cmdline-tools.zip
    mkdir -p cmdline-tools/latest
    mkdir -p cmdline-tools/rmme
    unzip cmdline-tools.zip -d cmdline-tools/rmme
    mv cmdline-tools/rmme/*/* cmdline-tools/latest
    rm -rf cmdline-tools.zip cmdline-tools/rmme
fi

export PATH="$ANDROID_CMD_TOOLS/bin:$ANDROID_SDK_ROOT/platform-tools:$PATH"
export ANDROID_HOME="$ANDROID_SDK_ROOT"

echo "==> Installing Android SDK components..."
yes | sdkmanager --sdk_root=$ANDROID_SDK_ROOT --licenses
sdkmanager --sdk_root=$ANDROID_SDK_ROOT "platform-tools" "platforms;$ANDROID_PLATFORM" "build-tools;$ANDROID_BUILD_TOOLS"

if [ ! -d "gradle" ]; then
    echo "==> Installing Gradle..."
    wget https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip -O gradle.zip
    unzip gradle.zip
    mv gradle-${GRADLE_VERSION} gradle
    rm gradle.zip
fi
export PATH="$PWD/gradle/bin:$PATH"

echo "==> Creating Android project..."
cd "$APP_DIR"

gradle init --type basic

# ✅ Fix: Proper Android project structure
mkdir -p app/src/main/java/com/example/$APP_NAME
mkdir -p app/src/main/res/layout

# ✅ Fix: Add a proper root `build.gradle`
cat > build.gradle <<EOF
plugins {
    id 'com.android.application' version '8.1.1' apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
subprojects {
    afterEvaluate { project ->
        if (project.hasProperty("android")) {
            project.android.compileSdkVersion = 34
        }
    }
}
EOF

# ✅ Fix: Add `settings.gradle`
cat > settings.gradle <<EOF
rootProject.name = '$APP_NAME'
include ':app'
EOF

# ✅ Fix: Create `app/build.gradle`
mkdir -p app
cat > app/build.gradle <<EOF
plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.$APP_NAME'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.$APP_NAME"
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
}
EOF

# ✅ Fix: Create `AndroidManifest.xml`
cat > app/src/main/AndroidManifest.xml <<EOF
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.$APP_NAME">
    <application
        android:allowBackup="true"
        android:label="Minimal App"
        android:theme="@style/Theme.MaterialComponents.DayNight">
        <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

# ✅ Fix: Create `MainActivity.java`
cat > app/src/main/java/com/example/$APP_NAME/MainActivity.java <<EOF
package com.example.$APP_NAME;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(android.R.layout.simple_list_item_1);
    }
}
EOF

echo "==> Running Gradle sync..."
gradle wrapper

echo "==> Building the project..."
./gradlew build

echo "✅ Android project setup complete!"
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.