Android: NoClassDefError occurs in <4.0 but not >=4.0? - android

I am trying to launch a new intent from my Activity, but I get a NoClassDefFoundError. The exception occurs resolving MyClassB.class. Oddly I can resolve other classes, and the error only occurs in android versions 2.2 and 2.3, it works fine in 4.0+.
Both the class that causes the error and the other classes that resolve successfully are in the same package as the Activity where the code is executing. Basically, the below code gives the below error on 2.2/2.3, but works fine on 4.0+. I have also tried using the full package name like: com.me.MyClassB.class, but get the same error.
I realized this question is pretty vague, but am thoroughly confused and hoping that somebody might be able to help.
package com.me;
public class MyActivity extends Activity
{
protected onCreate(Bundle bundle)
{
super.onCreat(bundle);
Class a = MyClassA.class;
Class b = MyClassB.class;
}
}
01-17 10:37:36.473: E/AndroidRuntime(1976): java.lang.NoClassDefFoundError: com.me.MyClassB

I'm not sure if this is the problem but in your onCreate method you need to add super.onCreate(bundle) at the beginning e.g.
protected onCreate(Bundle bundle)
{
super.onCreate(bundle);
Class a = MyClassA.class;
Class b = MyClassB.class;
}

This was caused because MyClassB.class implements ActionBar.OnNavigationListener. It seems if classes contain certain code for a higher api level, they don't get setup properly at runtime, resulting in these sorts of errors. I expecteded MyClassB.class to crash on 2.3, but the NoClassDefFoundError was particularly mystifying.

Related

Trouble instantiating gdx-pay purchaseManager in my game

I am trying to set up gdx-pay in my game. I have followed the official readme (https://github.com/libgdx/gdx-pay/), but can't seem to instantiate the PurchaseManager in my AndroidLauncher.
I have set up a class that handles PurchaseManager installation, but every time I try to instantiate it in the AndroidLauncher I get this error:
"java.lang.RuntimeException: Unable to start activity ComponentInfo{appPackage.AndroidLauncher}: java.lang.IllegalArgumentException: Support for pending purchases must be enabled. Enable this by calling 'enablePendingPurchases()' on BillingClientBuilder."
The readme says to add this to the AndroidLauncher onCreate
game.purchaseManager = new PurchaseManagerGoogleBilling(this);
The problem is that I have to add this
ProjectName.purchaseManager = new PurchaseManagerGoogleBilling(this);
This give me an error saying that I have to make purchaseManager static in my ProjectName class
When I do this, I get the RuntimeException mentioned above.
The sample gdx-pay project instantiates PurchaseManager with this code
public class AndroidLauncher extends GenericAndroidLauncher {
#Override
protected void initFlavor(GdxPayApp game) {
super.initFlavor(game);
game.purchaseManager = new PurchaseManagerGoogleBilling(this);
}
}
When I try this, #Override is invalid and initFlavor does not exist. I tried looking for initFlavor in the gdx-pay files, but had no luck finding anything...
Thanks for taking the time to help
Well it turns out that the following line in my android gradle was the issue
implementation 'com.android.billingclient:billing:2.0.1'
Gdx-pay takes care of this automatically and uses client 1.1
Setting the billingclient to 2.0.1 was the problem. I was able to fix it by deleting that line. Big thanks to the libGdx discord guys!

Error searching for native library (libjnidispatch.so) using JNA to access .so file

Please note that this question is a follow-up question to another question I asked here some time ago.
What would I like to achieve?
I would like to use JNA (version 4.5.2 with Android Studio) to achieve the following:
First I would like to get a list of all callable functions from a given .so file.
Once I have this list, I would like to call a specific function
What did I do so far?
So far, the code of the file "MainActivita.java" of my test app looks as follows:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String MyLibraryString = MyLibrary.Instance.toString();
}
public interface MyLibrary extends Library
{
MyLibrary Instance = (MyLibrary ) Native.loadLibrary("nameOfMyLibraryWithoutSoExtension", MyLibrary.class);
}
}
What issues am I facing?
I used to get an error message that my library is not found on my Android device but after putting it in the "jniLibs" subfolder my app seems to be able to detect it. Unfortunately, I now get the error message
java.lang.UnsatisfiedLinkError: Native library
(com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource
path (.)
whenever I try to start the app on my device. I already tried to extract the file "libjnidispatch.so" from the JNA.jar file and put it in its respective subfolders (as described here) but that doesn't work.
The folder structure of the subfolder "app\src\main\jniLibs" of my app looks as follows:
app\src\main\jniLibs\arm64-v8a\myLibrary.so
...
app\src\main\jniLibs\arm64-v8a\libjnidispatch.so
app\src\main\jniLibs\armeabi\myLibrary.so
...
app\src\main\jniLibs\armeabi\libjnidispatch.so
app\src\main\jniLibs\armeabi-v7a\myLibrary.so
...
app\src\main\jniLibs\armeabi-v7a\libjnidispatch.so
app\src\main\jniLibs\mips\myLibrary.so
...
app\src\main\jniLibs\mips\libjnidispatch.so
app\src\main\jniLibs\mips64\myLibrary.so
...
app\src\main\jniLibs\mips64\libjnidispatch.so
app\src\main\jniLibs\x86\myLibrary.so
...
app\src\main\jniLibs\x86\libjnidispatch.so
app\src\main\jniLibs\x86_64\myLibrary.so
...
app\src\main\jniLibs\x86_64\libjnidispatch.so
Any help would be highly appreciated.

LogCat is unable to show exceptions

Suppose I have this piece of code:
public class Class1 extends Activity
{
private int [] array;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
someFunction(array.length); <--- NullPointerException
}
}
Normally the LogCat in eclipse tells me what the exception is, and where is the line that is causing trouble, but for an unknown reason, it does not anymore. Instead, it just throw the following error :
12-30 22:27:06.650: E/Trace(28894): error opening trace file: No such file or directory (2)
12-30 22:27:06.670: W/dalvikvm(28894): Refusing to reopen boot DEX '/system/framework/hwframework.jar'
How can I solve this? I´ve seen many posts speaking of this problem, but I haven´t been able to solve the issue. Thanx.
I found that the only way to solve the issue, was shutting down eclipse and starting again. I dont know why this happens, but restarting fixed it.

