How to repair gradle sync and project build in android studio? - android

I'm new to android studio. I recently made a new project with default MainActivity, then deleted the activity and made a new Activity that i set as default in AndroidManifest.xml by adding
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Since I did that, my whole android studio broke down. I couldn't run any app I previously made or any project I created since then. After hitting run/Shift+F5 it just says
Error running 'app': Default Activity not found
It does this on any project I open/newly create. I am not able to launch with default or any activity, it just says that it isn't declared in manifest file. If it is a newly generated project, it usually won't even launch. I have to sync it with gradle files. After that I am able to sometimes run it, but after every android studio restart it breaks again. Some of my bigger projects were able to start after around 4-5 clean projects and gradle syncs, but broke down again after android studio restart. I have reinstalled android studio and build tools multiple times and I am all out of ideas. Every build or gradle sync is successful without errors, it just can't find the activity to run.

If none of these other troubleshooting options work, such as Invalidate Cache/Restart and Syncing Gradle, it may be the project setup itself.
Most Android Studio projects need at least one module. A module is a way to divvy up the source code based on different devices being targeted (e.g., you may need one for Android TV, one version of your app for Android Wearables, etc.). You can find more info here on that: https://developer.android.com/studio/projects/add-app-module
So make sure you have a module (typically named app or if it's the only one, lower-case version of your app's name maybe). It should have the source code, assets & res folders (drawables and launcher icons and such), the AndroidManifest.xml, and its own build.gradle file separate from the main project's. It doesn't have to have much in there other than where to look for library repositories.
Just ran into this issue when migrating an old project to Android Studio 4.0, this seemed to do the trick.
EDIT: Still working on it, looks like it will run the first time and then it recognizes "No Default Activity" and stops me from running/installing.
EDIT: Figured it out. It is a new issue with AS 4.0 where the merged manifests had a library (from androidx compatability library) that targeted minSdk=15 instead of 14 like I previously had. It appears when I click on that error in the Merged Manifest tab when I have AndroidManifest.xml open (bottom-left of the pane) I am able to run as normal again. See here: https://issuetracker.google.com/issues/158019870

Related

Could not identify launch activity: Default Activity not found after upgrading to Android Studio 4.0

I've been having this problem since upgrading to Android Studio 4.0 on macOS. When I try to start my app from Android Studio, I get the following error:
Could not identify launch activity: Default Activity not found
Error while Launching activity
What's a bit unique about my project is that the default activity is defined in the manifest of another library used by my project, not the project itself. This was all working fine before the upgrade, but now it isn't for me. When I open the project containing the dependency, it builds and runs fine. I've already tried the following steps:
clean the project, rebuild
invalidate caches and restart
reinstall Android Studio
delete all generated files and folders (build, gradle, .idea, etc)
You can solve the problem as follows:
Click your Module Selection window,then click Edit Configurations…
Then set Launch Options to Nothing; that's okay:
To dovetail off of Mike N.'s comment, it looks like it is an issue to be resolved in the next point release: https://issuetracker.google.com/issues/158019870
For the details of the quick fix, for me I looked at the Merged Manifest tab, which is at the bottom-left of the AndroidManifest.xml pane. This shows all of the library manifests combined with your activity's.
The dialog said an error occurred where browser:1.0.0 manifest had a minSdk of 15 whereas all the rest of my minSdk are 14. I clicked on:
use a compatible library with a minSdk of at most 14, or increase this
project's minSdk version to at least 15.
Which brought up my minSdk to 15, and the error with the launcher went away, it will now insta-launch on my device. So I would sit tight for AS 4.0.1 but in the meantime, check the Merged Manifest.
For me this was happening in a project in which the main activity was declared in the manifest of an imported module (e.g. not in "app" module).
The solution for me was to add again the activity declaration in the manifest file of my top project:
<activity android:name="com.cristian_slav.elements.MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
On my side it was a issue with the manifest. You can detect this type of issue by watching the merged manifest tab. For more details you can watch the link shared by Mike N. in the first comment.
The problem will arise in new projects when one forgets to tick the default activity checkbox during adding anyone of the first or subsequent new activity.
This can be rectified at later stage by going to Run> Edit Configurations > Launch : in which change to specified activity and select the activity you choose to keep as the first page on launch of app.
Try these steps:
Close Android Studio
Go to
C:\Users\my_user_name.AndroidStudio4.0\system\caches
Delete "caches" folder
Relaunch Android Studio
set Launch Options to (Nothing)

Importing Eclipse Library Project into AndroidStudio

