您可以使用 專案匯入器 在 Android Studio IDE 中開啟此範例。

此範例說明如何使用 Gradle 建置以 Java 編寫的簡單 Android 應用程式。此應用程式是按照 建立您的第一個應用程式指南 建立的。

app/build.gradle.kts
plugins {
    id("com.android.application") version "7.3.0"
}

repositories {
    google()
    mavenCentral()
}

android {
    compileSdkVersion(30)
    defaultConfig {
        applicationId = "org.gradle.samples"
        minSdkVersion(16)
        targetSdkVersion(30)
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}

dependencies {
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("com.google.android.material:material:1.2.0")
    implementation("androidx.constraintlayout:constraintlayout:2.0.4")
    testImplementation("junit:junit:4.13.1")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}
app/build.gradle
plugins {
    id('com.android.application') version '7.3.0'
}

repositories {
    google()
    mavenCentral()
}

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId 'org.gradle.samples'
        minSdkVersion 16
        targetSdkVersion 30
        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.2.0'
    implementation 'com.google.android.material:material:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

建置應用程式

$ ./gradlew build

如需更多資訊,建議您閱讀 Gradle 入門。您也可以在 Android 團隊提供的指南中找到 與 Android 開發相關的資訊