(原創)
摘要:
android studio在導入他人項目時會遇到各種兼容性問題,各種報錯,本文主要介紹利用復制原有項目依賴包和Build環境,以解決導入項目老舊或不兼容當前AS版本或環境的問題.
1.首先備份一份自有工程的依賴包和環境文件
准備 .gradle , gradle , build.gradle 文件用以替換到新項目中;
准備 app , gradle.properties , local.properties , settings.gradle用以更改新項目內容;
2.將1中准備的替換文件 .gradle , gradle , build.gradle 提換到新項目根目錄中,選擇替換.
3.逐個打開需要修改內容的文件,刪除導入項目中的廢棄語句,替換為自己能夠運行的語句:
打開APP -> build.gradle ,替換語句(大部分項目可以全部替換).
打開gradle.properties,復制代碼塊中啟用'android.useAndroidX'屬性的語句(紅色部分)到導入項目同名文件中,否則會報如下錯誤:
配置'debugRuntimeClasspath'包含AndroidX依賴項,但未啟用'android.useAndroidX'屬性,這可能會導致運行時問題.
# Project-wide Gradle settings. # IDE (e.g. Android Studio) users: # Gradle settings configured through the IDE *will override* # any settings specified in this file. # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true # AndroidX package structure to make it clearer which packages are bundled with the # Android operating system, and which are packaged with your app"s APK # https://developer.android.com/topic/libraries/support-library/androidx-rn android.useAndroidX=true # Automatically convert third-party libraries to use AndroidX android.enableJetifier=true
打開local.properties,復制代碼塊中配置SDK和NDK(如果有)的語句(紅色部分)到導入項目同名文件中:
## This file is automatically generated by Android Studio. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file should *NOT* be checked into Version Control Systems, # as it contains information specific to your local configuration. # # Location of the SDK. This is only used by Gradle. # For customization when using a Version Control System, please read the # header note. sdk.dir=C\:\\Users\\96325\\AppData\\Local\\Android\\Sdk
打開settings.gradle,復制加入紅色部分代碼到導入項目同名文件中:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon } } rootProject.name = "LitePalTest" include ':app'
4.可以啟動android studio,Open項目,試運行app
仍有可能報錯:
(1).
Manifest merger failed : android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
針對Android 12及更高版本的應用程序,當相應組件定義了意圖過濾器時需要為“Android:exported”指定一個顯式值.
解決方法:
在AndroidManifest中加入android:exported="true"
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
5.至此,環境與兼容性問題解決,導入項目成功.
本方法為自我學習研究的導入項目通法,不適用所有項目.