I have an Eclipse Project that I like to migrate to AndroidStudio, with the following Structure/Content
1 LibraryProject with most of classes for all my Apps. This Library has 1 Activity ("SharedStarterActivity"), which will be started from all my 3 Apps.
3 Apps using the mentioned LibraryProject. These Apps referencing an Activity of my ProjectLibrary App as the Start Activity in the Apps AndroidManifest.xml.
<activity android:name=".SharedStarterActivity"
android:label="#string/IndividualAppName">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In a first look, it does not make sense, but each app has its individual AppProp.class, which is used by the SharedStarterActivity.class and makes each app individual.
Now my question:
How can I migrate this Projects to AndroidStudio ?
I have read the migration guide "from Eclipse to AndroidStudio", but I am stuck now at
"Import Eclipse Projects to Android Studio" -> You should decide how
you will import your existing Eclipse ADT projects depending on their
structure:
Before Trial and Erroring, I wanted to ask you experts, how you would import the projects and will the mechanic still work with referencing an Activity from LibraryProject
in my Android Project (one of the 3 Apps) ?
see there is more types available in Google to migrate eclips to studio but which method I use please follow the step.
1) Open existing project in eclipse remove the library from setting and close eclipse
2) Open AS(Android Studio) import from eclipse option available there, choose the option and open your project in AS.
3) Some Library are already avail in AS. Click on your Main module (Main) press F4 open setting and check your lib available in listing or not.
4) if Avail then add from there else find the latest version of same library and add as module to your Main project.
5) please properly configure your Gradle ( Main Gradle and app Gradle file)
you may also refer below link to migrate in details
https://developer.android.com/studio/intro/migrate.html
or
http://www.developer.com/ws/android/migrating-from-eclipse-to-android-studio.html

How to add new activity to existing project in Android Studio - "Project not ready"

What needs to be done to add a new activity to existing project that works and compiles but has "Project not ready" in the menu. Please see the picture.
Other than that all works, so I don't know what IDE means by "Project not ready"
This is a little bit over a year but i thought i would provide an answer for anyone (novice like me) who may encounter the same problem.
I had the exact same problem as depicted and denoted. It was fixed by
Cleaning the project (Build --> Clean Project)
Sync with Gradle ( Tools --> Android --> Sync project with Gradle files)
What I did to solve this was to open up specifically the Android project in a new Android studio instance (not from the flutter project, which has everything)
Note if you don't see the option to do this in your project -> flutter options
Then you can just open the folder containing the project, followed by opening up the Android project specifically from there.
Now you should get the options
(Not an exact answer) Observation:
I don't know exactly what happened, but it works now, all I could notice is that gradle had some error from unclean build likely I must have had some code changes resulting in invisible compilation errors. After good build menus available again.
Please post your "observation" if you don't know exact answer.
Thanks
If you want to add a Activity to an android application,
you have to Extend Activity from one of your classes like this:
public class SecondActivity extends Activity
And add that activity to your manifest.
<activity
android:name=".ActivitySecondClass"
android:label="#string/app_name2" >
</activity>
You have to give the project time to build first. Wait a minute or two before jumping in.
I had the same issue. I fixed it by clearing the cache. (File > Invalidate caches & Restart).

Why do I get two shortcuts of my app, with different behaviour for each?

