Common Problems When Opening an Existing Project in Android StudioWhen I was studying engineering, a few years ago, I developed some Android applications using this IDE. Currently I have the need to create an application, but I remember absolutely nothing about how to create, develop or test it on a mobile device. So I will take note of all the steps so that they are not forgotten. When we're not experts at something, the first thing we do, after reading the documentation, is download examples to see how other people have managed to build their own solutions. One of the most frustrating things is not being able to properly execute that simple example, as shown in the screenshots.
Most common errors when trying to run a sample project in Android Studio
1. Not having the right development kit (SDK)Because any programming language evolves over time, it's normal for source code to require specific dependencies in order to run. That's why, when opening a sample project downloaded from the Internet, one of the most common errors is the following:
- License for package Android SDK Build-Tools xx.xx not accepted.
- No variants found for 'app'. Check build files to ensure at least one variant exists.
Solving this problem is very simple. To do this, you just have to go to the IDE preferences and download the SDKs needed for the project.
In the preferences window we must go to the Android SDK option and select the version that the IDE is requesting from us. In my case, it required version 28, corresponding to Android 9 Pie, which was the development kit used to build the example I downloaded from GitHub.
After installing this dependency, we need our IDE to analyze the project again. By now having the appropriate SDK, the IDE will be able to identify each part of the project and download its dependencies, allowing us to run it and install it on a virtual or real device. To do this, we must execute the following option:- Sync Project with Gradle Files
Once the Gradle file tasks finish running, the IDE will make a few changes: certain options that were previously disabled will be enabled, and the display menu on the left will show all the corresponding options.
2. Google Play requires a newer development kit (SDK)
This is a very common problem when trying to open old projects. As you might expect, these were built using the versions available back then. However, for security reasons, Google Play requires apps to be updated and use newer versions of the SDK, thus reducing the chance that they contain vulnerabilities. For example, this is a project that was built several years ago, when the SDK was in version 25. The build.gradle file, which is located inside the app's directory (usually called an app), indicates that it was compiled using that version. However, when we try to open it years later, we find that the IDE indicates that Google Play requires to migrate to a higher version.
Most of the time, simply increasing the version in said Gradle file can already compile our application, but there may be cases where we also have to update other dependencies.
In this example, after upgrading to a version of the SDK that supports Google Play's requirements, we find that some dependencies also need to be updated.
|
|
|
|
|
The version of a dependency is increased and now we no longer have problems in the Gradle file.
After updating the SDK in the Gradle file, we must do the same as in the previous point: verify that we have the corresponding SDK, check that it is downloaded from the SDK Manager and run the File → Sync Project with Gradle Files option again. In some cases we will also need to update the source code if we have used syntax that has been deprecated in later versions. This will depend on each project. What if we need to develop an app for a very old device that isn't capable of supporting apps compiled with a more modern SDK? This can be solved by downloading an old version of Android Studio or using a different IDE. In addition, we must check if we can get all the dependencies from that time, since, if they are not available on a public Maven server, it will be necessary to set up a private server and publish all the required dependencies.
3. Offline repository or unavailable dependencies
As we have indicated in the previous points, in old projects it is common for some dependencies to have been removed from the repositories due to their age. A quick way to fix this problem is to add another server or mount one of your own. For either of these two alternatives, we need to add the new repository in the file build.gradle that is located at the root of the project.
A possible solution could be to add Google's Maven repository:
4. Failing to run the project on a real device
To run the project on a virtual device, simply open the AVD Manager and create an Android device with the desired features. By default, Android Studio includes an emulator with a device similar to a Google Pixel preconfigured. To access the AVD Manager, we must do it from the Tools menu.
View of the virtual devices where we will test our applications. In them, you can emulate different versions of Android and configure some basic options, such as screen size and input methods.
To install the application on a device, we must use the Run menu and select the Select device... option.
We will be shown a list of the devices on which we can install and run our application. In my case, only the virtual devices that I have created appear. Nowhere do I see my Xiaomi Mi A2, which I have wired to the computer.
This is because our real device must have developer options enabled and, within them, USB Debugging mode. Unfortunately, enabling these options will be different on each device, as most manufacturers add layers of customization that make menus in different places. However, the procedure is similar. To activate these options, the first thing we must do is look for the build number and tap it several times until the corresponding message appears. I leave the example of how to do it from my phone.
Second, enable USB debugging.
View all (5 images) Once the previous steps have been carried out and with the device connected to our computer, Android will ask us if we want to allow the computer to connect to the phone. We simply accept the message and that's it.
Now we will be able to see our physical Android device in the IDE.
Finally, we run the Run "..." option and we will have our application running on the device.
|
|
|