java.lang.NoClassDefFoundError: Failed resolution of: Landroid/telephony/data/ApnSetting$Builder; - android

I'm trying to create a mobile application to add APN(Access Point Network) into mobile application. Then I used ApnSetting.Builder() according to the android documentation
I implemented it as following in MainActivity class:
package com.example.myapplication;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.telephony.data.ApnSetting;
import android.util.Log;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private static String TAG = "MainActivity";
#RequiresApi(api = Build.VERSION_CODES.P)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an MMS proxy address with a hostname. A network might not be
// available, so supply a dummy (0.0.0.0) IPv4 address to avoid DNS lookup.
String host = "mms.example.com";
byte[] ipAddress = new byte[4];
InetAddress mmsProxy;
try {
mmsProxy = InetAddress.getByAddress(host, ipAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
return;
}
ApnSetting apn = new ApnSetting.Builder()
.setApnTypeBitmask(ApnSetting.TYPE_DEFAULT | ApnSetting.TYPE_MMS)
.setApnName("apn.example.com")
.setEntryName("Example Carrier APN")
.setMmsc(Uri.parse("http://mms.example.com:8002"))
.setMmsProxyAddress(mmsProxy)
.setMmsProxyPort(8799)
.build();
ComponentName name = new ComponentName(getApplicationContext(), MainActivity.class);
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
devicePolicyManager.addOverrideApn(name, apn);
}
}
But it is showing the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 4314
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/telephony/data/ApnSetting$Builder;
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.telephony.data.ApnSetting$Builder" on path: DexPathList[[zip file "/data/app/com.example.myapplication-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:42) 
at android.app.Activity.performCreate(Activity.java:6609) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3113) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3275) 
at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:7007) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
Suppressed: java.lang.ClassNotFoundException: android.telephony.data.ApnSetting$Builder
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 15 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
So what should I do for that?

The ApnSetting has added in API level 28 means below API level 28 don't have that method ApnSetting.Builder(). That's why it's not working and causing ClassNotFoundException in Android API level < 28.
Option - 1:
If you really want to use this API and don't need to support older
devices just set the minSdkVersion to 28 in your build.gradle or
AndroidManifest.xml
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
...
minSdkVersion 28 // this is important
targetSdkVersion 29
...
}
}
Option - 2:
If your code is deliberately accessing newer APIs, and you have
ensured (e.g. with conditional execution) that this code will only
ever be called on a supported platform, then you can annotate your
class or method with the #TargetApi annotation specifying the local
minimum SDK to apply, such as #TargetApi(28), such that this check
considers 28 rather than your manifest file's minimum SDK as the
required API level.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
...
ApnSetting apn = new ApnSetting.Builder()
...
}

Related

Android unit test are failing with initializationError if RunWith RobolectricTestRunner

My Android unit test is failing with initializationError if RunWith RobolectricTestRunner.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.robolectric.RobolectricTestRunner;
import static junit.framework.Assert.assertEquals;
#RunWith(RobolectricTestRunner.class) // If I use #RunWith(Junit4.class) it works
#Config(application = Application.class)
public class LoginActivityTest {
#Test
public void sampleTest() {
assertEquals("actualMessage", "expectedErrorMessage");
}
}
Here is the entire stacktrace:
java.lang.TypeNotPresentException: Type [unknown] not present
at java.base/java.lang.reflect.Method.getDefaultValue(Method.java:682)
at java.base/sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:132)
at java.base/sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:267)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:121)
at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:73)
at java.base/java.lang.Class.createAnnotationData(Class.java:3757)
at java.base/java.lang.Class.annotationData(Class.java:3746)
at java.base/java.lang.Class.getAnnotation(Class.java:3651)
at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:213)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:47)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.NoClassDefFoundError: android/app/Application
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:398)
at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at java.base/sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:440)
at java.base/sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:421)
at java.base/sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:350)
at java.base/java.lang.reflect.Method.getDefaultValue(Method.java:674)
... 13 more
Caused by: java.lang.ClassNotFoundException: android.app.Application
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 31 more
testImplementation in build.gradle for robolectric is 'org.robolectric:robolectric:4.8.1'
jdk version 11
api level 31
gradle version 4.10.1
android studio ChipMunk
Please help! Thank You!

Android Studio, Db connection error: Something unusual has occurred to cause the driver to fail. Please report this exception

