Use SQLiteNetextensions with Xamarin for Android-App - android

I've read some threads about SQLite for Xamarin but I'm still not able to set up SQLiteNetExtensions in a proper way. I'm currently developing an android app with Target Framework Android 7.1(API Level 25 - Nougat).
Can anybody tell me what I'm doing wrong?
I installed nuget packages:
Install-Package SQLiteNetExtensions -Version 2.0.0-alpha2 -Pre
Install-Package SQLite.Net-PCL -Version 3.1.1
According to: https://bitbucket.org/twincoders/sqlite-net-extensions
Then I set up my code.
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
namespace AppName.Resources.Model
{
public class Entry
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Stock))]
public int StockId { get; set; }
public DateTime Date { get; set; }
public double Amount { get; set; }
}
}
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
using System;
using System.Collections.Generic;
namespace AppName.Resources.Model
{
public class Stock
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public DateTime LastUpdated { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<Entry> Entrys { get; set; }
}
}
using Android.Util;
using AppName.Resources.Model;
using SQLite.Net;
using SQLite.Net.Platform.XamarinAndroid;
using SQLiteNetExtensions.Extensions;
using System.Collections.Generic;
using System.IO;
namespace AppName.Resources.DataHelper
{
public class DataBase
{
string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
public bool CreateDataBase()
{
try
{
using (var stocksDBConnection = new SQLiteConnection(new SQLitePlatformAndroid(), Path.Combine(folder, "Stock.db")))
{
stocksDBConnection.CreateTable<Entry>();
stocksDBConnection.CreateTable<Stock>();
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
public bool InsertIntoTableStock(object stock)
{
try
{
using (var stocksDBConnection = new SQLiteConnection(new SQLitePlatformAndroid(), Path.Combine(folder, "Stock.db")))
{
stocksDBConnection.InsertWithChildren(stock);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
...
These References were added by nuget:
SQLite-net
SQLite.Net
SQLite.Net.Platform.XamarinAndroid
SQLiteNetExtensions
SQLitePCLRaw.Batteries_green
SQLitePCLRaw.Core
SQLitePCLRaw.lib.e_sqlite3
SQLitePCLRaw.provider.e_sqlite3
Occuring error:
'SQLiteConnection' does not contain a definition for 'InsertWithChildren' and the best extension method overload 'WriteOperations.InsertWithChildren(SQLiteConnection, object, bool)' requires a receiver of type 'SQLiteConnection'
'SQLiteConnection' does not contain a definition for 'GetAllWithChildren' and the best extension method overload 'ReadOperations.GetAllWithChildren(SQLiteConnection, Expression>, bool)' requires a receiver of type 'SQLiteConnection'
Well that's how far I get. Anybody out there who knows what to do? Maybe remove conflicting references?

Ok I'm sure that its just a version problem because I just tried your code and it works fine for me. Try installing SQliteNetExtensions v 1.3.0 package from the NuGet package manager window in Visual Studio and nothing else. It will install all its dependencies.
Here is what I did - Try to do the same and see if this helps :
I Created a new Android Single View App Project in Visual Studio
(I'm using community 2017 - but it should not matter ) and created
the Stock , Entry and Database Classes and pasted your given
code respectively.
I installed only the SQliteNetExtensions v 1.3.0 package from the NuGet Package Manager
It installed the following dependencies along with it, I didn't installed these myself.
SQLite.Net-PCL v3.0.5
NewtonSoft.Json v6.0.8
Here is what I got after installing just SQliteNetExtensions v 1.3.0 package.
As you can see its showing an update for SQLite.Net-PCL v3.0.5 to v3.1.1 I tired also with the updated one and it still works fine , so its up to you to update or not.
Here a bit proof that its working fine. I also compiled and run the app on emulator and it works all fine.

Ok after a bit of searching , I came to know that the creator of this extension used a pattern called MvvmCross while developing it. So unless you are using that pattern model you will get this error.
For non-MvvmCross users they have to rebuild the source files and reference it in their project and not use the pre-compiled one, the one you are using.
Go to the fourth post on the following link
https://forums.xamarin.com/discussion/20117/sqlite-net-extensions-and-sqliteconnection here he tells how to rebuild from source in his post in a very brief manner.
Hope you will understand it but if not wait till I install Visual Studio and Xamarin and try it out myself so I can give you accurate steps.
It will take a while so till then , Kudos!
Here is the link to source files https://bitbucket.org/twincoders/sqlite-net-extensions/downloads/
Click on download repository

After some research I found out that:
Install-Package SQLiteNetExtensions -Version 2.0.0-alpha2 -Pre
depends on
Install-Package sqlite-net-pcl
instead of
Install-Package SQLite.Net-PCL -Version 3.1.1
But after removing all references, cleaning the solution and reinstalling the two needed packages there is no SQLite.net available anymore.
When trying to use SQLite instead:
'SQLiteConnection' does not contain a definition for 'InsertWithChildren' and the best extension method overload 'WriteOperations.InsertWithChildren(SQLiteConnection, object, bool)' requires a receiver of type 'SQLiteConnection'
I'm curious if it's even possible to develop Android-Apps with VS seamlessly. Since everything takes years to set up.
EDIT:
Well, after some days of pain in the a** the solution was to use the even newer pre release (alpha3) which wasn't even referenced to on the official pages. Currently I'm satisfied. :)
Install-Package SQLiteNetExtensions-netstandard -Version 2.0.0-alpha3 -Pre
Hope anybody may need this.

I just solved this after doing a whole load of updates. Hit the same issues.
Uninstall ALL SQLite plugins. See attached pic/link below.
Reinstall SQLiteNetExtensions and this installs all dependencies too, actually all files in that list except SQLite.Net-PCL.
SQLiteNetExtensions installs SQLite-net-pcl but you need the other one too so, install SQLite.Net-PCL as well.
Restart Visual Studio as it doesn't always pickup all newly installed packages, the reopen your solution.
That worked for me so hopefully it'll help someone else here too.

Related

App crashes on Launch when made as system application

I developed an application that uses DJI SDK. When installed as an system application it crashes on Launch because its not able to load the libraries. It works fine when installed as normal application.
Steps followed to make system application
1)Rooted the device
2)Copied the .apk file to system/app directory
3)Rebooted the device
4)App was installed as default application but crashed on Launch
After commenting out the following line related to DJI SDK and followed the same steps as above. It worked fine as system app.
Helper.install(context);
The implementation of Helper class which is in DJI SDK is as below:
public static void install(Application app) {
Object var1 = null;
try {
System.loadLibrary("DexHelper_sdk");
if (PPATH != null) {
System.load(PPATH);
}
} catch (Error var3) {
;
}
String app_dataDir = app.getApplicationInfo().dataDir;
DexInstall.install(app, new File(app_dataDir + "/.cache_sdk/sdkclasses.jar"));
}
As far as my understanding the crash is because the system is failing to load the libraries.
My gradle looks similar as in the following link:
https://github.com/dji-sdk/Mobile-SDK-Android/blob/master/Sample%20Code/app/build.gradle
this issue has been fixed since 4.6 version of Android MSDK, pls try to update to the later version, right now the 4.7.1 has been released.

sqlite-net-pcl CreateTableAsync in Xamarin Android throws the error 'Java.Interop.JavaLocationException'

I'm having a Xamarin native devlopment solution. It includes a couple of projects, a PCL project and a Xamarin.Android project.
I installed sqlite-net-pcl for the PCL project and I am passing the platform specific database location path from the android project to the PCL. The folder path sets properly and when the execution head executes,
database.CreateTableAsync<MyModel>().Wait();
public class MyModel {
public int Id { get; set; }
public string Name { get; set; }
public bool IsDone { get; set; }
public DateTime Date { get; set; }
}
It throws,
Exception of type 'Java.Interop.JavaLocationException' was thrown. An unhandled exception occured.
and crashes. How can I fix it?
This error can appear if the Model class is containing any non complex data types like Arrays or Lists. The plugin supports only non complex types like, int, bool, string, enum .... The Model class is not having an issue here.
I solved this problem by,
installing the sqlite-net-pcl package for the android project as well.
Cleaning the solution.
Deleting the bin and obj folders of the android project.
then running the application after these steps, the app started functioning well šŸ˜Š

how to use Instrumentation under the uiautomator project?

I'm working on a uiautomator project recently and then the UiObject.getFromParent turn out a wrong element to me and I look into the source code of uiautomator and found out the answer is because by the UiSelector I used.
I found that the uiautomator is using the Instrumentation to get the UI element stuff just like :
getInstrumentation().getUiAutomation().getRootInActiveWindow();
I just want to get a AccessibilityNodeInfo node just like what uiautomator do but the uiautomator didn't exposed this.
Iā€™m trying this way by a new class extends InstrumentationTestCase,by the getInstrumentation() always return a null to me.
i found an answer on
android instrumentation test case - getinstrumentation() returning null
that needs injectInstrumentation(InstrumentationRegistry.getInstrumentation());
and told InstrumentationRegistry is from the official Android testing-support-lib:0.1
I have download the Android Support Repository and import the testing-support-lib-0.1-source.jar into my project but I still can't see InstrumentationRegistry.
Anyone have any idea about my cast?
I'm using an extension of InstrumentationTestCase and it works just fine like this:
#Override
public void setUp() throws Exception{
super.setUp();
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
AccessibilityNodeInfo root = instrumentation.getUiAutomation().getRootInActiveWindow();
}
I imported
import android.test.InstrumentationTestCase;
import android.support.test.InstrumentationRegistry;
import android.view.accessibility.AccessibilityNodeInfo;
and installed
Android Support Repository (15)
Android Support Library (22.2)
Hope it helps.
well,this question has beep solved by
unpack testing-support-lib-0.1.aar and get the classes.jar instead of
testing-support-lib-0.1-source.jar
.and now the eclipse will not saying can't resolved anymore.By the way,u have to
add this lib to Ant's javac classpath by edit build.xml
or else u will get a lib not found while u trying ant build.
but now the NEW question has turns out
INSTRUMENTATION_STATUS: stack=java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/test/InstrumentationRegistry;
maybe it's another question so i am going to find out via google and i will update this if figure it out or i will post a new question.
Frist tried:
i have noticed that the point of this question is the uiautomator didn't expose the api i want,and when i look into the source and found the UiDevice.dumpWindowHierarchy that has these code:
AccessibilityNodeInfo root =getAutomatorBridge().getQueryController().getAccessibilityRootNode();
the getAutomatorBridge was implemented by UiDevice:
UiAutomatorBridge getAutomatorBridge() {
if (mUiAutomationBridge == null) {
throw new RuntimeException("UiDevice not initialized");
}
return mUiAutomationBridge;
}
the point is there is no modifier on it,so what i have done is modified it's modifier to public ,modified the byte code of
/system/framework/uiautomator.jar
and the other one in sdk(sorry i don't,ya it worked!i can use it like this.
AccessibilityNodeInfo root =getUidevice().getAutomatorBridge().getQueryController().getAccessibilityRootNode();
but there is a compatibility issue isn't it?
Second tried:
Let's think about the default modifier of java,we can access it under the same package,so why don't we just implement a class has the same Package name ?then we can call it without any unnomal patch.
that's what i have done and it really works well:
package com.android.uiautomator.core;
import android.view.Display;
import android.view.accessibility.AccessibilityNodeInfo;
public class AutomationUltilites
{
public AutomationUltilites()
{
}
public static AccessibilityNodeInfo getRootNode()
{
return UiDevice.getInstance().getAutomatorBridge().getRootInActiveWindow();
}
public static Display getDisplay()
{
return UiDevice.getInstance().getAutomatorBridge().getDefaultDisplay();
}
}
Hope it helps.

Getting the Android SDK directory within a gradle task

Recently the gradle plugin for android got updated (with android studio), after which the previous way of getting to the SDK directory ceased to work. The expression
${android.plugin.sdkDirectory}
which worked in an older version now returns the error
Error:(42, 0) No such property: sdkDirectory for class: com.android.build.gradle.LibraryPlugin
What would be the proper way of getting the android SDK directory being used, preferably independent of the user's configuration such as plugin and gradle version? The script needs to be shareable with several users.
Since all the previous answers depend on the environment or specific user intervention on top of normal configuration, I'll just post my technically messy fix.
if (android.hasProperty('plugin')) {
if (android.plugin.hasProperty('sdkHandler')) {
androidPath = android.plugin.sdkHandler.sdkFolder
} else {
androidPath = android.plugin.sdkDirectory
}
} else {
androidPath = android.sdkDirectory
}
Unlike all previous methods, this actually works, but it still looks hacky.
In gradle.properties set location sdkdir=/home/user/android-sdk and then in gradle you can use $sdkdir
I'm using Android gradle plugin v1.2.3 and this works fine:
${android.sdkDirectory}
You can use
$System.env.ANDROID_HOME
export ANDROID_HOME=/xxx/xxx/ in shell, then use it by System.env.ANDROID_HOME in gradle file.

Unable to run Bitmap sample code for Android

I am trying to run the sample from :
http://developer.android.com/training/displaying-bitmaps/display-bitmap.html
However I encountered lots of errors like:
Description Resource Path Location Type
BuildConfig cannot be resolved to a variable ImageGridFragment.java /ImageGridActivity/src/com/example/android/bitmapfun/ui line 124 Java Problem
Description Resource Path Location Type
SuppressLint cannot be resolved to a type Utils.java /ImageGridActivity/src/com/example/android/bitmapfun/util line 99 Java Problem
I ran thru Google but could get nothing. Adjusted the android build target to 4.0.3 (15) but still no clue. Anyone ran this sample successfully?
Thanks.
Here is my solution:
1.Create a new class:
package com.example.android.bitmapfun;
public class BuildConfig {
public static final boolean DEBUG = true;
}
2.Comment the lines that contain "SuppressLint":
//import android.annotation.SuppressLint;
// #SuppressLint("NewApi")
Android developer tools r17 brings a feature to Eclipse where a class is auto built at build-time, called BuildConfig, which contains a constant that can be used by the app developer to sense whether the build is a dev build or a production build. This feature appears to be in the Eclipse integration support, so when using IntelliJ, this useful feature is not available
In gen folder with R.java there should be BuildConfig.java if your program compiled successfully.
/** Automatically generated file. DO NOT MODIFY */
package com.example.android.bitmapfun;
public final class BuildConfig {
public final static boolean DEBUG = true;
}
Clean your project and try to launch it again.
It worked for me.
For me also it is not running directly import to eclipse. Just i put comments which lines is showing errors then it is working fine for me. May be it is not a right answer but we can see the application functionality by running the code so i did like that.

Categories

Resources