(原创)
摘要:
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.至此,环境与兼容性问题解决,导入项目成功.
本方法为自我学习研究的导入项目通法,不适用所有项目.