I'm opening this discussion because I have a big issue regarding a school project in Android that I'm working on.
Right now I have a free database with the online service ElephantSQL.
The database is perfect because when I try to connect it to a project written in java using IntelliJIDEA everything works fine.
However, if I try to connect the database with the same credentials (ip, username, password etc) in Android Studio with code written in Kotlin, the connection fails everytime.
I've been working on this the whole day and I've tried every solution I found on internet but it still doesn't work.
Here are my build.gradle file dependencies
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.myapplication"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'org.postgresql:postgresql:42.2.5.jre7'
implementation 'net.sourceforge.jtds:jtds:1.3.1'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
and this is my code
package com.example.myapplication
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException
var username = "myUsername";
var password = "password";
var url = "jdbc:postgresql://tyke.db.elephantsql.com:5432/myUsername";
private var connection: Connection? = null
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun start(view: View?) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.INTERNET),
PackageManager.PERMISSION_GRANTED
)
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
try {
Class.forName("org.postgresql.Driver")
connection = DriverManager.getConnection(url, username, password)
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show()
Log.d("client","connected")
} catch (e: ClassNotFoundException) {
e.printStackTrace()
Toast.makeText(this, "Class fail", Toast.LENGTH_SHORT).show()
Log.d("client","1 not connected")
} catch (e: SQLException) {
e.printStackTrace()
Log.d("client","2 not connected"+e)
Toast.makeText(this, "Connected no", Toast.LENGTH_SHORT).show()
}
}
}
This is the error that occurs every time
org.postgresql.util.PSQLException: The connection attempt failed.
I'm hoping for a little help because I'm stuck and I don't know what to do. Thank you in advance!
*This is the full stack trace of error, maybe it can be of help!
2022-05-25 10:28:25.143 20453-20453/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 20453
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
at android.view.View.performClick(View.java:7125) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) 
at android.view.View.performClickInternal(View.java:7102) 
at android.view.View.access$3500(View.java:801) 
at android.view.View$PerformClick.run(View.java:27336) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
at java.sql.DriverManager.getConnection(DriverManager.java:580)
at java.sql.DriverManager.getConnection(DriverManager.java:218)
at com.example.myapplication.MainActivity.start(MainActivity.kt:39)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
at android.view.View.performClick(View.java:7125) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) 
at android.view.View.performClickInternal(View.java:7102) 
at android.view.View.access$3500(View.java:801) 
at android.view.View$PerformClick.run(View.java:27336) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Caused by: java.net.SocketException: socket failed: EPERM (Operation not permitted)
at java.net.Socket.createImpl(Socket.java:492)
at java.net.Socket.connect(Socket.java:619)
at org.postgresql.core.PGStream.<init>(PGStream.java:70)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) 
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) 
at org.postgresql.Driver.makeConnection(Driver.java:454) 
at org.postgresql.Driver.connect(Driver.java:256) 
at java.sql.DriverManager.getConnection(DriverManager.java:580) 
at java.sql.DriverManager.getConnection(DriverManager.java:218) 
at com.example.myapplication.MainActivity.start(MainActivity.kt:39) 
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
at android.view.View.performClick(View.java:7125) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) 
at android.view.View.performClickInternal(View.java:7102) 
at android.view.View.access$3500(View.java:801) 
at android.view.View$PerformClick.run(View.java:27336) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
after a few days of digging, i was finally able to manage a solution for my problem.
When you edit the android manifest, you must uninstall (manually) your app from your emulator and reinstall it. It's the only way to make the changement definitive.
So, my problem was simply that... If someone in the future will be in the same situation, dear friend try my solution!

Cannot load class from .aar file when i added Room persistence library into my library

