[英]Android Studio with Firebase Not Working
I'm trying to make a messenger Using Firebase. 我正在尝试使用Firebase制作Messenger。
And then android studio gives me an error. 然后android studio给我一个错误。 Execution failed for task ':app:dexDebug'.
任务':app:dexDebug'的执行失败。
Here's the error: 这是错误:
Execution failed for task ':app:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command: 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 Error Code: 2 Output:
com.android.ide.common.internal.LoggedErrorException:无法运行命令:C:\\ Users \\ *** \\ AppData \\ Local \\ Android \\ sdk \\ build-tools \\ 21.1.2 \\ dx.bat --dex-不优化-输出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)
Here's My Build.gradle: 这是我的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+'
}
My MainActivity: 我的主要活动:
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);
}
}
I Saved The Jar file in the folder app/libs/firebase-client-android-2.1.2.jar 我将Jar文件保存在app / libs / firebase-client-android-2.1.2.jar文件夹中
I got the <uses-permission android:name="android.permission.INTERNET"/>
in the Android-Manifest. 我在Android清单中得到了
<uses-permission android:name="android.permission.INTERNET"/>
。
Thanks in Advance! 提前致谢!
Solution: 解:
I included com.firebase.client.AuthData
twice, I had to delete the jar file and the dependencies in my build.gradle is enough. 我包括
com.firebase.client.AuthData
两次,我必须删除jar文件,并且build.gradle中的依赖项就足够了。 But now i get another error! 但是现在我又遇到了另一个错误!
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.
While i instantiated it in my mainactivity. 当我在mainactivity中实例化它时。 Any help?
有什么帮助吗?
I had to use Firebase.setAndroidContext(this);
我不得不使用
Firebase.setAndroidContext(this);
somewhere else. 别的地方。 You'll have to create a new java file that will look somethig like this
您必须创建一个新的java文件,看起来像这样
import com.firebase.client.Firebase;
public class MessengerApplication extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
Then you'll have to edit the androidManifest. 然后,您必须编辑androidManifest。 You'll have to add
android:name="<Your_Package_Name>.<JavaFile>"
您必须添加
android:name="<Your_Package_Name>.<JavaFile>"
This error means that you've included the com.firebase.client.AuthData
class in your project twice, which you're not permitted to do. 此错误意味着您已在项目中两次包含
com.firebase.client.AuthData
类,但不允许这样做。 By your description one copy is coming from: 根据您的描述,一个副本来自:
compile 'com.firebase:firebase-client-android:2.1.2+'
and the other is from: 另一个来自:
I Saved The Jar file in the folder app/libs/firebase-client-android-2.1.2.jar
我将Jar文件保存在app / libs / firebase-client-android-2.1.2.jar文件夹中
I would recommend removing the jar from your libs folder and just using the statement in your build script; 我建议从您的libs文件夹中删除jar,而仅使用构建脚本中的语句; that makes it easier to maintain.
这样更易于维护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.