作者:Young_阳先生 | 来源:互联网 | 2023-05-22 10:35
我正在尝试使用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 post = new HashMap();
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);
}
}
我将Jar文件保存在app/libs/firebase-client-android-2.1.2.jar文件夹中
我得到了
Android-Manifest.
提前致谢!
解:
我包括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="."
1> Scott Barta..:
此错误意味着您已将com.firebase.client.AuthData
该类包含在项目中两次,但您不允许这样做.根据您的描述,一份副本来自:
compile 'com.firebase:firebase-client-android:2.1.2+'
另一个来自:
我将Jar文件保存在app/libs/firebase-client-android-2.1.2.jar文件夹中
我建议从libs文件夹中删除jar,只需在构建脚本中使用该语句; 这使得维护更容易.