Android Studio 3.1.X 導入項目的正確姿勢

2021-02-13 Android編程精選
點擊上方「Android編程精選」,選擇「置頂公眾號」

關鍵時刻,第一時間送達!

使用Android Studio 3.1.2導入以前的項目遇到一些坑,藉此機會把相關處理方法分享出來。

下面以導入Android Studio2.3.3項目為例:
在此之前先建議你用Android Studio 3.1.2創建一個新的項目,看看有哪些變化,這對你很有幫助。

修改appuild:gradle

修改compileSdkVersion和buildToolsVersion

修改前:

 compileSdkVersion 23
buildToolsVersion '25.0.0'

修改後:

compileSdkVersion 27
buildToolsVersion '27.0.3'

其中buildToolsVersion 是在Android Studio 3.0之後取消了,你可以保留也可以注釋掉,在defaultConfig方法中將targetSdkVersion 為27並增加一下代碼。

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

修改依賴關鍵字

compile(implementation/api),provided(compileOnly),apk(runtimeOnly)

修改前:

 dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile name: 'SMSSDK-3.0.0', ext: 'aar'
    compile name: 'SMSSDKGUI-3.0.0', ext: 'aar'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.youth.banner:banner:1.4.9'
    compile 'com.facebook.fresco:fresco:0.13.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.foamtrace:photopicker:1.0'
    compile 'com.github.chrisbanes.photoview:library:1.2.4'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile project(':ZXingAndroid')
    compile 'com.google.zxing:core:3.3.1'
}

修改後:

dependencies {

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation name: 'SMSSDK-3.0.0', ext: 'aar'
    implementation name: 'SMSSDKGUI-3.0.0', ext: 'aar'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.youth.banner:banner:1.4.9'
    implementation 'com.facebook.fresco:fresco:0.13.0'
    implementation 'com.squareup.okhttp3:okhttp:3.4.1'
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.foamtrace:photopicker:1.0'
    implementation 'com.github.chrisbanes.photoview:library:1.2.4'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.zxing:core:3.3.1'
}

修改項目下的XXX(項目名)uild:gradle
修改前:

buildscript {
    repositories {
    jcenter()
} dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'

    
    
   }
}

