ormlite - 由以下引起:java.lang.IllegalArgumentException:类com.github.snowdream.android.a.a.j中没有字段具有DatabaseField批注

时间:2015-05-14 15:04:17

标签: android ormlite

我正在使用ormlite 4.46。

我收到错误,"引起:java.lang.IllegalArgumentException:没有字段在类com.github.snowdream.android.a.a.j"中有一个DatabaseField注释。对于以下场景,

/*
 * Copyright (C) 2013 Snowdream Mobile <yanghui1986527@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.snowdream.android.app.downloader;

import android.content.Context;
import android.text.TextUtils;
import android.webkit.URLUtil;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * The download task
 *
 * @author snowdream <yanghui1986527@gmail.com>
 * @version v1.0
 * @date Sep 29, 2013
 */
@DatabaseTable(tableName = "downloadtask")
public class DownloadTask extends Object {
    @DatabaseField(persisted = false)
    private Context context = null;
    /**
     * the time of finish downloading
     */
    @DatabaseField(columnName = "finishTime")
    private long finishTime = 0;
    /**
     * id
     */
    @DatabaseField(columnName = "id",generatedId = true, canBeNull = false)
    private int id = 0;

    @DatabaseField(persisted = false)
    private DownloadListener listener = null;
    /**
     * mimetype
     */
    @DatabaseField(columnName = "mimeType")
    private String mimeType = "";
    /**
     * name
     */
    @DatabaseField(columnName = "name")
    private String name = "";
    /**
     * the save path on the sdcard
     */
    @DatabaseField(columnName = "path",canBeNull = false)
    private String path = "";
    /**
     * total size
     */
    @DatabaseField(columnName = "size")
    private long size = 0;
    /**
     * the time of start downloading
     */
    @DatabaseField(columnName = "startTime")
    private long startTime = 0;
    /**
     * download status
     */
    @DatabaseField(columnName = "status")
    private int status = DownloadStatus.STATUS_PENDING;

    @DatabaseField(persisted = false)
    private AsycDownloadTask task = null;
    /**
     * type
     */
    @DatabaseField(columnName = "type")
    private int type = DownloadType.TYPE_UNKNOWN;
    /**
     * url
     */
    @DatabaseField(columnName = "url",canBeNull = false)
    private String url = "";

    @SuppressWarnings("unused")
    private DownloadTask() {
    }

    public DownloadTask(Context context) {
        super();
        this.context = context;
    }

