Android successful built gradle but not install apps on emulator

Viewed 8089

I was having some problem with Android Architecture Component. What I am trying to do is from the view model, I will execute the functions inside repository class. Here is my view model:

private final ReservationRepository reservationRepository;
private final UserRepository userRepository;
private LiveData<List<ReservationEntity>> reservations;

@Inject
public ReservationViewModel(ReservationRepository reservationRepository, UserRepository userRepository) {
    this.reservationRepository = reservationRepository;
    this.userRepository = userRepository;
    reservations = reservationRepository.loadReservations();
}

public LiveData<List<ReservationEntity>> getAllReservations() {
    return reservations;
}

Then in my repository class:

private final ReservationDao reservationDao;

@Inject
public ReservationRepository(ReservationDao reservationDao) {
    this.reservationDao = reservationDao;
}

public LiveData<List<ReservationEntity>> loadReservations() {
    return reservationDao.getAllReservation();
}

In my DAO class:

@Query("SELECT * FROM reservation")
LiveData<List<ReservationEntity>> getAllReservation();

I got one AppModule to @Provide the @Inject:

@Provides
@Singleton
ReservationDatabase provideReservationDatabase(Application application) {
    return Room.databaseBuilder(application,ReservationDatabase.class,
            ReservationDatabase.DATABASE_NAME).allowMainThreadQueries().build();
}

@Provides
@Singleton
ReservationDao provideReservationDao(ReservationDatabase reservationDatabase) {
    return reservationDatabase.reservationDao();
}

I managed to build the project successfully. However, after successfully built gradle, it does not install the apps onto the emulator. Any ideas?

Thanks!

8 Answers

In my case, a Gradle Sync solved the issue.

You can do the following to resolve your problem

  • You can turn off the instant Run.

    File>Settings>Build , Execution , Deployment>Instant Run>Uncheck the Instant Run.

After that try to run your app on emulator again.

  • Restart Android studio

    File->Invalidate Caches / Restart

  • Perform Clean Build

    Build->Clean Project

  • Try to Sync your project

    File-> Sync project

  • Make sure Project location does not contain the special character.

    E:\Android_Projects\T&PUIET,KUK\app\build\outputs\apk\app-debug.apk

    close android studio > rename folder containing the special character(here T&PUIET,KUK ) > restart android studio.

Hope this Solution helps!

As @David Miguel Say that just do sync project with Gradle.

I do my problem solved.

Just use All search in my case Linux then I do double Shift press.

enter image description here

1.In Android Studio go to Build>Build APK(s) Then find the apk file of your app at [project-location]\app\build\outputs\apk\debug

2.Open command prompt or terminal(for MAC) of your PC and type cd C:\Users[user-name]\AppData\Local\Android\Sdk\platform-tools

It may vary if location of your installed SDK is different.

3.Then copy paste the generated apk in step1 at the location in step2.

  1. Then run adb install [generated apk name]

This should install apk in your emulator. Just make sure no other device is connected and no other emulator is running other than where you want to install it.

Behavior changes: Apps targeting Android 12!

Warning: If an activity, service, or broadcast receiver uses intent filters and doesn't have an explicitly-declared value for android:exported, your app can't be installed on a device that runs Android 12.

In my case (on physical device) I had to change android:exported from false to true in AndroidManifest.xml othervice device is not able to open app installed by android studio (app icon appears on devie but clicking = app isnt installed)

<manifest ...>
    <application ... >
        <activity ... android:exported="true" >

In my case, syncing Gradle and other answers didn't help. Because my problem was pretty idiotic. I didn't let the android studio to generate MainActivity when creating the project, so I had to generate the class and the layout manually. But I misplaced assigning the MainActivity to manifest file using the the tag <activity... </activity>

in my "dumb - case" i didn't have a launcher activity, add this to the first one :

 <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
Related