1. 程式人生 > >Android studio 2.3 遷移專案到 3.0、3.1

Android studio 2.3 遷移專案到 3.0、3.1

記錄了將公司大專案,完整從Android Studio 2.3遷移到 Android Studio 3.0的全部過程。
按照步驟遷移,遇到的所有報錯和解決辦法都在後面。

Android專案從2.3遷移到3.0、3.1

版本:2018/8/14-1(1456)

遷移到AS3.0

0-下載Android Studio 3.0

  • 需要在網站中的”下載選項”中,選擇android-studio-ide-171.4443003-windows.zip下載。

注意!下載無SDK的壓縮包,而不是exe檔案。因為通過解壓縮的方式,可以讓Android Studio 3.0和Android Studio 2.3共存。

  • 本地解壓縮zip壓縮包,在as目錄下的bin中studio64就是執行的exe檔案,可以建立快捷方式放置於桌面。

1-gradle下載4.1

請去官網或搜尋下載

  1. gradle-wrapper.properties
    -將gradle-2.10-all.zip更改為gradle-4.1-all.zip

  2. AS的settings中搜索gradle
    -在use local gradle中設定本地gradle的路徑,例如:D:/gradle-4.1-all/gradle-4.1

2-Build tools 下載 26.0.2

請去官網或搜尋下載

  1. 將下載的26.0.2的tools資料夾新增到本地sdk目錄中的build-tools內,例如“D:\android-sdk\build-tools”

  2. 公司網路限制下的備選方法:經測試在as3.0裡面的SDK manager可以直接更新,需要在settings->proxy->選擇’auto-detect proxy’,之後就可以在SDK Manager中更新需要API/Build-Tools等。

3-SDK API26(android 8.0)

請去官網或搜尋下載

  1. 將下載的android-26新增到本地SDK目錄中的platforms目錄下

  2. SDKManager中直接更新

4-更改build.gradle裡面的設定

dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
}

需要將2.3更改為3.0.1(as3的版本):

dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
}

5-gradle報錯處理

1-gradle打包,自定義apk名稱

