In which SDK, jar this class can be found? I need to check at runtime if an exception is an instance of it.
I know this class can be found in AOSP sources. But this doesn't help much in runtime.
That is a hidden class in the Android SDK (see the Android 8.1 edition).
You can download the android sdk source, see this link:
Android SDK source code
With regards to checking the exception type at runtime, you can do the following (in Kotlin - transcribe to Java if required):
fun doSomething() {
try {
// Do something that may cause an exception
} catch (ex: KeyStoreException) {
}
}
or:
fun checkType() {
try {
// Something that could throw an exception
} catch(ex: Exception) {
when (ex) {
is KeyStoreException -> {
// Handle
}
}
}
}
This is a hidden class in Android . This can only be accessed by the framework or below layers and system apps , cannot be used by third party applications . You can check out the defination in the below link .
http://androidxref.com/8.0.0_r4/xref/frameworks/base/keystore/java/android/security/KeyStoreException.java.
There are 3 such classes in Android . All the 3 are under different packages and used in different context .
http://androidxref.com/8.0.0_r4/search?q=&defs=&refs=&path=%22KeyStoreException.java%22&hist=&project=art&project=bionic&project=bootable&project=build&project=cts&project=dalvik&project=developers&project=development&project=device&project=docs&project=external&project=frameworks&project=hardware&project=kernel&project=libcore&project=libnativehelper&project=packages&project=pdk&project=platform_testing&project=prebuilts&project=sdk&project=system&project=test&project=toolchain&project=tools
Related
I have a library in my Android app like UnityAds. I want to get the version name and the version code of it in runtime. I wrote the below code, but always exception occurred. What is the best practice to get this information programmatically?
implementation 'com.unity3d.ads:unity-ads:3.7.1'
try {
Log.d("Version", getPackageManager().getPackageInfo("com.unity3d.ads", 0).versionName);
} catch (PackageManager.NameNotFoundException e) {
Log.e("error", e.getMessage());
}
That won’t work, PackageManager APIs help you check for any valid packages/apps installed using a package name lookup.
Since the library is in the class path for your own package the only way to reference these ids at runtime would be to have a workflow to record these during compilation and expose it to your application at runtime.
Following approaches are viable:
Expose library metadata in build config/resources (https://developer.android.com/studio/build/gradle-tips#simplify-app-development)
Custom gradle plugin to capture all libraries in classpath into assets which can be read at runtime, something similar to https://github.com/google/play-services-plugins
With (1) you can’t scale it for multiple libraries conveniently so it’s only useful for very specific needs.
(2) might require some customisations for your usecase (adding version info) but is definitely scalable.
it's my first time diving in with protobuf and jetpack's DataStore but I've encountered an issue which is quite confusing.
I've created my .proto file in src/main/java/proto, it's really simple:
syntax = "proto3";
option java_package = "com.test.appname.proto";
option java_multiple_files = true;
message WalkInfo {
float distance = 1;
bool run = 2;
}
Then I've written in kotlin a serializer class for some data in my app.
object WalkInfoSerializer : Serializer<Walker.WalkInfo>{
override val defaultValue: Walker.WalkInfo
get() = WalkInfo.getDefaultInstance()
override fun readFrom(input: InputStream): Walker.WalkInfo {
try {
return WalkInfo.parseFrom(input)
} catch (exception: InvalidProtocolBufferException) {
throw CorruptionException("Cannot read proto.", exception)
}
}
override fun writeTo(t: Walker.WalkInfo, output: OutputStream) {
t.writeTo(output)
}
}
I've also set up my build.gradle file like this:
plugins {
id 'com.android.application'
id 'kotlin-android'
id "com.google.protobuf" version "0.8.14"
}
...
dependencies {
...
//DataStore
implementation "androidx.datastore:datastore:1.0.0-alpha05"
implementation 'com.google.protobuf:protobuf-javalite:3.14.0'
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}
Everything works fine, the classes seem to be generated correctly and they even pop up in android studio's autocomplete.
The problem is that I can't get the project to compile as I can't seem to find a way to resolve the "Unresolved Reference" for the methods generated (like getDefaultInstance(), parseFrom(), writeTo()
Even by writing the full class path it won't work.
Am I missing something? I've tried to play around with build.gradle and the proto file with some settings I found in their documentation but I still couldn't get it to work
Thank you!
Went to sleep, woke up, and knew the answer to my problems.
Dumb brain managed to do 1 + 1 during the night.
I realized that if the proto classes are being generated, I should not be defining them myself in kotlin code.
I had a WalkInfo message that generated a WalkInfo class, but I also had a WalkInfo class already with some methods in it. This is what was confusing the compiler.
After further research I realized that classes generated by proto are not even meant to be extended, they are supposed to be just data containers.
What I ended up doing is renaming my message to WalkInfoStorage while also keeping my WalkInfo kotlin class, I'll then be handling generating WalkInfo instances from the serialized data
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.
I'm trying to use the scala-reflect package for android development.
I have added the scala-reflect dependency in my build.sbt:
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.11.8"
but I get an exception:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/rmi/Remote;
at scala.reflect.internal.Definitions$DefinitionsClass.RemoteInterfaceClass$lzycompute(Definitions.scala:370)
at scala.reflect.internal.Definitions$DefinitionsClass.RemoteInterfaceClass(Definitions.scala:370)
at scala.reflect.runtime.JavaUniverseForce$class.force(JavaUniverseForce.scala:255)
at scala.reflect.runtime.JavaUniverse.force(JavaUniverse.scala:16)
at scala.reflect.runtime.JavaUniverse.init(JavaUniverse.scala:147)
at scala.reflect.runtime.JavaUniverse.<init>(JavaUniverse.scala:78)
at scala.reflect.runtime.package$.universe$lzycompute(package.scala:17)
at scala.reflect.runtime.package$.universe(package.scala:17)
I have tried to add the java source of java.rmi.Remote and java.rmi.RemoteException and built the project with android:package --core-library (because sbt has not found the dexCoreLibrary option) , it builded successfully, but I got the runtime error.
So, is it possible to add the java.rmi dependency otherwise, that scala.reflect can use it?
I'm using the scala.reflect library in the context of an implementation of a method Option.orDefault:
class RichOption[+A : TypeTag](val delegate: Option[A]) {
def orDefault : A = delegate.getOrElse {
delegate match {
case t if typeOf[A] <:< typeOf[Int] => 0.asInstanceOf
case _ => throw new IllegalAccessException("there is no default value for this type.")
}
}
}
If you know a better implementation for Option.orDefault
(possibly without scala.reflect), please let me know.
Much better:
case class Default[A](value: A)
object Default {
implicit val intDefault: Default[Int] = Default(0)
// other Default implementations
}
class RichOption[+A](val delegate: Option[A])(implicit d: Default[A]) {
def orDefault : A = delegate.getOrElse(d.value)
}
You get a compilation error instead of a runtime error if used with a type which doesn't have a defined default value, no complex matching, no need for a large dependency, etc.
my question is simple enough. I am using Gluon's plugin for Eclipse and developing an application in JavaFX to run on Android. However, I need to use an Android library (.aar format) and after a while of trying, cannot. Does anybody know how I can do this? I have already tried just using the classes.jar inside the .aar, while this has sort of worked, there are a few resources and things I believe the library is missing that is essential to its function. Thank you.
Edit: The library I'm trying to use is CloudRail, the download is here (Direct download)
Currently, using the classes from the extracted classes.jar it is somewhat functional but when the activity from their library is launched (com.cloudrail.si.servicecode.commands.awaitCodeRedirect.AuthenticationActivity) it is displayed however it is blank and there is nothing in it. I am currently under the assumption that this is incorrect and that the missing resources/files are the cause of this.
I've managed to extract the classes.jar from the aar file and add it to a Gluon project as dependency.
Next, I added the activity to the AndroidManifest.xml file, after the main FXActivity:
<activity android:name="com.cloudrail.si.servicecode.commands.awaitCodeRedirect.AuthenticationActivity" />
Then I created a class in the Android package, provided the required credentials to access to Google Drive.
public AndroidTestAar() {
GoogleDrive cs = new GoogleDrive(FXActivity.getInstance(), "****","****");
new Thread() {
#Override
public void run() {
List<CloudMetaData> metas = cs.getChildren("/");
Log.i("info", "Folder has " + metas.size() + " children");
for (CloudMetaData meta : metas) {
Log.i("info", "Child: " + meta.getName());
}
}
}.start();
}
This class is called from the View:
public BasicView(String name) {
...
button.setOnAction(e -> {
try {
Class c = Class.forName("com.testaar.AndroidTestAar");
c.newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {}
});
}
I could deploy the apk and run it.
The activity runs outside the JavaFX app, in an Android WebView.
First it asked me to sign in with my user/password, then it asked me about allowing offline access, and finally on the console it successfully listed all my files.