M.A.Murali
M.A.Murali

Reputation: 10148

Android studio : java.lang.NoClassDefFoundError

I have imported my project from Eclipse to Android Stdio and its runs on Lollipop devices if I run on kitkat devices it gives me "No class def found" exception.

In my project I have two packages 1. com.qapp which has core functionality class and 2. om.qapp.common_class which has commonly used functionality class, for example I have UtillClass,there is a method called showOkAlert it is used for show an alert dialog with "ok" button.

If I called the showOkAlert methods from an Activity, its successfully executed in all Lollipop devices and other version devices gives me java.lang.NoClassDefFoundError.

Code sample:

package com.qapp.common_class;

public class UtillClass {
        // static method
    public static void showOkAlert(final Context context, final String msgBody) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        alertDialogBuilder.setTitle(context.getResources().getString(
                R.string.alert_msg_title));
        alertDialogBuilder.setMessage(msgBody).setCancelable(false)
                .setPositiveButton("OK", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

}

And I have used showOkAlert method in an Activity as follows:

package com.qapp;

import com.qapp.common_class.UtillClass;

    public class TestActivity2 extends Activity {

        private Button sendPush, gotoQnow;
        .............
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.gonow);
            gotoQnow = (Button) findViewById(R.id.gotonow_btn);
            .............
            gotoQnow.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    UtillClass.showOkAlert(TestActivity2.this,"hello"); // I get NoClassDefFoundError here Line no #53
                }
            });

        }
    }

Note:

Please help me to resolve this in Android studio to works all in Android versions, I have spend more than One day for it but I could not found solution. If you need more information please leave a comment here.

My Log here:

java.lang.NoClassDefFoundError: com.qapp.common_class.UtillClass$1
at com.qapp.common_class.UtillClass.showOkAlert(UtillClass.java:382)
at com.qapp.TestActivity2$1.onClick(TestActivity2.java:53)
at android.view.View.performClick(View.java:3528)
at android.view.View$PerformClick.run(View.java:14235)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)

java.lang.NoClassDefFoundError: com.qapp.common_class.UtillClass$1
at com.qapp.common_class.UtillClass.showOkAlert(UtillClass.java:382)
at com.qapp.TestActivity2$1.onClick(TestActivity2.java:53)
at android.view.View.performClick(View.java:3528)
at android.view.View$PerformClick.run(View.java:14235)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)

my whole imports are here for Utillclass

import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.TimeZone;

import org.json.JSONArray;
import org.json.JSONObject;

import retrofit.RetrofitError;
import retrofit.client.Response;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Base64;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

Upvotes: 4

Views: 2237

Answers (2)

M.A.Murali
M.A.Murali

Reputation: 10148

I have found out a solution for NoClassDefFoundException in Android studio. I have added this in my Manifest XML

<application
    android:name="android.support.multidex.MultiDexApplication"
    ............
</application> 

Add multiDexEnabled true in defaultConfig

defaultConfig {
    .......
    multiDexEnabled true
}

it is working now and nothing changed in my source code. Thanks for all.

Upvotes: 4

Blundell
Blundell

Reputation: 76466

From your LogCat:

java.lang.NoClassDefFoundError: com.qapp.common_class.UtillClass$1 at com.qapp.common_class.UtillClass.showOkAlert(UtillClass.java:382)

The $1 means the first 1 inner class $, so the first inner class at line 382's class definition cannot be found.

It appears the compilation / classloader step was making a mistake. It was getting http://developer.android.com/reference/android/view/View.OnClickListener.html

and

http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html

mixed up.

If you pre-fix the OnClickListener at line 382 with DialogInterface like this:

.setPositiveButton("OK", new DialogInterface.OnClickListener()

and remove the import import android.content.DialogInterface.OnClickListener; from the top of the class. This tells the compiler more explicitly what you are expecting to happen.

Upvotes: 0

Related Questions