Millennial Media interstitial crash

In the onCreate() method of the activity I want the interstitial to appear in,
I have the following code:
MMAdView interAdView = new MMAdView(this, MY_APP_ID, MMAdView.FULLSCREEN_AD_LAUNCH, true, null);
interAdView.fetch();
interAdView.setListener(new BasicMMAdListener()
{
#Override
public void MMAdCachingCompleted(MMAdView adview, boolean success)
{
if(success)
adview.display();
}
});
The code compiles without error, but at runtime I get the following Log output
which indicates an error at the first line of my code snippet, i.e. the one
starting: MMAdView interAdView = new MMAdView(this, ...
Could not find class 'com.millennialmedia.android.MMAdView', referenced from method com.mycompany.mygame.myactivity.onCreate
I am puzzled because I thought that if there was somehow a missing
class then the app should not compile.
Thanks to Vinay's comment, it forced me to investigate another problem I'd been having first.
My problem disappeared when I resolve an earlier problem covered on SO here.

Android java.lang.NoSuchFieldError

In some case, when accessing an activity field from an anonymous class, I get NoSuchFieldError at runtime: java.lang.NoSuchFieldError: MyActivity.myField
EDIT: I'm now sure that it's an Android issue, because everything compiles correctly but resolved incorrectly at runtime. When changing to MyActivity.this.myField instead of plain myField, everything works perfect.
BTW, I rechecked and I don't have any other myField anywhere else, Also, when opening declaration in Eclipse, it goes to the intended field (myField).
The only problem is that I couldn't reproduce this in a sample project. I'm left only to state that it's a specific problem in my project, but I'm reluctant doing so and I can't publish this project.
I'll keep this open in case someone stumbles upon something similar.
Here's the code that I can publish that crashes my project but not the sample one:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Runnable() {
#Override
public void run() {
Toast.makeText(MyActivity.this, myField.getName(), Toast.LENGTH_SHORT).show();
}
}.run();
}
It seems that accessing the field with the class' prefix solves it: MyActivity.this.myField
Strange. I guess it's something with Android's field resolution in runtime.

Categories

Resources