当我尝试实现android许可检查时,我得到NoClassDefFoundError:com.XX.XXXXXXX.LicenseCheck $ MyLicenseCheckerCallback

时间:2013-07-06 07:50:27

标签: android

这是我的班级:

import com.google.android.vending.licensing.AESObfuscator;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.ServerManagedPolicy;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.Toast;

public class LicenseCheck extends Activity {
    private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
        @Override
        public void allow(int reason) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            // Should allow user access.
            startMainActivity(true);
        }

        @Override
        public void applicationError(int errorCode) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            // This is a polite way of saying the developer made a mistake
            // while setting up or calling the license checker library.
            // Please examine the error code and fix the error.
            toast("Error Code: " + errorCode);
            startMainActivity(false);

        }

        @Override
        public void dontAllow(int reason) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }

            // Should not allow access. In most cases, the app should assume
            // the user has access unless it encounters this. If it does,
            // the app should inform the user of their unlicensed ways
            // and then either shut down the app or limit the user to a
            // restricted set of features.
            // In this example, we show a dialog that takes the user to Market.
            showDialog(0);
            startMainActivity(false);
        }
    }

    private static final String BASE64_PUBLIC_KEY = "PLACE YOUR BASE KEY FROM GOOGLE HERE";

    private static final byte[] SALT = new byte[] { 13, 32, 81, 65, 53, 82, 18, 100, -69, -17, 51, 79, -15, 86, -10, -40, 19, 45, 63, -7 };
    private LicenseChecker mChecker;

    // A handler on the UI thread.

    private LicenseCheckerCallback mLicenseCheckerCallback;

    private void doCheck() {

        mChecker.checkAccess(mLicenseCheckerCallback);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Try to use more data here. ANDROID_ID is a single point of attack.
        String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

        // Library calls this when it's done.
        mLicenseCheckerCallback = new LicenseCheck.MyLicenseCheckerCallback();
        // Construct the LicenseChecker with a policy.
        mChecker = new LicenseChecker(this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY);
        doCheck();

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // We have only one dialog.
        return new AlertDialog.Builder(this).setTitle("Application Not Licensed").setCancelable(false).setMessage("This application is not licensed. In order to use this feature Please purchase it from Android Market")
                .setPositiveButton("Buy App", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/details?id=" + getPackageName()));
                        startActivity(marketIntent);
                        finish();
                    }
                }).setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).create();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mChecker.onDestroy();
    }

    private void startMainActivity(boolean isLicensed) {
        Intent intent = new Intent(this, MainActivity.class);
        Bundle b = new Bundle();
        b.putBoolean(CommunicationStrings.isLicensed, isLicensed);
        intent.putExtras(b); //Put your id to your next Intent
        startActivity(intent); // REPLACE MainActivity.class WITH YOUR APPS ORIGINAL LAUNCH ACTIVITY
        finish();
    }

    public void toast(String string) {
        Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
    }
}

这是logcat:

07-06 10:41:02.765: E/AndroidRuntime(24277): FATAL EXCEPTION: main
07-06 10:41:02.765: E/AndroidRuntime(24277): java.lang.NoClassDefFoundError: com.XX.XXXXXXX.LicenseCheck$MyLicenseCheckerCallback
07-06 10:41:02.765: E/AndroidRuntime(24277):    at com.ck.autoWiFi3G.LicenseCheck.onCreate(LicenseCheck.java:85)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.app.Activity.performCreate(Activity.java:5206)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.os.Looper.loop(Looper.java:137)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-06 10:41:02.765: E/AndroidRuntime(24277):    at dalvik.system.NativeStart.main(Native Method)

我发现这篇帖子谈到了这个错误:noclassdeferror for inner class MyLicenseCheckerCallback

但它没有帮助我。我尝试了干净的项目,添加google play license作为jar(在构建路径中)文件。 什么都没有帮助...

可能是什么问题?

2 个答案:

答案 0 :(得分:0)

你可以试试这个.. 右键单击Project - > build Path - > Configure Build Path - > java build path - > order and export tab - > check check on jar - > click ok

现在清理项目

答案 1 :(得分:0)

我有完全相同的例外,上述建议均不适合我。

然而,通过确保您的LVL项目是一个图书馆项目(右键单击项目 - >属性 - > android 并勾选“Is Libary”checkbo),我得到了我的工作。

要在您的应用程序项目中包含它,请删除对此的任何其他引用并应用更改。然后,如果还没有导航到(右键单击应用程序项目 - >属性 - > android ),并在库部分单击添加并选择您的LVL项目。应用更改。好又干净的项目。

希望这有助于某人。