yangjunjie 5 kuukautta sitten
sitoutus
e03ec50080

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.gradle/
+.idea/
+**/build/

+ 34 - 0
README.md

@@ -0,0 +1,34 @@
+# Android Kotlin Demo
+
+This is a minimal Android project written in Kotlin and built with Gradle. It can be packaged into an APK once you have the Android SDK and Gradle installed.
+
+## Setup
+
+1. **Android SDK**
+   - Ensure you have the Android SDK installed and `sdk.dir` set in `local.properties`.
+
+2. **Gradle**
+   - Install Gradle on your system (https://gradle.org/install).
+   - Run `gradle wrapper` in the project root to generate the Gradle wrapper (`gradlew`).
+
+3. **Build APK**
+   - Use the wrapper or Gradle to assemble a debug APK:
+     ```sh
+     ./gradlew assembleDebug
+     ```
+     on Windows use `gradlew.bat`.
+
+   - The APK will be located at `app/build/outputs/apk/debug/app-debug.apk`.
+
+## Project Structure
+
+- `app/`: Android application module
+  - `src/main/java/com/example/myapp/MainActivity.kt` – main activity
+  - `src/main/res/layout/activity_main.xml` – layout file
+  - `build.gradle` – module build configuration
+
+- `build.gradle` – top-level build script
+- `settings.gradle` – includes `app` module
+- `gradlew`/`gradlew.bat` – placeholder wrapper scripts
+
+Feel free to expand this demo with your own features.

+ 56 - 0
app/build.gradle

@@ -0,0 +1,56 @@
+plugins {
+    id 'com.android.application'
+    id 'org.jetbrains.kotlin.android'
+}
+
+android {
+    namespace 'com.example.myapp' // AGP 8.0+ 强制要求命名空间
+
+    // 建议使用 34 或 35,AGP 9.0 支持最高 36
+    compileSdk = 35
+
+    defaultConfig {
+        applicationId "com.example.myapp"
+        minSdk = 21
+        targetSdk = 35 // 建议与 compileSdk 保持一致
+        versionCode 1
+        versionName "1.0"
+
+        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
+    }
+
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+        }
+    }
+
+    // 编译选项:AGP 9.0 + Gradle 9.1 推荐使用 Java 17 或 21
+    compileOptions {
+        sourceCompatibility = JavaVersion.VERSION_21
+        targetCompatibility = JavaVersion.VERSION_21
+    }
+
+    kotlinOptions {
+        jvmTarget = '21'
+    }
+
+    // 可选:启用构建特征预览 (如果需要使用新特性)
+    // buildFeatures {
+    //     viewBinding true
+    // }
+}
+
+dependencies {
+    // Kotlin 标准库通常由插件自动引入,无需显式声明,除非需要特定版本
+    // implementation "org.jetbrains.kotlin:kotlin-stdlib:2.0.21"
+
+    implementation 'androidx.core:core-ktx:1.15.0' // 使用较新版本以适配 SDK 35
+    implementation 'androidx.appcompat:appcompat:1.7.0'
+    implementation 'com.google.android.material:material:1.12.0'
+
+    testImplementation 'junit:junit:4.13.2'
+    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
+    androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
+}

+ 5 - 0
app/proguard-rules.pro

@@ -0,0 +1,5 @@
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to
+#     <sdk>/tools/proguard/proguard-android.txt
+# You can edit the include path and order by editing
+# the proguardFiles directive in build.gradle.

+ 20 - 0
app/src/main/AndroidManifest.xml

@@ -0,0 +1,20 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.example.myapp">
+
+    <application
+        android:allowBackup="true"
+        android:label="@string/app_name"
+        android:icon="@mipmap/ic_launcher"
+        android:roundIcon="@mipmap/ic_launcher_round"
+        android:supportsRtl="true"
+        android:theme="@style/Theme.MyApp">
+        <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>

+ 14 - 0
app/src/main/java/com/example/myapp/MainActivity.kt

@@ -0,0 +1,14 @@
+package com.example.myapp
+
+import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import android.widget.TextView
+
+class MainActivity : AppCompatActivity() {
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.activity_main)
+
+        findViewById<TextView>(R.id.textView).text = "Hello from Kotlin Demo"  
+    }
+}

+ 19 - 0
app/src/main/res/layout/activity_main.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".MainActivity">
+
+    <TextView
+        android:id="@+id/textView"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Hello World!"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 3 - 0
app/src/main/res/values/strings.xml

