Android Studio 導入問題總結-IT藍豹

Android Studio報錯--Error: Library projects cannot set applicationId. applicationI

 

今天在導入一個Android Studio 工程時,發生了這樣的錯誤:

Error: Library projects cannot set applicationId. applicationId is set to "com.du.android.recyclerview" in default config

我是很苦惱啊,整了一晚上纔算明白,原來導入一個庫/工程不是那麼容易的.出現這個錯誤的原因在於工程下有個庫,庫的build.gradle裏面defaultConfig內存在:

defaultConfig {
applicationId "com.du.android.recyclerview"
minSdkVersion 21
targetSdkVersion 21
versionCode 1
versionName "1.0"

}

紅色部分的字體,這時刪除紅色標註的內容即可!

 

 

問題一:導入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。

 

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

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

 

問題三: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>。

 

問題四:AS導入包含jni設置的工程,編譯錯誤

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

 

問題五: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{}同級

 

 

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {
jcenter()
}


dependencies {
classpath
'com.android.tools.build:gradle:1.0.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章