目前學習用Android Studio進行android手機開發,出現了各種問題,出現問題后,搜索百度,有的一個問題,各種答案,通過多次試錯排查,終於總結出了一套可以完全使用的方法了。具體的步驟如下:
1. 首先下載最新安裝版Android Studio Arctic Fox,安裝好后,會下載SDK,由於"牆"的存在,致使容易出現下載緩慢或者根本無法下載的問題,這時就需要進行設置了,看百度上多是設置proxy,但是這種方法,也不太可靠,我試了很多代理服務器都失效了,最后才找到一種目前看來較為穩妥的方法,就是使用http://ping.chinaz.com/ ,來ping谷歌服務器,看其中最短時間的網址,並加入hosts文件中,hosts文件地址:C:\WINDOWS\System32\drivers\etc\hosts,如
2. 在Android Studio中,新建hello world工程
3. 修改配置文件
修改gradle-wrapper.properties文件,android studio采用gradle來編譯,而gradle從外圍難以訪問,所以可以從https://services.gradle.org/distributions/中下載gradle-7.1-all.zip到本地如C:/Users/Administrator/.gradle/wrapper/dists/gradle-7.1-all/9ccjyfn4kvk7v1w1wbmf8rhqp/gradle-7.1-all.zip,
其中9ccjyfn4kvk7v1w1wbmf8rhqp是系統自動生成的
並且在setting的Build Tools下的Gradle設置為下圖所示:
distriutionUrl項改為
distributionUrl=file:///C:/Users/Administrator/.gradle/wrapper/dists/gradle-7.1-all/9ccjyfn4kvk7v1w1wbmf8rhqp/gradle-7.1-all.zip
4.build.grale模塊級的設置為
plugins { id 'com.android.application' } android { compileSdk 30 defaultConfig { applicationId "com.example.test1" minSdk 21 targetSdk 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } // compileOptions { // sourceCompatibility JavaVersion.VERSION_1_8 // targetCompatibility JavaVersion.VERSION_1_8 // } } dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
其中的compileOptions一定要去掉,因為compileSdk設置為30,所以在SDK中也應下載API為30版本的,並且要制定SDK的路徑,如:
5. build.gradle工程級的設置為:
使用阿里雲
buildscript { repositories { google() //mavenCentral() maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'} } dependencies { classpath 'com.android.tools.build:gradle:4.2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() //mavenCentral() maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'} } }
6.在settings.gradle(Project Settings)中去掉前面的設置,改為
//dependencyResolutionManagement { // repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) // repositories { // google() // mavenCentral() // jcenter() // Warning: this repository is going to shut down soon // } //} rootProject.name = "Test1" include ':app'
7.所有這設置后,就應該可以編譯和調試android程序了
如果使用模擬器調試,需要BIOS打開VT-X支持,並在SDK TOOL中下載HAXM,然后調試。
筆者參照網上的例子。activity_main.xml:
<?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="300dp" android:background="#00aaff" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:background="#ffff99" android:padding="60dp"> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
運行后,手機模擬器效果: