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!
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.
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>
Try using Make Project option under Build.
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.
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>
Related
I have generated the apk for an android app but can't figure out how to keep track of the log files.
My app is running during build and run, however it's crashing when the apk file is generated.
I need help to track the error for the apk file.
Also using crash reporting tools like Crashlitycs would be a good choice.
You need to create an Account and put your API key in your app. Then initilize Crashlitycs and now on, everytime your app crash it will send a report and you can track them all even they are grouped by API level and App Version.
According to this guide the way to go is:
Add the Kit to Your build.gradle
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
// These docs use an open ended version so that our plugin
// can be updated quickly in response to Android tooling updates
// We recommend changing it to the latest version from our changelog:
// https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
classpath 'io.fabric.tools:gradle:1.+'
}
}
Then in your app level gradle:
// Put Fabric plugin after Android plugin
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
And add the dependency:
compile('com.crashlytics.sdk.android:crashlytics:2.9.5#aar') {
transitive = true;
}
In your manifest, add INTERNET PERMISSION and add this meta with your API KEY:
<meta-data
android:name="io.fabric.ApiKey"
android:value="<FABRIC_API_KEY>"
/>
Finally initialize Crashlitycs with:
Fabric.with(this, new Crashlytics());
You can write your logs and exceptions in a file and can check that file later
or can email/post on server that file
also you can add Log Events - Firebase - Google library inside your project thats a good approach to maintain the app in long term
https://firebase.google.com/docs/analytics/android/events
I have recently developed an app in which I wanna use a small app I have developed before, let's say in one view like below:
public class MyMain extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showmysmallapp(); //how?
}
}
I have saved the small app as aar file and imported it to my new app and added in my build.gradle dependency also:
dependencies {
compile project(":mysmallapp")
...
}
How can I run the entire small app when a view of my main app starts pls?
or should I have added the library in another way?
Much thanks ahead.
I guess you want to run an app as a plugin.
You can use one of these libraries:
VirtualApp
VirtualApp is an open platform for Android that allows you to create a
Virtual Space, you can install and run apk inside. Beyond that,
VirtualApp is also a Plugin Framework, the plugins running on
VirtualApp does not require any constraints. VirtualApp does not
require root, it is running on the local process
DL: Apk (Readme in chinese).
Android PluginManager
PluginManager is used to manage android applications like eclipse
plugins. you can start an activity from an uninstalled apk placed in
sdcard,just like it has installed or registed in the application's
AndroidManifest.xml.
I am working on a code that was made in the Eclipse and i am migrating to Android Studio, however i am having a problem to the to import
com.google.android.maps.GeoPoint;
but i can import
com.google.android.gms.maps.GoogleMap;
I've tried to add in my Gradle file
compile 'com.google.android.gms:play-services:6.5.87'
and also i've tried also to use .jar, however i didn't have good results.
compile files('libs/google-play-services.jar')
I searched about this in many places, but i found nothing to help me, case somebody have resolved this problem and want help me i thank you.
To work, i had that to replace the below line in the my Gradle file.
android {
...
compileSdkVersion 16
...
}
To
android {
...
compileSdkVersion "Google Inc.:Google APIs:16"
...
}
Please following all the steps to Add Google Play Services to Your Project in Android Studio:
1.Open the build.gradle file inside your application module directory.
Note: Android Studio projects contain a top-level build.gradle file and a build.gradle file for each module. Be sure to edit the file for your application module. See Building Your Project with Gradle for more information about Gradle.
2.Add a new build rule under dependencies for the latest version of play-services. For example:
apply plugin: 'com.android.application'
...
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
Be sure you update this version number each time Google Play services is updated.
Note: If the number of method references in your app exceeds the 65K limit, your app may fail to compile. You may be able to mitigate this problem when compiling your app by specifying only the specific Google Play services APIs your app uses, instead of all of them. For information on how to do this, see Selectively compiling APIs into your executable.
3.Save the changes and click Sync Project with Gradle Files in the toolbar.
4.Open your app's manifest file and add the following tag as a child of the element:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
You can now begin developing features with the Google Play services APIs.
Please refer to here for more details.
I had a working android project but needed to start from scratch. I copied all my source files and layout files over. There are no compilation errors but when I try to run, I get the following error.
11-27 17:21:56.793: E/AndroidRuntime(1450): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{projects.mobile.mapappproject/projects.mobile.mapappproject.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "projects.mobile.mapappproject.MainActivity" on path: /data/app/projects.mobile.mapappproject-1.apk
I am running eclipse juno and trying to run on Android 4.2. Thanks !
Rather than doing copy paste try following
1.Create an android application project
2.Delete the files which was created Eg.MainActivity and layout.
2.Right click on project name and click import ,then from File system.Go to your project directory to import the source from there
3.select that, then finish.
4.Clean and build then run
Try these
Add the Activity to AndroidManifest.xml
Is there a difference in your folder names ? For example projects.mobile.mapappproject-1 or projects.mobile.mapappproject !
I copied all my source files and layout files over.
It looks like you forgot to copy over the entries that existed in AndroidManifest.xml, however. Each Activity has an entry there and that entry is required for the framework to find and launch the UI.
I had the same problem, but I found out that a library reference path wasn't right. After I fixed that everything worked fine.
You can check this at Right-click on your project => Properties => Android -> (Library)
Just for the record in case I can help someone :
I had a project that somehow didnt work after importing it from git. I solved this by giving the name of the activity explicitly.
example:
before:
<activity android:name=".Main" [...] /> (relative)
after: <activity android:name="com.example.package.Main" [...] /> (absolute)
This may also occur if you change the manifest package and keep your relative reference (which I think is default).
In my case, following 2 steps fixed the problem
a. Setting compileSdkVersion, buildToolsVersion, targetSdkVersion to the same SDK version in build.gradle.
b. I was missing a dot in application class name in AndroidManifest.xml (Ex. android:name=".ui.ApplicationLoader")
I was developing an application on API 9, 2.3.3 then I changed the build to 4.0.3 because I only had 4.0.3 installed other than 2.3.3 and 2.2 due to admob's policy.
I have tried to embed admob into my Android application but have been failing ever since. I finally did it on my school computer and it works perfectly. However, I imported my project into my own laptop, trying to edit and add on more coding and lots of problems started to arise without even editing any part of the code.
What I know is that my school PCs are using eclipse indigo and I am using juno.
Is it because I did not import my codes properly or it is my eclipse version or something else?
Here's the dummy code I did.
MainActivity.Java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
XML
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="the right code"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
Manifest
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
So there is it. I have looked into many sites for help but to no avail.
When you move a project in between computers you have to re-import things (because their file path's have changed). Try looking in your imports and seeing if the SDK is still imported.
Whether it is or not, if you are using ADT tools r-17 and up then you have to create a libs/ folder in you main directory, and put a copy of the adMob SDK in there.
Hope this solves your problem
Check for updates and installed sdks in your sdk manager in eclipse and check if the admob sdk for your target is installed.
and do not forget to set your application target to google apis x.y