In android studio (1.0.2), I made a android library on its own project : I've just created a new project and changed the build.gradle, replacing apply plugin 'com.android.application' with 'com.android.library' (github repository). So building it I am ending with a .aar file. Notice that I am also trying to use Kotlin language in order to build it.
Then I imported this aar library file into an existing android project of my own (github repository), adding adjustement to the app build.gradle.
Though it works, at least on my android Emulator (x86 image of Jeally Bean api : 4.2.2), I got a strange behaviour :
In the application list, I got two shortcuts for my new application
But also, while a shortcut just starts with the library main activity, the other starts my owner ChessPositionManager activity, and goes immediatly inside the contained FileExplorer activity.
So, is that behaviour of having two different icons with a little different behaviour is explicable ? Can I avoid it and at the same time keep the fact that I am coding the library and my application separately ? (Indeed, a library module can easily be added to any project, but it is still coupled with this project, as far from what I've understood).
I assume that your library file has a manifest declaration making it show up in your launcher. See if your library manifest has:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If it does that is why its showing up twice, that intent filter signals to android you want it to show up.
Also, I suggest taking a look at http://developer.android.com/guide/components/intents-filters.html

unable to instantiate application - ClassNotFoundException

EDIT: This problem has not already been resolved in the other suggested SO question
I had a fully working app on the market for over a year, with very few crash reports. Then recently I changed my app into a library, so that it could be included within multiple different "wrapper" projects. This was so that I could easily make different version - free, paid, non-google markets, with/without in-app purchasing etc etc.
The new "library+wrapper" app appeared to work fine. I could run it multiple times, without error. But then a day later (when presumably the OS had closed some or all of the app's activities) I tried to run it and it reported
Unable to instantiate application com.mycompany.mygamelibrary.MyGameApplicationClass: java.lang.ClassNotFoundException: com.mycompany.mygamelibrary.MyGameApplicationClass
The class it failed to find is the first class that runs when the program starts up, MyGameApplicationClass - which extends Application. This class is part of the library.
I suspect something goofy in one of the two manifest files.
The manifest of the wrapper project contains the lines...
<application android:icon="#drawable/mygame_icon"
android:screenOrientation="portrait" android:label="My Game Name"
android:name="com.mycompany.mygamelibrary.MyGameApplicationClass">
Any ideas what could have gone wrong?
EDIT: The library was referenced "the correct way" as defined by yorkw's answer to this SO question.
EDIT: I can not repeat the crash at the moment :-( I don't know what it is the OS does when the app is not used for a day or two.
There are two possibilities. Either you, like me, have a spelling error in your manifest file. Have a co-worker or friend read it to make sure the name is correct. Or you have not referenced the project correctly.
The official document describes how to properly link projects in its documentation.
Why it would first seem to work and later stop working is a bit of a mystery. However, I guess that the VM might still have had the necessary references ready to resolve the classes in the library just fine. A restart of the VM removed all those references and trying to resolve them was unsuccessful.
Update: Regarding the edits in the OP: As you confirm that you have correctly referenced the other project, you can check if the project is included in APK, just to be sure. You can rename and open an APK as any other archive (.rar works fine for me). Sometimes, it happened to me, the project is not correctly included in the APK. A cleaning of your workspace usually remedies the problem and so could a restart of your IDE depending on what you are using. To manually conduct a clean in Eclipse for example, use Project->Clean... or try Android Tools->Fix Project Properties by right-clicking on your project.
As you seem to also have fixed the problem by restarting your device, it could be that the libraries were linked incorrectly. A problem that I have never seen myself but as a very common quote says: "Have you tried turning it off and on again?".
For Android Studio:
Build --> Clean Project
Fixed issue.
Have you tried to make a new subclass of MyGameApplicationClass in your 'main' project and set it in the manifest as Application class?
I had a slew of bugs with Android Studio 3.0 Canary 4 and the way I fixed them was by editing the AndroidManifest.xml by adding in some jibberish to the application name. Then, I clicked build. Obviously, a whole mess of new error messages appeared. I changed the name back to what it should be, built the app, and it just ran.
Sometimes, I just don't know...
EDIT: Just ran into this issue on Android Studio 3.0 Canary 4 on my laptop when switching over. I again went through the same process of changing AndroidManifest.xml file to contain a typo, building, and changing back. That didn't work.
I then noticed that instant run was still enabled. Going into settings (by clicking command + , (comma key)) and typing "instant run", I was able to disable instant run, built the app, and the error of class not found went away.
Summary of Steps to Fix [FOR ME]
Invalidate cache / restart
Clean the project
Manually delete the build folder (need to be in project view for this one)
Make an intentionally errant edit to your AndroidManifest.xml file, build the app, observe the errors, remove the errant edit and build again
Disable instant run
Again, I don't mean to insinuate that this will fix everyone's error, but I have now used some combination of these steps on two different machines (MacOS Sierra) and it has been resolved for me. Hope it helps.
In my case, application id and package were mismatched. This should be same as presented in following images...
AndroidManifest.xml
app/build.gradle
In this case you can see, applicationId and package both are same that is com.mycompany.mygamelibrary
May be its a Build Path Configuration problem.I did the following to solve the issue.
1.Right click on your project and go to Java Build Path.
2.Click on Order and Export tab.
3.Check Android Private Libraries and other 3rd part libraries if you have added.
4.Press ok and clean the project.
I hope it will solve the issue.
Once I had the same error message, but maybe the cause isn't the same.
I did a code and worked for a while, then I wanted to improve it and got the same error and I couldn't run it.
I could fix the problem with
the correct Build Path order (as I can see you've already did this)
I check on the Order and Export tab the android-suppor-v4.jar
and the key was the Android SDK Managert->Upgrade everything and (next) Eclipse->Help->Check for updates.
After I upgraded to the latest android plugin and SDK my app compiled and ran again.
I hope this will help you!
I'm not very sure about this but it might be that your system's debug.keystore license validity has expired as it is valid for only 365 days. You just need to delete the debug.keystore from your computer. The debug.keystore will be generated automatically by Eclipse when you compile your Android App.
Same message seen ... this time it turned out to be different output folders for MyApp/gen and MyApp/src in the Build Path (caused by Maven integration).
Unchecking "Allow output folders for source folders" solved the problem.
I had this issue in an Android application that needed an Application class which was created in wrong path inside the Android Studio project. When I moved the class file to the correct package, it was fixed.
This all Process work for me to solve application class Exception.
Step 1: Open Run(window+R) Search -> Prefetch Remove all file (Some file not Delete)
Step 2: Open Run(window+R) Search -> %temp% Remove all file (Some File not Delete)
Step 3: Open Android Studio -> Build -> Clean Project
OR
Select File > Invalidate Caches / Restart > Invalidate and Restart from Android Studio toolbar.
OR
Close and reopen Android project.
OR
Restart System
I ran into this issue several times and both times it seemed to be caused by some instant run feature.
In my case, deleting the application from the device and then installing it from Android Studio again resolved the issue.
I ran into this problem today. The project runs well for over a year but today it reports this issue, and cannot debug on my testing device.
I fixed it by updating to latest gradle version. Hope this can solve your problem.

Categories

Resources