allprojects {
    repositories {
        jcenter()
        maven {url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

修改後:

buildscript {
    repositories {
         google()
         jcenter()
    }
    dependencies {
         classpath 'com.android.tools.buiwld:gradle:3.1.3'

         
         
    }
}
allprojects {
    repositories {
         google()
         jcenter()
         maven { url "https://jitpack.io" }
    }
}
task clean(type: Delete) {
     delete rootProject.buildDir
}

repositories方法中都增加了google(),build:gradle改和當前AS版本號一致。
修改gradle-wrapper.properties
修改前:


distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-3.3-all.zip

修改後:


distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-4.4-all.zip

主要修改了:

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

這裡告一段落,上面都改好之後同步項目(sync)。我為什麼建議你把上面的都改好之後再同步,這樣省事兒,剛開始的時候我也是改一點同步一下,問題多且很浪費時間,如果其中有些問題沒能解決就容易走偏。
如果報錯:

Error: java.util.concurrent.ExecutionException:
com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details

修改gradle.properties,增加如下代碼

android.enableAapt2=false

添加android.enableAapt2=false報如下錯誤請移步Android Studio 3.0後出現AAPT2和「android.enableAapt2」問題以有解決方法


如果有這個錯誤:這需要更新SDK,點擊藍色文字下載就好。

【點擊成為Java大神】

相關焦點

  • Android Studio中文版常見問題解決方法
    需要運行起來,studio才能連接到手機,點擊圖標,手機屏幕就會顯示「adb connect 192.168.0.x:5555」的字樣。到這裡手機設置就結束了。電腦部分1、下載好android studio 、SDK以及JDK,安裝好。
  • 機友分享 | 導入機智雲Android開源項目的正確姿勢
    因此只要我們將源碼工程文件成功導入Android Studio,那麼我們便可以自由定製我們的應用程式,那麼如何正確導入到Android Studio中,編譯成功並在真機上運行呢?在機智雲官網,我們定義好數據點後,在服務->應用開發子菜單中,下載工程文件。
  • Android Studio 4.1 發布啦
    Android Studio 4.1 主要是包含了各種新功能和改進,其中 Android Gradle 插件也升級為 4.1.0,要了解更多信息請查看完整的 Android Gradle 插件發行說明:https://developer.android.com/studio/releases/gradle-plugin#4-1-0
  • Android Studio3.5及使用AndroidX的一些坑
    那麼我們還是把它添加進去,注意:studio3.5後添加包會有點不一樣右鍵你的項目,選中Open Module Setting在打開的界面,選中Dependencies,點擊裡面的加號,繼續選擇Library Dependency在搜索框裡,搜索design。
  • Android Studio 導入 AOSP 源碼
    2018-04-17 OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode).
  • Android Studio 3.4 穩定版發布
    (給安卓開發精選加星標)轉自:oschinahttps://www.oschina.net/news/106095/android-studio
  • WebRTC導入Android Studio
    在WebRTC的src/example目錄下有很多的關於WebRTC的demo,那麼如何將這些demo導入到Android Studio中進行分析呢?本文來為你揭曉...1、編譯# 配置輸出目錄和cpu架構 將產物輸出到out/StudioDebug目錄gn gen out/StudioDebug --args='target_os="android" target_cpu="arm"'# 編譯ninja -C out/StudioDebug AppRTCMobile
  • 在Android Studio上設置OpenCV Android庫的初學者指南
    截至撰寫本文時,最新版本為3.4.1。(https://sourceforge.net/projects/opencvlibrary/files/opencv-android/)第3步:導入OpenCV模塊成功創建Android項目後,是時候將OpenCV模塊導入Android項目了。
  • 實用教程|Android Studio安裝和設置
    下載地址·  Cocos2d-x  v3.15 (4.14論壇發布社區版本,4.21官網發布正式版本)http://www.cocos.com/download·  Android Studio 2.3 https://developer.android.com
  • Android Studio 4.1 新特性詳解
    更多信息見 :https://d.android.com/studio/inspect/database更多信息見:https://developer.android.com/studio/releases#dagger-navigation
  • Android Studio 4.1 發布,全方位提升開發體驗
    https://developer.android.google.cn/studiohttps://developer.android.google.cn/jetpackAndroid Studio 4.1 的一些亮點如下: 引入全新的 Database Inspector,用於查詢應用的資料庫;支持瀏覽使用 Dagger 或 Hilt 進行依賴項注入的項目;支持在 Android 項目中使用
  • Android Studio Bumblebee (2021.1.1)穩定版發布
    簡介經過很多版本的更新,Android Studio Bumblebee (2021.1.1)(還包括了Android Gradle plugin (AGP) 7.1.0)在昨天的晚些時候已經可以在穩定渠道上進行更新使用了。Android Studio Bumblebee (2021.1.1)的主要更新了以下功能:構建和部署、分析和檢查以及設計。
  • Android Studio使用大全
    Paste_Image.pnghttps://meedamian.com/post/deuglifying-android-studio/?hi3、關閉拼寫檢查忽略完文件後,我們進行項目同SVN的關聯,選擇VCS->Import into Version Control->Share Project(Subversion);這裡說明一點,在Import into Version Control下有Import into Subversion和Share Project(Subversion)兩個選項【在studio 1.3
  • Android studio 3.4 穩定版來了
    下載地址 >>> https://developer.android.com/studio/#downloadsIntelliJ IDEA 2018.3.4:Android Studio 3.4 也已升級至 IntelliJ IDEA 2018.3.4 平臺,並帶來同樣的改進升級至 Android Gradle plugin
  • Android Studio 3.3更新了
    1月14號,Google 發布Android Studio 3.3 release 穩定版連結地址:https://developer.android.google.cn/studio/Google blog:https://android-developers.googleblog.com/2019/01/android-studio-33.
  • Android下玩JNI的新老三種姿勢
    馬北劍西的博客地址:http://blog.csdn.net/mabeijianxihttps://github.com/mabeijianxi說明:本篇不擼代碼,只玩編譯,其包含了Android studio 2.2最新的JNI玩法編譯環境:macOS 10.12.3工具包含:Android Studio
  • Unity導出Android Studio工程
    在出遊戲包的時候,如果需要接入sdk,一般都不會直接在unity裡操作,而是導出android工程,在android工程裡操作,從unity5.3
  • Android Studio的下載與安裝
    Google 已經在 2015 年結束了對 Eclipse Android 開發工具的支持Android Studio 下載選擇從 Android Studio 中文組 官網進行下載:http://www.android-studio.org/如果電腦網路好,能翻牆的,可以直接從官網首頁推薦的 google 正版連結進行下載,如:android-studio-ide
  • Android Studio 3.4 穩定版發布,新特性全總結!
    轉自:oschinahttps://www.oschina.net/news/106095/android-studio-3-4-releasedAndroid Studio 3.4 穩定版發布了。除許多性能改進和錯誤修復之外,此版本還增添了一些新功能。
  • Android Studio打包apk,aar,jar包
    一片楓葉_劉超的博客地址:http://blog.csdn.net/qq_23547831作者編寫了github項目解析、android源碼分析以及產品研發多個專題,有興趣的可以關注下學習學習~文本我們將講解android studio打包apk,aar,jar包的相關知識。