第一个工程,那必然是程序员都熟悉的HelloWorld
1 创建工程
File >> New >> New Project >> 选择空白模板(Empty Activity)
如果一个app想上线应用商店,包名可能被作为唯一识别表示(包名常常采用反域名命名规则)
根据个人的开发习惯,可以将"android目录"展现形式切换为"Project目录"展现形式
gradle.properties中添加如下代码:
android.enableJetifier=true
2 文件目录介绍
(我们开发者主要关注app下的文件即可)
res文件夹下,drawable(图片),layout(布局),values(颜色,字符,主题),mipmap(应用图标,为了兼容不同设备,因此有很多文件夹)
AndroidManifest.xml整个Android项目配置文件,在程序中定义的所有四大组件(Activity组件,service组件,content provider组件,broadcast receiver组件)都需要在这里注册,另外还可以在这个文件中给应用程序添加权限声明
3 运行流程
App在AndroidManifest.xml找到注册的Activity组件,通过Activity.java进行相关的逻辑操作,向用户展示引入的activity.xml内容
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="xyz.aboluo">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Hello_world"
tools:targetApi="31">
<activity
android:name=".AbcActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Hello_world">
<!--设置此Activity为启动页-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AbcActivity.java 所有的Activity都要在AndroidManifest中注册
package xyz.aboluo;
import android.app.Activity;
import android.os.Bundle;
public class AbcActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="xyz.aboluo.AbcActivity">
<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" />
</LinearLayout>
build.gradle(:app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'xyz.aboluo'
compileSdk 33
defaultConfig {
applicationId "xyz.aboluo"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.5.1'
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
implementation platform('androidx.compose:compose-bom:2022.10.00')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
}
运行APP