報錯:
Error:(58, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=AHMobileReleaseUnsigned, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
<a href="openFile:E:\Projects\AS3.0projects\v2.x\src\mobile\iptvclient\build.gradle">Open File</a>

原內容如下:

applicationVariants.all { variant ->
  variant.outputs.each { output ->
    def outputFile = output.outputFile
    if (outputFile != null && outputFile.name.endsWith('.apk')) {
      //這裡修改apk檔名
      def fileName = "${variant.productFlavors[0].name}.apk"
      output.outputFile = new File(outputFile.parent, fileName)
    }
  }
}

處理辦法(直接用下列程式碼替換):

android.applicationVariants.all { variant ->
    variant.outputs.all {
      outputFileName = "${variant.productFlavors[0].name}.apk"
    }
  }

${variant.productFlavors[0].name}.apk就是之前寫法中def fileName =後面的內容。

2-Build Tools需要使用26.0.2或者更高版本

報錯:

Android SDK Build Tools 26.0.2 will be used.
To suppress this warning, remove "buildToolsVersion '25.0.0'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.

直接選擇下方的內容:

Update Build Tools version and sync project

或者直接在所有build.gradle中,將 buildToolsVersion後面更新為'26.0.2'
結果:buildToolsVersion '26.0.2'

3-維度統一

報錯:
Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

首先,在專案build.gradle的defaultConfig新增內容:

defaultConfig {
    ...省略...
    //最後新增如下內容
    flavorDimensions "versionCode"
  }

其次,在配置編譯時需要編譯的版本的配置中新增內容:

productFlavors {
    AHMobile {
      applicationId "com.ahmobile.android.tvclient" //com.zte.iptvclient.android.ahmobile
      signingConfig signingConfigs.ahmobile_release
      versionCode 11
      versionName "1.1.2.3"
      manifestPlaceholders = [APP_MARKET_VALUE: "Https-market-Android"]

   //新增下列兩行內容
      dimension "versionCode"
      matchingFallbacks = ['versionCode']
    }
    ...
}
分析:

專案中用了多渠道,3.0之前配置多渠道:productFlavors配置不同的渠道包,3.0 新增了flavorDimensions的配置。
報錯的大致原因是:
Android Plugin3.0的依賴機制:在使用library時會自動匹配variant(debug, release),就是說app的debug會自動匹配library的debug,相信大多數人也像我一樣,當library多了,不會手動選擇每個Library的variant。現在好了,它會自動匹配了。同樣如果使用flavor的時候,比如app的redDebug同樣會自動匹配library的readDebug。雖然有這樣的優勢,但是在使用flavor時,必須定義flavor dimension,否則會提示錯誤

4-Unable to resolve dependency

Error:Unable to resolve dependency for ':mobile:[email protected]/compileClasspath': Could not resolve project :uisdk:baseclient.
<a href="openFile:E:/Projects/AS3.0projects/v2.x/src/mobile/iptvclient/build.gradle">Open File</a><br><a href="Unable to resolve dependency for &#39;:mobile:[email protected]/compileClasspath&#39;: Could not resolve project :uisdk:baseclient.">Show Details</a>

build.gradle的buildTypes內修改如下:

buildTypes {
    release {
      minifyEnabled true
      shrinkResources true
      zipAlignEnabled true
      proguardFiles 'proguard.cfg'

/*
 *   因為只有release,沒有debug,新增如下內容
 *   如果有debug內容,需要在其中新增:matchingFallbacks = ['release']
 */
      matchingFallbacks = ['debug']
    }

    releaseUnsigned.initWith(buildTypes.release)
    releaseUnsigned {
      signingConfig null
    }
}

原因:
local與debug不相容。原因是在主專案中有一個變種local,而在library中是沒有對應的。

5-AAPT2 編譯報錯 AAPT2 error

報錯
Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

解決:在gradle.properties中關閉APPT2 編譯

android.enableAapt2=false

注:如果是eclipse轉到as上的專案,可能沒有gradle.properties檔案,請在專案根目錄中手動建立

6-Error:Removing unused resources requires unused code

Error:Removing unused resources requires unused code shrinking to be turned on. See http://d.android.com/r/tools/shrink-resources.html for more information.

android {
    ...
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
}

在android 3.0需要android profile時都false

7-Error:Unable to resolve dependency for ‘:mobile:[email protected]/compileClasspath’-multidex:1.0.0

老版本如果遇超過方法數超過65535的解決辦法一般是defaultConfig 中新增multiDexEnabled true

現在AS3.0好像沒有這個限制,需要將gradlemultiDexEnabled true去掉:

    defaultConfig {
        applicationId "com.zte.iptvclient.android.armenia"
        signingConfig signingConfigs.release
//        multiDexEnabled true
    }

Application中將MultiDex.install(this)刪除:

    /**
     * 相容5.0以下系統支援MultiDex分包
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
//        MultiDex.install(this);
    }

8-….省略…Error while executing process …省略…/aapt.exe with arguments…

需要在SDK Manager的SDK Tools中選擇Tools和Platform-Tools進行更新,此外更新SDK到Android 8(api-26)

將專案中compilesdk替換為26

9-error: style attribute ‘@android:attr/windowEnterAnimation’ not found.

提示我們找不到@android:attr/windowEnterAnimation,因為已經不支援@開頭使用android自帶的屬性,我們只要把@符號刪掉就可以了。

全域性搜尋@android:attr/windowEnterAnimation找到相應的地方,把@去除

修改前:

<style name="remnote_play_time_animation">
        <item name="@android:windowEnterAnimation">@anim/remote_play_popup_show</item>
        <item name="@android:windowExitAnimation">@anim/remote_play_popup_hide</item>
</style>

修改後:

<style name="remnote_play_time_animation">
        <item name="android:windowEnterAnimation">@anim/remote_play_popup_show</item>
        <item name="android:windowExitAnimation">@anim/remote_play_popup_hide</item>
</style>

10-Error:(20, 36) 錯誤: 找不到符號 符號: 變數 mAvailIndices 位置: 型別為FragmentManagerImpl的變數 fragmentManagerImpl

原因是support-v4版本問題。
gradle中新增如下內容implementation 'com.android.support:support-v4:26.1.0'
有網路可以直接更新,也可以使用快取的support v4(找我下載)

6-新增google()

有些內容需要從google上面更新(公司的專案沒加這個也沒影響)

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

allprojects {
    repositories {
        ...
        google()
    }
}

7-專案轉為kotlin專案

在加入Kotlin檔案後,as會提示自動新增kotlin相關配置。但是此時在你的build.gradle檔案最下方會被系統自動加入

    repositories {
        mavenCentral()
    }

一定要刪掉!!會導致你一直去下載一些依賴,而這些依賴是你通過aar新增的。

1-異常

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter animation

表示null值給了不能為null的變數。kotlin中嚴格區分Int和Int?這種前者絕對不可為null和後者可為null的情況。

8-其他

1-可能會自動更新espresso

2-gradle下載的依賴包路徑

C:\Users\6005001819\.gradle\caches\modules-2\files-2.1

用阿里倉庫

 maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}

在as設定中搜gradle,在android studio中disable embedded maven repository

遷移到AS3.1

1-下載AS3.1

2-下載Gradle4.4

3-下載Build Tools 27.0.3

4-Gradle異常

The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?

instrumentTest.setRoot('tests')

解決辦法:在gradle檔案中搜索instrumentTest,該內容已經被棄用。用 androidTest 替換 instrumentTest,編譯執行即可。

androidTest.setRoot('tests')

解決辦法:參考上面3.0中Gradle錯誤中關於維度統一的部分。

Unable to resolve dependency for ‘:xxx/compileClasspath’: Could not resolve project :xxx.

build.gradle的buildTypes內修改如下:

buildTypes {
    release {
      minifyEnabled true
      shrinkResources true
      zipAlignEnabled true
      proguardFiles 'proguard.cfg'

/*
 *   因為只有release,沒有debug,新增如下內容
 *   如果有debug內容,需要在其中新增:matchingFallbacks = ['release']
 */
      matchingFallbacks = ['debug']
    }

    releaseUnsigned.initWith(buildTypes.release)
    releaseUnsigned {
      signingConfig null
    }
}

