打开Android Studio,创建空活动项目,关注gradle.build、AndroidManifest.xml、MainActivity.java和activity_main.xml四个文件。首先gradle环境没搭建好,需要在gradle.build中
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}前加上
buildscript{
repositories{
mavenCentral()
}
dependencies{
classpath 'com.android.tools.build:gradle:7.2.0'
}
},且编辑~/.gradle/gradle.properties文件,注释掉所有代理配置,之后Android Studio开始下载gradle依赖包并搭建项目运行所需的环境,在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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学习安卓,你准备好了吗?" />
<Button
android:id="@+id/bt_android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Welcome"
android:text="准备好了" />
</LinearLayout>在MainActivity.java增加Welcome函数的定义public void Welcome(View view){Toast.makeText(this,"欢迎来到安卓世界",Toast.LENGTH_SHORT).show();},创建虚拟设备运行项目,选择pixel2设备,下载Android11包,确认配置,“Next”按钮不可用直接回车创建。运行成功后,进入应用,点击按钮,弹出提示框
参考链接:解决错误:Plugin with id 'com.android.application' not found
新建项目下Gradle sync报错Plugin was not found in any of the following sources
Android Studio入门级教程(详细)【小白必看】