I am currently developing an alarm library, that needs to store multiple alarm details in sqlite database.
So I used google's Room persistence library, with my alarm library.
I have added below code to build.gradle.
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
I have added database class
#Database(entities = AlarmDetails.class, version = 1)
public abstract class AlarmDatabase extends RoomDatabase {
public abstract AlarmDao alarmDao();
}
I have also added Data Access Object
#Dao
public interface AlarmDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(AlarmDetails alarmDetails);
#Update
void update(AlarmDetails alarmDetails);
#Delete
void delete(AlarmDetails alarmDetails);
#Query("SELECT * FROM alarms")
List<AlarmDetails> getAll();
}
I am creating a database instance in the main class of my library as below
/*Constructor*/
public AlarmBase(Context mcontext,Context ncontext) {
this.mContext = mcontext;
alarmDatabase= Room.databaseBuilder(ncontext,
AlarmDatabase.class, "alarm_db")
.build();
}
i am calling the above constructor from my demo project and passing application context as below
Context applicationContext=MyApplicationClass.getContext();
alarmBase = new AlarmBase(this,applicationContext);
And this is my application class
public class MyApplicationClass extends MultiDexApplication {
private static MultiDexApplication instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static Context getContext() {
return instance.getApplicationContext();
}
}
But when I run the project I get the below error
--------- beginning of crash
02-21 20:23:34.643 11930-11930/com.mid.alarmdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mid.alarmdemo, PID: 11930
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/mid/alarmlib/roomsqlite/AlarmDatabase;
at com.mid.alarmlib.AlarmBase.<init>(AlarmBase.java:39)
at com.mid.alarmdemo.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6362)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.access$1100(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5613)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mid.alarmlib.roomsqlite.AlarmDatabase" on path: DexPathList[[zip file "/data/app/com.mid.alarmdemo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mid.alarmdemo-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.mid.alarmlib.AlarmBase.<init>(AlarmBase.java:39) 
at com.mid.alarmdemo.MainActivity.onCreate(MainActivity.java:54) 
at android.app.Activity.performCreate(Activity.java:6362) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548) 
at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5613) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
Suppressed: java.lang.NoClassDefFoundError: com.mid.alarmlib.roomsqlite.AlarmDatabase
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:226)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
at dalvik.system.DexPathList.findClass(DexPathList.java:338)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
... 16 more
Suppressed: java.lang.ClassNotFoundException: com.mid.alarmlib.roomsqlite.AlarmDatabase
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 15 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
I was able to run the project along with my library until I added Room persistence to my library
I just added below lines to the build.gradle(Module app) of the project also,
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
and it worked fine

Android App Shortcuts Broke in Lower Android Versions

Today, I was making an application with the newest Android 7.1 feature, App Shortcuts. At first, it runs well on Android 7.1.1. However, when I made it run on MIUI 8 on Android M, it broke unfortunately. I have wrote something to make it run on M or lower version, but it broke again. I was confused. I hope someone can give me a solution on this problem. I will be appreciated if someone can help me. Thanks anyway!
Here is my code.
findViewById(R.id.textView5).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addshortcut();
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
shortcutManager = getSystemService(ShortcutManager.class);
}
public void addshortcut(){
TextView textView = (TextView)findViewById(R.id.textView5);
String name = textView.getText().toString();
ShortcutInfo dynamicShortcut = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
dynamicShortcut = new ShortcutInfo.Builder(this, "shortcut_dynamic-zty")
.setShortLabel(name)
.setLongLabel(name)
.setIcon(Icon.createWithResource(this,R.drawable.ic_account_circle))
.setIntents(
new Intent[]{
new Intent(Intent.ACTION_VIEW, Uri.EMPTY,com.zengtianyu.gallery1.DetailZTY.this, DetailZTY.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
})
.build();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
shortcutManager.setDynamicShortcuts(Arrays.asList(dynamicShortcut));
}
Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
}
Here is the log.
java.lang.NoClassDefFoundError: Failed resolution of:Landroid/content/pm/ShortcutManager;
at com.zengtianyu.gallery1.DetailLSW.onCreate(DetailLSW.java:88)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2518)
at android.app.ActivityThread.access$1000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5544)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.content.pm.ShortcutManager" on path: DexPathList[[zip file "/data/app/com.zengtianyu.gallery1-1/base.apk"],nativeLibraryDirectories=[/data/app/com.zengtianyu.gallery1-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 14 more
Suppressed: java.lang.ClassNotFoundException: Didn't find class "android.content.pm.ShortcutManager" on path: DexPathList[[dex file "/data/dalvik-cache/xposed_XResourcesSuperClass.dex"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 15 more
Suppressed: java.lang.ClassNotFoundException:android.content.pm.ShortcutManager
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 16 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
You are passing ShortcutManager.class instead of the string constant SHORTCUT_SERVICE
Change line 8 to the following:
shortcutManager = (ShortcutManager)getSystemService(SHORTCUT_SERVICE);
If you wish to support older android versions, don't make shortcutManager a global variable and instead create it in the addShortcut() function and wrapping the usage of the function inside the sdk version check on line 7
So you should end up with:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
addShortcut();
}
...
public void addShortcut(){
ShortcutManager shortcutManager = (ShortcutManager)getSystemService(SHORTCUT_SERVICE);
...
}

