The solution to the version incompatibility problem when opening the Android Studio project downloaded from GitHub in a newer version of AS.

        Downloaded an Android Studio calculator project on GitHub, and a series of problems appeared after opening the project. This article simply records the problems and solutions.

1. There is an error in the path of the Gradle installation directory

问题描述:The specified Gradle installation directory 'G:\Android\Android Studio\gradle\gradle-2.14.1' does not exist.

Workaround: Follow the steps below to change the path of the Gradle installation directory

  1. Open your Android Studio project

  2. 点击File -> Settings -> Build, Execution, Deployment -> Gradle

  3. Change the path to the Gradle installation directory in the "Gradle home" field, making sure to point to the correct directory. For example, if the actual installation directory of Gradle is "G:\Android\gradle\gradle-2.14.1", you need to change the path to this directory.

  4. Click the "Apply" and "OK" buttons to save the changes.

  5. Click the "Sync Project with Gradle Files" button in the Android Studio project to sync the changes.

Note: Follow the steps below to find the actual installation directory of local Gradle

  1. Open Android Studio

  2. 点击"File" -> "Settings" -> "Build, Execution, Deployment" -> "Gradle"。

  3. In the Gradle settings, look at the path in the "Gradle home" field. This is the installation directory for the Gradle version your current Android Studio uses.

If you have installed multiple versions of Gradle, you can switch versions in the Gradle settings as needed and view the installation directory of each version. In the Windows operating system, the default installation directory of Gradle is usually located in the C:\Users\username.gradle\wrapper\dists directory.

2. After synchronizing Gradle (click the "Sync Project with Gradle Files" button), a connection timeout error occurs.

Problem description: Cause: connect timed out.

When you click the "Sync Project with Gradle Files" button in Android Studio, Gradle will try to download the required dependencies and plugins. If you get a "connect timed out" error, it's likely that Gradle was unable to connect to the remote repository or that the connection to download dependencies timed out. There may be many reasons for the connection timeout. Generally, it is the Gradle synchronization failure caused by the mirror source used in the project cannot be accessed normally.

Solution: Change the mirror source in the project, and change the mirror source to Ali's mirror source below.

1. Open the build.gradle file in the project, as shown below 

2. Change the contents of the two repositories{ } statement blocks in the build.gradle file to:

maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
google()

3. Save the changes with Ctrl+S, and then synchronize Gradle to connect successfully.

 Note: Using VPN (that is, scientific Internet access) will also cause a connection timeout error, and it is troublesome to solve this situation. Here's a workaround:

Using a VPN to connect to the Internet, you can configure a proxy server while connecting to the VPN to implement proxy settings for Gradle.

Configure proxy server while VPN connection. In Android Studio, you can configure Gradle's proxy settings by opening "File" -> "Settings" -> "Appearance & Behavior" -> "System Settings" -> "HTTP Proxy" and enter the proxy server address for the VPN connection and port number. Then, you can follow the usual steps for Gradle synchronization. If using a VPN to connect to the Internet, proxy server settings may vary depending on the VPN provider and network environment. Check with your VPN provider or network administrator if you are unsure about proxy server settings.

3. The Gradle version used by the project is not compatible with Android Studio 2022.1. 

Problem description: The project uses Gradle 2.14.1 which is incompatible with Android Studio 2022.1

The Gradle version 2.14.1 used by the project is not compatible with the Android Studio version 2022.1 I am using.

Regarding version compatibility, it is necessary to consider whether JDK, Gradle, Android Studio, and gradleplugin (Gradle plugin) are compatible. For specific version compatibility, please refer to the following article:

http://t.csdn.cn/TnTiM

Solution: In this project, Gradle version 7.5, Gradle plugin version 7.4.2, Android Studio version 2022.1, JDK

1. Change the Gradle version used by the project to 7.5: open the gradle-wrapper.properties file, and set

distributionUrl=https\://services.gradle.org/distributions/gradle-版本号-bin.zip

Change the version number in this line of code to 7.5

 

2. Change the project Gradle plugin version to 7.4.2: Open the build.gradle file in the project and set

classpath 'com.android.tools.build:gradle:version number'

Change the version number in this line of code to 7.4.2

 

 3. Save the changes after completing the changes, click the "Sync Project with Gradle Files" button to synchronize Gradle, it may be slow, just wait for the completion.

4. Outdated Gradle syntax is used in the project.