@@ -0,0 +1,3 @@
+<resources>
+    <string name="app_name">MyApp</string>
+</resources>

+ 5 - 0
app/src/main/res/values/styles.xml

@@ -0,0 +1,5 @@
+<resources>
+    <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
+        <!-- Customize your theme here. -->
+    </style>
+</resources>

+ 5 - 0
build.gradle

@@ -0,0 +1,5 @@
+// 根目录 build.gradle
+// 插件版本已在 settings.gradle 中管理,此处无需重复定义
+tasks.register('clean', Delete) {
+    delete rootProject.buildDir
+}

+ 1 - 0
gradle.properties

@@ -0,0 +1 @@
+# Project-wide Gradle settings

+ 12 - 0
gradle/gradle-daemon-jvm.properties

@@ -0,0 +1,12 @@
+#This file is generated by updateDaemonJvm
+toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/ec7520a1e057cd116f9544c42142a16b/redirect
+toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/4c4f879899012ff0a8b2e2117df03b0e/redirect
+toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/ec7520a1e057cd116f9544c42142a16b/redirect
+toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/4c4f879899012ff0a8b2e2117df03b0e/redirect
+toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/73bcfb608d1fde9fb62e462f834a3299/redirect
+toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/846ee0d876d26a26f37aa1ce8de73224/redirect
+toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/ec7520a1e057cd116f9544c42142a16b/redirect
+toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/4c4f879899012ff0a8b2e2117df03b0e/redirect
+toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/9482ddec596298c84656d31d16652665/redirect
+toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/39701d92e1756bb2f141eb67cd4c660e/redirect
+toolchainVersion=21

+ 8 - 0
gradle/wrapper/gradle-wrapper.properties

@@ -0,0 +1,8 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+# ?? Gradle 9.3.1 ????
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
+networkTimeout=10000
+validateDistributionUrl=true
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists

+ 4 - 0
gradlew

@@ -0,0 +1,4 @@
+#!/bin/sh
+# Placeholder gradlew script. Install Gradle and run "gradle wrapper" to generate actual wrapper.
+echo "Please install Gradle and run 'gradle wrapper' in this directory to generate wrapper scripts."
+exit 1

+ 4 - 0
gradlew.bat

@@ -0,0 +1,4 @@
+@echo off
+rem Placeholder Gradle wrapper script. Install Gradle and run "gradle wrapper" to generate actual wrapper.
+echo Please install Gradle and run "gradle wrapper" in this directory to generate wrapper scripts.
+exit /b 1

+ 8 - 0
local.properties

@@ -0,0 +1,8 @@
+## This file must *NOT* be checked into Version Control Systems,
+# as it contains information specific to your local configuration.
+#
+# Location of the SDK. This is only used by Gradle.
+# For customization when using a Version Control System, please read the
+# header note.
+#Thu Feb 26 16:48:04 CST 2026
+sdk.dir=D\:\\Programs\\Android\\Sdk

+ 36 - 0
settings.gradle

@@ -0,0 +1,36 @@
+pluginManagement {
+    repositories {
+        // 1. 阿里云 Google 插件镜像 (关键:解决 AGP 插件下载失败)
+        maven { url 'https://maven.aliyun.com/repository/google' }
+        // 2. 阿里云公共插件镜像 (关键:解决 Kotlin 等插件下载失败)
+        maven { url 'https://maven.aliyun.com/repository/public' }
+        // 3. Gradle 官方插件门户
+        gradlePluginPortal()
+
+        // 原有的 google() 和 mavenCentral() 可以保留,但建议放在后面作为备用
+        // google()
+        // mavenCentral()
+    }
+    plugins {
+        id 'com.android.application' version '9.0.1' apply false
+        id 'com.android.library' version '9.0.1' apply false
+        id 'org.jetbrains.kotlin.android' version '2.0.21' apply false
+    }
+}
+
+dependencyResolutionManagement {
+    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
+    repositories {
+        // 1. 阿里云 Google 内容镜像 (关键:解决 androidx 等依赖下载失败)
+        maven { url 'https://maven.aliyun.com/repository/google' }
+        // 2. 阿里云公共内容镜像
+        maven { url 'https://maven.aliyun.com/repository/public' }
+
+        // 原有的仓库放在后面
+        google()
+        mavenCentral()
+    }
+}
+
+rootProject.name = "android-demo"
+include ':app'