Build a minimal Android project structure from scratch, supporting Kotlin/Java, Gradle Kotlin DSL (recommended), or Groovy, containing only one main Activity and the necessary resources.
Choose before creating
| Option | Recommended | Description |
|---|---|---|
| Language | Kotlin | First choice for new projects; if Java is chosen, use the Java template below |
| Gradle | Kotlin DSL(.kts) | Default for new projects; for Groovy see reference.md |
Directory structure (Kotlin + Kotlin DSL)
Create the project root directory at the target location (e.g. MyApp/), then create the files according to the following structure:
MyApp/
├── settings.gradle.kts
├── build.gradle.kts
├── gradle.properties
├── gradle/
│ └── wrapper/
│ ├── gradle-wrapper.properties
│ └── gradle-wrapper.jar
├── app/
│ ├── build.gradle.kts
│ ├── src/
│ │ └── main/
│ │ ├── AndroidManifest.xml
│ │ ├── java/com/example/myapp/
│ │ │ └── MainActivity.kt
│ │ └── res/
│ │ └── values/
│ │ └── strings.xml
│ └── proguard-rules.pro
├── gradlew
└── gradlew.bat
gradle-wrapper.jar Can be copied from an existing Android project, or use gradle wrapper to generate. Only the required source code and configuration are given below.
Root directory configuration
settings.gradle.kts
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "MyApp"
include(":app")
build.gradle.kts (root)
plugins {
id("com.android.application") version "8.2.0" apply false
id("org.jetbrains.kotlin.android") version "1.9.20" apply false
}
Version numbers can be upgraded as needed, keeping them compatible with AGP and Kotlin.
gradle.properties
android.useAndroidX=true
kotlin.code.style=official
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
app module
app/build.gradle.kts
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.myapp"
compileSdk = 34
defaultConfig {
applicationId = "com.example.myapp"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
}
app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@android:drawable/sym_def_app_icon"
android:label="@string/app_name"
android:roundIcon="@android:drawable/sym_def_app_icon"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Note: When using AppCompatActivity , the theme must use the Theme.AppCompat series, and system themes such as Theme.Material.Light.NoActionBar。
app/src/main/java/com/example/myapp/MainActivity.kt
The package name is consistent with namespaceand the directory path.
package com.example.myapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(android.R.layout.simple_list_item_1)
}
}
If you need a custom layout: define it in res/layout/activity_main.xml ,setContentView(R.layout.activity_main)。
app/src/main/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">我的应用</string>
</resources>
app/src/main/res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#3F51B5</item>
<item name="colorPrimaryDark">#303F9F</item>
<item name="colorAccent">#FF4081</item>
</style>
</resources>
Description:
Theme.AppCompat.Light.NoActionBar: Light theme, no ActionBarcolorPrimary: Primary colorcolorPrimaryDark: Status bar colorcolorAccent: Accent color- In AndroidManifest.xml use
android:theme="@style/Theme.MyApp"to reference
app/proguard-rules.pro
Can be left empty or keep only comments:
# Add project specific ProGuard rules here.
Java version (optional)
If you choose Java:
app/build.gradle.ktsremoveid("org.jetbrains.kotlin.android"), do not configurekotlinOptions。- change
MainActivity.kttoMainActivity.java, with the package name consistent with the directory:
package com.example.myapp;
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);
}
}
- Root
build.gradle.ktsthe Kotlin plugin line can be removed.
Gradle Wrapper
If there is no gradlew / gradle/wrapper:
- Copy from another Android project
gradle/wrapper/、gradlew、gradlew.bat, or - run in the project root directory:
gradle wrapper --gradle-version 8.2(Gradle must already be installed on the machine).
gradle/wrapper/gradle-wrapper.properties Example:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Running and verification
- Open the project root directory with Android Studio (select the one containing
build.gradle.ktsdirectory). - Sync Gradle (Sync Project with Gradle Files).
- Connect a device or start an emulator, run
app。
Command-line build:
./gradlew assembleDebug
APK output:app/build/outputs/apk/debug/app-debug.apk。
Additional notes
- Groovy version
build.gradleFor templates, custom themes and layouts, permissions and multi-module instructions see reference.md。 - When integrating SDKs such as maps/location, add dependencies in
app/build.gradle.kts, and inAndroidManifest.xmldeclare the required permissions and components.