问题描述:Build file 'C:\Users\Administrator\Desktop\Calculator-master\app\build.gradle' line: 23 A problem occurred evaluating project ':app'. > Could not find method compile() for arguments [directory 'libs'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. 

Workaround: Change obsolete syntax

It can be solved by changing compileto implementation. build.gradleFind the problematic dependency statement in the file and change it to , compilefor implementationexample: 

// old syntax

compile fileTree(dir: 'libs', include: ['*.jar'])

// new syntax

implementation fileTree(dir: 'libs', include: ['*.jar'])

5. The project uses an old version of the Android Support library.

Problem Description:

dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation 'com.android.support:appcompat-v7:24.2.1' testImplementation 'junit:junit:4.12' }Version 28 (intended for Android Pie and below) is the last version of the legacy support library, so we recommend that you migrate to AndroidX libraries when using Android Q and moving forward.

This information indicates that the project uses an old version of the Android Support library, and it is recommended to migrate to the AndroidX library. AndroidX is a refactored version of the Android Support Library, which provides a cleaner package structure, better naming, better performance, and also fixes many bugs and limitations. 

Solution:

To migrate to AndroidX, you need to use the migration tool of Android Studio, the specific steps are as follows

  1. Make sure your project uses Android Studio 3.2 or higher.
  2. From the Android Studio menu bar, select Refactor > Migrate to AndroidX.
  3. Make sure all libraries are up to date, then click Refactor.

Android Studio will automatically modify your project files, including build.gradle, AndroidManifest.xmland Java code, to use the AndroidX library. You may need to manually modify some code, because some libraries may not be part of AndroidX. Finally, compile and test your application to make sure it works.

 6. A conflict occurred when merging the Android manifest file AndroidManifest.xml.

Problem description:
Manifest merger failed with multiple errors, see logs

The "Manifest merger failed with multiple errors, see logs" error usually indicates that a conflict occurred while merging Android manifest files. 

Task :app:processDebugMainManifest FAILED package="com.jupiter.calculator" found in source AndroidManifest.xml: C:\Users\Administrator\Desktop\Calculator-master\app\src\main\AndroidManifest.xml. Setting the namespace via a source AndroidManifest.xml's package attribute is deprecated. Please instead set the namespace (or testNamespace) in the module's build.gradle file, as described here: https://developer.android.com/studio/build/configure-app-module#set-namespace This migration can be done automatically using the AGP Upgrade Assistant, please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information. [androidx.vectordrawable:vectordrawable-animated:1.0.0] C:\Users\Administrator\.gradle\caches\transforms-3\d58132105ab173fe617d3377fd03fd9b\transformed\vectordrawable-animated-1.0.0\AndroidManifest.xml Warning: Namespace 'androidx.vectordrawable' used in: androidx.vectordrawable:vectordrawable-animated:1.0.0, androidx.vectordrawable:vectordrawable:1.0.0. C:\Users\Administrator\Desktop\Calculator-master\app\src\main\AndroidManifest.xml:11:9-17:20 Error: android:exported needs to be explicitly specified for element <activity#com.jupiter.calculator.main_activity>. Apps targeting Android 12 and hig her 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. See https://developer.android.com/r/studio-ui/build/manifest-merger for more information about the manifest merger.

Solution:

This error is caused by issues in the AndroidManifest.xml file, where the following two issues need to be resolved:

  1. Deprecated Warning: The package name is set in the package attribute in the source AndroidManifest.xml file. You need to set namespace (or testNamespace) in the module's build.gradle file instead. It is recommended to use AGP Upgrade Assistant to automatically complete the migration. For details, please refer to https://developer.android.com/studio/build/agp-upgrade-assistant .

  2. Error: You need to explicitly specify the value of the android:exported attribute when the component defines an intent filter. For details, please refer to https://developer.android.com/guide/topics/manifest/activity-element#exported . To solve this problem, you need to explicitly add the android:exported="true" or android:exported="false" attribute to the relevant activity tag in the AndroidManifest.xml file.

Therefore, you need to update the relevant activity tag in the AndroidManifest.xml file and set the android:exported attribute to true or false. In addition, you also need to set the namespace in the module's build.gradle file. Finally, re-run the Gradle task.

The above stack does two things:

1. Open the manifest file AndroidManifest.xml, and set the android:exported attribute of the relevant activity tag to true or false.

 

2. Open the build.gradle file of the module, in


Add a line of namespace "com.jupiter.calculator" below the line of code applicationId 
"com.jupiter.calculator"

 

 

 

Guess you like

Origin blog.csdn.net/Seattle_night/article/details/129827718