1. 程式人生 > >使用Android Studio遇到的一些常見問題總結

使用Android Studio遇到的一些常見問題總結

問題1:匯入SDK中的Samples後,Messages中提示“Error:Cause: failed to find target Android-19.0.1 Please install the missing platform from the Android SDK Manager.”

原因:AS安裝後,自帶的Build-tools版本是19.0.3,而示例是在19.0.1下建立的。

解決:1、使用SDK Manager把19.0.1裝上;2、修改project->app->build.gradle中的buildToolsVersion "19.0.1"改為buildToolsVersion "19.0.3"。

另外如果從網上下來的示例匯入後,一般需要修改compileSdkVersion為19。

2、或者使用你Android Studio裡面現有的Build-tools

問題2:安裝新版本JDK後,編譯時仍使用舊版本的JDK

解決:除了更新JAVA_HOME環境變數,在AS中調整File->Project Structure->SDK Location和File->Other Settings->Default Project Structure->SDK Location。

問題3:AS的程式碼編輯視窗中和執行時,中文顯示亂碼

解決:AS的程式碼編輯視窗中的亂碼,只需要把IDE右下角的UTF-8改為GBK;執行時顯示亂碼,1、在project->app->build.gradle中新增compileOptions.encoding = "GBK"。2、不能在佈局檔案中直接輸入中文,需要在R檔案中註冊下,比如<activity  android:label="中文" >改為<activity  android:label="@string/chinese" >,在strings.xml中新增<string name="chinese">中文</string>。

問題4:AS匯入包含jni設定的工程,編譯錯誤

解決:一般Eclipse工程,AS可以直接匯入。如果有問題,確認該工程在Eclipse中執行良好,匯出為AS工程後,再在AS中匯入。

問題5:MainActivity.Java中顯示R類路徑無效

解決:因為某些原因,AS沒有自動生成R檔案。比如專案依賴的庫檔案版本與指定的compileSdkVersion不符,這時需要手動指定需要編譯的庫檔案版本號。比如android.compileSdkVersion為19,那麼在project->app->build.gradle末尾新增

dependencies {

    compile 'com.android.support:support-v4:+' 改為 compile 'com.android.support:support-v4:19.+'
    compile 'com.android.support:appcompat-v7:+'改為compile 'com.android.support:appcompat-v7:19.+'
}

注意dependencies{}與android{}同級

問題6:Error running app: This version of Android Studio is incompatible with the Gradle Plugin used. Try disabling Instant Run (or updating either the IDE or the Gradle plugin to the latest version)

解決:

① 禁用Instant Run,在Settings/Preferneces > Build, Execution, Deployment option > Instant Run 中,取消所有的選中項


② 更新gradle services 的url:在project目錄下的 gradle>wrapper>gradle-wrapper.properties中將

distributionUrl=https://services.gradle.org/distributions/gradle-2.10-all.zip

更新為:

distributionUrl=https://services.gradle.org/distributions/gradle-2.11-all.zip

③ 刪除Project下面的build資料夾


④ clean專案,重新編譯跑程式