    public DownloadTask(DownloadTask task) {
        if (task == null) return;

        this.context = task.context;
        this.startTime = task.startTime;
        this.finishTime = task.finishTime;
        this.id = task.id;
        this.mimeType = task.mimeType;
        this.name = task.name;
        this.path = task.path;
        this.size = task.size;
        this.status = task.status;
        this.type = task.type;
        this.url = task.url;
        this.task = task.task;
        this.listener = task.listener;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return url.hashCode();
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public long getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(long finishTime) {
        this.finishTime = finishTime;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public DownloadListener getListener() {
        return listener;
    }

    public void setListener(DownloadListener listener) {
        this.listener = listener;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * url, name and path is not empty.
     *
     * @return true is valid,otherwise not.
     */
    public boolean isComplete() {
        return !TextUtils.isEmpty(url) && !TextUtils.isEmpty(name) && !TextUtils.isEmpty(path);
    }

    /**
     * Check whether the Task is valid.
     *
     * @return true is valid,otherwise not.
     */
    public boolean isValid() {
        return URLUtil.isNetworkUrl(url);
    }

    protected void setDownloadTask(DownloadTask task) {
        //this.context = task.context;
        this.startTime = task.startTime;
        //this.finishTime = task.finishTime;
        this.id = task.id;
        if (TextUtils.isEmpty(this.mimeType)) {
            this.mimeType = task.mimeType;
        }
        if (TextUtils.isEmpty(this.name)) {
            this.name = task.name;
        }
        if (TextUtils.isEmpty(this.path)) {
            this.path = task.path;
        }
        this.size = task.size;
        this.status = task.status;
        this.type = task.type;
        this.url = task.url;
        //this.task = task.task;
        //this.listener = task.listener;
    }

    /**
     * Start the Task
     *
     * @param context  Context
     * @param listener DownloadListener
     */
    @SuppressWarnings({
            "rawtypes", "unchecked"
    })
    protected void start(Context context, DownloadListener listener,boolean isOnlyGetHead) {
        //Get context,which will be used to communicate with sqlite.
        if (this.context == null && context != null) {
            this.context = context;
        }

        if (task != null) {
            task.cancel(false);
        }

        task = new AsycDownloadTask(listener,isOnlyGetHead);
        task.execute(this);
    }

    /**
     * Stop the task
     */
    protected void stop(){
        setStatus(DownloadStatus.STATUS_STOPPED);

        if (task != null) {
            task.cancel(false);
        }
    }


    /**
     * Delete the task
     */
    protected void delete(){
        setStatus(DownloadStatus.STATUS_DELETED);

        if (task != null) {
            task.cancel(false);
        }
    }
}

但是得到如下错误:

E/AndroidRuntime(30057): Caused by: java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in class com.github.snowdream.android.a.a.j
E/AndroidRuntime(30057):        at com.j256.ormlite.table.DatabaseTableConfig.void extractFieldTypes(com.j256.ormlite.support.ConnectionSource)(Unknown Source)
                                                                              com.j256.ormlite.field.FieldType[] extractFieldTypes(com.j256.ormlite.support.ConnectionSource,java.lang.Class,java.lang.String)
E/AndroidRuntime(30057):        at com.j256.ormlite.table.DatabaseTableConfig.com.j256.ormlite.table.DatabaseTableConfig fromClass(com.j256.ormlite.support.ConnectionSource,java.lang.Class)(Unknown Source)
E/AndroidRuntime(30057):        at com.j256.ormlite.table.TableInfo.<init>(Unknown Source)
E/AndroidRuntime(30057):        at com.j256.ormlite.dao.BaseDaoImpl.void initialize()(Unknown Source)
E/AndroidRuntime(30057):        at com.j256.ormlite.dao.BaseDaoImpl.<init>(Unknown Source)
E/AndroidRuntime(30057):        at com.j256.ormlite.dao.BaseDaoImpl.<init>(Unknown Source)
E/AndroidRuntime(30057):        at com.j256.ormlite.dao.BaseDaoImpl$4.<init>(Unknown Source)
E/AndroidRuntime(30057):        at com.j256.ormlite.dao.BaseDaoImpl.com.j256.ormlite.dao.Dao createDao(com.j256.ormlite.support.ConnectionSource,java.lang.Class)(Unknown Source)
                                                                    com.j256.ormlite.dao.Dao createDao(com.j256.ormlite.support.ConnectionSource,com.j256.ormlite.table.DatabaseTableConfig)
E/AndroidRuntime(30057):        at com.j256.ormlite.dao.DaoManager.com.j256.ormlite.dao.Dao createDao(com.j256.ormlite.support.ConnectionSource,java.lang.Class)(Unknown Source)
                                                                   com.j256.ormlite.dao.Dao createDao(com.j256.ormlite.support.ConnectionSource,com.j256.ormlite.table.DatabaseTableConfig)
E/AndroidRuntime(30057):        at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.com.j256.ormlite.dao.Dao getDao(java.lang.Class)(Unknown Source)
E/AndroidRuntime(30057):        at com.github.snowdream.android.app.downloader.dao.DatabaseHelper.com.github.snowdream.android.app.downloader.dao.DatabaseHelper getHelper(android.content.Context)(Unknown Source)
                                                                                                  com.j256.ormlite.dao.Dao getTaskDao()
E/AndroidRuntime(30057):        at com.github.snowdream.android.app.downloader.dao.ISqlImpl.com.github.snowdream.android.app.downloader.DownloadTask queryDownloadTask(com.github.snowdream.android.app.downloader.DownloadTask)(Unknown Source)
E/AndroidRuntime(30057):        at com.github.snowdream.android.app.downloader.DownloadManager.void a(com.github.snowdream.android.app.downloader.DownloadManager$a,com.github.snowdream.android.app.downloader.DownloadTask,com.github.snowdream.android.app.downloader.DownloadListener,java.lang.Integer)(Unknown Source)
                                                                                               void add(com.github.snowdream.android.app.downloader.DownloadTask,com.github.snowdream.android.app.downloader.DownloadListener)
E/AndroidRuntime(30057):        at com.github.snowdream.android.apps.downloader.MainActivity.void onCreate(android.os.Bundle)(Unknown Source)
E/AndroidRuntime(30057):        at android.app.Activity.performCreate(Activity.java:5990)
E/AndroidRuntime(30057):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
E/AndroidRuntime(30057):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
E/AndroidRuntime(30057):        ... 10 more
W/ActivityManager(  784):   Force finishing activity 1 com.github.snowdream.android.apps.downloader/.MainActivity

1 个答案:

答案 0 :(得分:2)

我的答案来自here

提供解决方案可能已经晚了但这是我的解决方案:
你看到proguard尝试混淆代码,如果你读深度或介绍proguard

http://proguard.sourceforge.net/FAQ.html

在proguard中萎缩 缩减程序(如ProGuard)可以分析字节码并删除未使用的类,字段和方法。 因此我们可以假设它正在删除你的对象,因为它没有被任何地方使用......

那么您可能需要什么? 你需要阻止proguard从过程中推卸那些方法或对象。

检查以下行

-keep class com.j256.**<br>
-keepclassmembers class com.j256.** { *; }
-keep enum com.j256.**
-keepclassmembers enum com.j256.** { *; }
-keep interface com.j256.**
-keepclassmembers interface com.j256.** { *; }

此行将防止专业人员删除我的公共方法和变量。

-keepclassmembers class classpath.** { public *; }

你需要为atlest id写一个列名...因为它会搜索它并且会改变它的名字......所以你需要为主键定义列名id ..