安装了Firebase的Android Studio无效

时间:2015-02-13 08:31:35

标签: android android-studio firebase

我试图使用Firebase创建一个使者。

然后android studio给我一个错误。  任务':app:dexDebug'。

执行失败

这是错误:

Execution failed for task ':app:dexDebug'.
  

com.android.ide.common.internal.LoggedErrorException:无法运行命令:       C:\ Users \ *** \ AppData \ Local \ Android \ sdk \ build-tools \ 21.1.2 \ dx.bat --dex --no-optimize --output C:\ Users \ *** \ AndroidStudioProjects \ TheMessenger \ app \ build \ intermediates \ dex \ debug --input-list = C:\ Users \ *** \ AndroidStudioProjects \ TheMessenger \ app \ build \ intermediates \ tmp \ dex \ debug \ inputList.txt   错误代码:       2   输出:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/firebase/client/AuthData;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
    at com.android.dx.command.dexer.Main.run(Main.java:246)
    at com.android.dx.command.dexer.Main.main(Main.java:215)
    at com.android.dx.command.Main.main(Main.java:106)

这是我的Build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "joel.themessenger"
    minSdkVersion 12
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.firebase:firebase-client-android:2.1.2+'
}

我的主要活动:

public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Firebase.setAndroidContext(this);
}

Firebase myFirebaseRef = new Firebase("https://intense-inferno-9606.firebaseio.com/");
EditText messageInput = (EditText)findViewById(R.id.messageEdit);
String message;
String name = "Joël";

public void sendMessage(View v) {
    message = messageInput.getText().toString();
    Map<String, String> post = new HashMap<String, String>();
    post.put("name", name);
    post.put("text", message);
    myFirebaseRef.push().setValue(post);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

我在文件夹app / libs / firebase-client-android-2.1.2.jar中保存了Jar文件

我在Android-Manifest中获得了<uses-permission android:name="android.permission.INTERNET"/>

提前致谢!

解决方案:

我包括com.firebase.client.AuthData两次,我不得不删除jar文件,build.gradle中的依赖项就足够了。但现在我又得到了另一个错误!

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{joel.themessenger/joel.themessenger.MainActivity}: java.lang.RuntimeException: You need to set the Android context using Firebase.setAndroidContext() before using Firebase.

虽然我在主要活动中实例化了它。有什么帮助吗?

我不得不在其他地方使用Firebase.setAndroidContext(this);。您必须创建一个新的java文件,看起来像这样

 import com.firebase.client.Firebase;

    public class MessengerApplication extends android.app.Application {

        @Override
        public void onCreate() {
            super.onCreate();
            Firebase.setAndroidContext(this);
        }
    }

然后你必须编辑androidManifest。您必须添加android:name="<Your_Package_Name>.<JavaFile>"

1 个答案:

答案 0 :(得分:5)

此错误表示您已在项目中包含com.firebase.client.AuthData课程两次,但您不允许这样做。根据您的描述,一份副本来自:

compile 'com.firebase:firebase-client-android:2.1.2+'

另一个来自:

  

我在文件夹app / libs / firebase-client-android-2.1.2.jar中保存了Jar文件

我建议从你的libs文件夹中删除jar,只使用你的构建脚本中的语句;这使得维护更容易。