9-參考資料

相關推薦

Android studio 2.3 遷移專案3.03.1

記錄了將公司大專案,完整從Android Studio 2.3遷移到 Android Studio 3.0的全部過程。 按照步驟遷移,遇到的所有報錯和解決辦法都在後面。 Android專案從2.3遷移到3.0、3.1 版本:2018/8/1

Android Studio 2.33.0 升級後問題解決

更新:Android Studio 3.0出來了,和 升級2.3版本時類似 依然需要下載對應的gradle版本 Android Studio 2.3 預設對應 gradle-3.3-all.zip Android Studio 3.0 預設對應 gradle-4.1-all

android studio 2.3 cmake ndk開發第一個專案執行

勾選support c++的ndk配置 建立專案 新增 defaultConfig {             ndk{             moduleName "HelloJni" //so檔案: lib+moduleName+.so             ab

簡訊驗證碼的實現--基於Mob3.0 SMS SDK + Android Studio 2.3.3

前言: 1. Mob的官方文件有點迷人,但是,終於還是解決了。 特此總結,幫助下學習Android的兄弟們。 2. 那個AppKey的獲取很簡單,我這裡沒記錄,老鐵們自己去Mob官網註冊下就可以了,而且,網上教程也很多。 1.環境配置 1.1 獲

Android studio 2.3.3 升級到 3.1.3最新版本 匯入舊專案遇到的諸多錯誤,終極解決辦法!!

                                                         如果上天再給我一次機會,我不會將舊專案匯入到最新的studio版本。薩瓦迪卡,歡迎大家來到我們美麗的泰國。我們的東西很好吃。 如果你是跟我一樣升級完stu

Android Studio 2.3版本 Run專案不能自動啟動APP的問題

重點提示: Android Studio 2.3.1版本已經解決了此問題!!!!! Android Studio 2.3.1版本已經解決了此問題!!!!! Android Studio 2.3.1版本已經解決了此問題!!!!! A

Android Studio 2.3.3 添加ksoap2的引用(拒絕網上其他的忽悠),也適用於添加其他Jar的引用

ima mage androi img 適用於 網上 .com 2.3 ksoap2 Android Studio 2.3.3 添加ksoap2的引用(拒絕網上其他的忽悠),也適用於添加其他Jar的引用

android studio 2.3.3 最新 中文 漢化包 韓夢飛沙 安卓工作室 美化包

android studio 漢化 2.3韓夢飛沙 韓亞飛 [email protected] yue31313 han_meng_fei_sha漢化包 百度雲盤 下載地址:https://pan.baidu.com/s/1pLjwyeB最新最詳細全面最牛逼的漢化!穩定無BUG!設置界面可以

Android Studio 2.3 更新小記

作者 palette panel 面板 rain 壓縮 功能 out AS Instant Run 的改進和 UI 變化Instant Run 基本上能夠解決中小型項目的編譯緩慢問題。作為 Google 重點關註的一個功能,Android Studio 2.3 版本在原來的

Android Studio 2.3之簽名打包

APK signature scheme v2 官方說明:https://developer.android.com/about/versions/nougat/android-7.0.html#apk_signature_v2 Android 7.0 引入一項新的應用簽名方案

Android Studio 2.3使用來自控制檯的模擬器,“/ dev / kvm device:

最近遷移到了基於Ubuntu 18.04開發的Elementart OS Juno上, 執行AndroidStudio建立AVD時出現如下錯誤: 解決方案: [email protected]:~$ sudo chown -R seven /dev/kvm # seven是我的Linux使用

Android Studio 2.3 以後給apk簽名打包後安裝失敗的問題

本人java小白,最近一直在學習java,然後接觸到AS,apk簽名打包完成並顯示打包成功(具體簽名步驟看Android Studio 程式簽名打包),在驗證簽名打包是否真的成功,發現結果顯示沒有簽名。如下圖: 谷歌搜尋了一些解決方法,發現 官網上的解釋: 標紅的地方已經提到

Android Studio 2.3.3 出現Error:(26.13) Fail to resole: com.android.support.appcompat永久解決方法...

Android Studio 出現Error(26.13):Fail to resole:com.android.support.appcompat-v7.28_ Install Repository sync project show in file Show in project structure di

Android studio 2.3.3配置OpenCV 3.4.2

Android studio2.3.3配置OpenCV 3.4.2,這裡就將自己踩過的坑碼下來共免。 首先我的Android studio版本是2.3.3,OpenCV可以去官網上下載相應的Androidsdk版:           &

Android Studio 2.3 Build app 比 2.2

之前在 Android Studio 2.2.3 不到 30秒就可以在 app 裡 run, Android Studio 2.3 需要在電腦前乾等 1分半,等到快吐血。 而且新的 Android Studio 2.3 如果不使用新版 Gradle 2.3.0 就不能用 Instant Run, 問題在有時

Android Studio 2.3更新問題及更新內容

更新Android Studio2.3後發現以下二個問題: 1. Gradle sync failed: Unable to tunnel through proxy. Proxy return

Android Studio 2.3 正式起航

歡迎Follow我的GitHub, 關注我的CSDN. 其餘參考Android目錄.已同步微信公眾號:猛戳這裡 推薦文章: Android Studio 2.3 現已提供下載。此版本的重點是整個 IDE 的質量改進。我們感謝大家迄今為止提供的所有反饋。我們致

Android Studio 2.3.3 kotlin plugin not find

最近專案我使用kotlin編寫的,前幾天開啟專案的時候蹦出來一個updata,沒細看直接升級了。 問題接踵而至,升級的是kotlin plugin的版本。 今天上午開啟專案的時候報錯: Cannot add jps/kotlin-jps-plugin.jar from

Android studio 2.3.3+Dlib

最近因為需要,嘗試著將Dlib移植到android上。android小白一枚,做之前沒接觸過android,所以就用了別人說比較好的android studio 最新穩定版(windows下)。在android studio2.3.3版本中使用cmake+cla

Android Studio 2.3.3 32位 win7 cmake的配置

在Android Studio中,CMake是可以直接通過SDK Manager下載的,但是在Android Studio 32位裡就只能自己下載了。我從CMake官網獲取了一份最新的CMake,放到報錯提示的位置下,遇到了錯誤: CMake Error: Could n