class not found and null object reference exception out of nowhere

I have now completed all the scripting and designing with no compile time error but out of no where this run time error comes in and my app flashes away and do not get launched.
I have got all the files that it requires and everything done,
Please suggest now,
This is my MainActivity.java code thats probably throwing these exceptions
package com.example.dell.demologin;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.HashMap;
import activity.LoginActivity;
import helper.SQLiteHandler;
import helper.SessionManager;
public class MainActivity extends Activity {
private TextView txtName;
private TextView txtEmail;
private Button btnLogout;
private SQLiteHandler db;
private SessionManager session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
txtName = (TextView) findViewById(R.id.name);
txtEmail = (TextView) findViewById(R.id.email);
btnLogout = (Button) findViewById(R.id.btnLogout);
// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
String email = user.get("email");
// Displaying the user details on the screen
txtName.setText(name);
txtEmail.setText(email);
// Logout button click event
btnLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
logoutUser();
}
});
}
/**
* Logging out the user. Will set isLoggedIn flag to false in shared
* preferences Clears the user data from sqlite users table
* */
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
// Launching the login activity
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}
This is my logcat,
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.demologin, PID: 15578
java.lang.RuntimeException: Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.example.dell.demologin.AppController
at android.app.LoadedApk.makeApplication(LoadedApk.java:601)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4757)
at android.app.ActivityThread.access$1600(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5507)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.example.dell.demologin.AppController
at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:220)
at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239)
at android.app.Application.attach(Application.java:187)
at android.app.Instrumentation.newApplication(Instrumentation.java:1001)
at android.app.Instrumentation.newApplication(Instrumentation.java:985)
at android.app.LoadedApk.makeApplication(LoadedApk.java:586)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4757) 
at android.app.ActivityThread.access$1600(ActivityThread.java:159) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5507) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.ClassNotFoundException: com.example.dell.demologin.AppController
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:324)
at java.lang.Class.forName(Class.java:285)
at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:209)
at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239) 
at android.app.Application.attach(Application.java:187) 
at android.app.Instrumentation.newApplication(Instrumentation.java:1001) 
at android.app.Instrumentation.newApplication(Instrumentation.java:985) 
at android.app.LoadedApk.makeApplication(LoadedApk.java:586) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4757) 
at android.app.ActivityThread.access$1600(ActivityThread.java:159) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5507) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.dell.demologin.AppController" on path: DexPathList[[zip file "/data/app/com.example.dell.demologin-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.dell.demologin-2/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at java.lang.Class.classForName(Native Method) 
at java.lang.Class.forName(Class.java:324) 
at java.lang.Class.forName(Class.java:285) 
at com.android.tools.fd.runtime.BootstrapApplication.createRealApplication(BootstrapApplication.java:209) 
at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:239) 
at android.app.Application.attach(Application.java:187) 
at android.app.Instrumentation.newApplication(Instrumentation.java:1001) 
at android.app.Instrumentation.newApplication(Instrumentation.java:985) 
at android.app.LoadedApk.makeApplication(LoadedApk.java:586) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4757) 
at android.app.ActivityThread.access$1600(ActivityThread.java:159) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5507) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.example.dell.demologin.AppController" on path: DexPathList[[dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-support-annotations-23.4.0_2f85958e3577c0887206665b958d529f2917b10d-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-mysql-connector-java_910883ba1d0637ad761451367a22e76261a44141-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-jtds_dd6bef0a517729fc55f6897f784dfc3379233a03-classes.dex", dex file "/data/data/com.example.dell.demologin/files/instant-run/dex/slice-internal_impl-23.4.0_acbefc0422230ccc1164ab
E/AndroidRuntime: Error reporting crash
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Application.getApplicationContext()' on a null object reference
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:96)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
I/Process: Sending signal. PID: 15578 SIG: 9
Application terminated.
Known bug in the Google Play store upgrade process, https://code.google.com/p/android/issues/detail?id=56296

Categories

Resources