新手..请解决这个问题..当我尝试编译appcompat v7库时,我得到这个多个dex文件定义构建错误。任务':app:dexDebug'的执行失败。 这是代码::
MainActivity.java
import java.util.ArrayList;
import java.util.Currency;
import android.content.Context;
import android.os.Vibrator;
import android.speech.RecognizerIntent;
import android.app.Activity;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.support.v7.widget.ShareActionProvider;
import android.widget.Toast;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.support.v7.app.ActionBar;
import android.support.v4.app.ShareCompat;
@SuppressLint("DefaultLocale") public class MainActivity extends ActionBarActivity {
protected static final int REQUEST_OK = 1;
ListView l1;
ArrayList<Array_songs> songlist;
EditText e1;
Song_adapter adp;
private ShareActionProvider MenuSAP;
ImageButton ib;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
//actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.drawable.iconw);
l1=(ListView)findViewById(R.id.lv1);
ib=(ImageButton)findViewById(R.id.ib1);
songlist=new ArrayList<Array_songs>();
e1= (EditText)findViewById(R.id.editText1);
e1.addTextChangedListener(tw);
ib.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
try{
startActivityForResult(i,REQUEST_OK );
}catch(ActivityNotFoundException r){
Toast.makeText(getApplicationContext(),"song cant be played", Toast.LENGTH_SHORT).show();
}
}
});
adp=new Song_adapter(MainActivity.this, songlist, this.getApplication());
l1.setAdapter(adp);
getSongList("");
l1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
int sCB = getVar();
if(sCB == 0) {
Array_songs slist = songlist.get(position);
String Song_Title = slist.get_title();
String Song_Artist = slist.get_artist();
long Song_ID = slist.get_id();
//Toast.makeText(getApplicationContext(), Song_Artist + Song_Title + String.valueOf(Song_ID), Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
Uri song_path = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Song_ID);
Intent musicplayer = new Intent(Intent.ACTION_VIEW);
musicplayer.setDataAndType(song_path, "audio/*");
try {
startActivity(musicplayer);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "Try Again", Toast.LENGTH_SHORT).show();
}
}
else
{
}
}
});
l1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
changeVar();
vibrate();
int select = l1.getSelectedItemPosition();
l1.setItemChecked(select, true);
adp.notifyDataSetChanged();
return false;
}
});
}
private void vibrate() {
Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(120);
}
private void changeVar() {
((SyncClass) this.getApplication()).setShowCheckBox(1);
}
private int getVar()
{
Integer sCB = Integer.parseInt(((SyncClass) this.getApplication()).getShowCheckBox());
return sCB;
}
TextWatcher tw=new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
songlist.clear();
adp.notifyDataSetChanged();
getSongList(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
protected void onActivityResult(int requestcode,int resultcode,Intent data){
super.onActivityResult(requestcode, resultcode, data);
if(requestcode==REQUEST_OK && resultcode==RESULT_OK){
ArrayList<String> ls=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
e1.setText(ls.get(0));
}
}
public void getSongList(String stitle){
ContentResolver cr= getContentResolver();
Uri music=android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor mus=cr.query(music, null, null, null, null);
if(mus != null && mus.moveToFirst()){
int titleint=mus.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int artistint= mus.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int songid=mus.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
do{
String songTitle=mus.getString(titleint);
String songArtist=mus.getString(artistint);
long songId=mus.getLong(songid);
if(songTitle.toUpperCase().contains(stitle.toUpperCase())){
songlist.add(new Array_songs(songId, songTitle, songArtist));
}
}while(mus.moveToNext());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
MenuSAP = new ShareActionProvider(this);
MenuSAP.setShareIntent(createShareIntent());
MenuItemCompat.setActionProvider(item, MenuSAP);
return super.onCreateOptionsMenu(menu);
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("audio/*");
shareIntent.putExtra(Intent.EXTRA_MIME_TYPES,"audio/*");
return shareIntent;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
int check;
check = getVar();
if(check == 1)
{
((SyncClass) this.getApplication()).setShowCheckBox(0);
adp.notifyDataSetChanged();
}
else
{
super.onBackPressed();
}
}
}
SongAdapter.java
public class Song_adapter extends BaseAdapter {
private ArrayList<Array_songs> song_ob;
private LayoutInflater inflator_ob;
private Application xyz;
//private int sCB = ((SyncClass) this.getApplication()).getSomeVariable();
public Song_adapter(Context c,ArrayList<Array_songs> songlist, Application a)
{
song_ob=songlist;
inflator_ob= LayoutInflater.from(c);
xyz = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return song_ob.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout lin_ob =(LinearLayout)inflator_ob.inflate(R.layout.song_content, parent,false) ;
Array_songs ars_ob= song_ob.get(position);
TextView title= (TextView)lin_ob.findViewById(R.id.song_name);
TextView artist= (TextView)lin_ob.findViewById(R.id.artist);
//CheckBox check = (CheckBox) lin_ob.findViewById(R.id.cbselect);
if(((SyncClass) xyz).getShowCheckBox() == "1")
{
lin_ob.findViewById(R.id.cbselect).setVisibility(View.VISIBLE);
}
else
{
lin_ob.findViewById(R.id.cbselect).setVisibility(View.GONE);
}
String stitle= ars_ob.get_title();
String sartist= ars_ob.get_artist();
title.setText(stitle);
artist.setText(sartist);
return lin_ob;
}
}
SyncClass.java
public class SyncClass extends Application {
private String showCheckBox = "0";
public String getShowCheckBox()
{
return showCheckBox;
}
public void setShowCheckBox(int showCheckBox)
{
this.showCheckBox = Integer.toString(showCheckBox);
}
}
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="@+id/menu_item_share"
app:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
的build.gradle(模块:应用)
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.musicginni"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//compile 'com.android.support:support-v4:19.1.+'
compile "com.android.support:appcompat-v7:22.+"
}
logcat的:
Information:Gradle tasks [:app:assembleDebug]
:app:preBuild
:app:compileDebugNdk UP-TO-DATE
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:app:prepareComAndroidSupportAppcompatV72221Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42221Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources
:app:generateDebugSources
:app:compileDebugJava UP-TO-DATE
:app:preDexDebug UP-TO-DATE
:app:dexDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\Arpit\Desktop\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\build-tools\22.0.1\dx.bat --dex --no-optimize --output H:\AndroidStudioProjects\MusixMatch\app\build\intermediates\dex\debug --input-list=H:\AndroidStudioProjects\MusixMatch\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$LayoutParams;
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)
Information:BUILD FAILED
Information:Total time: 4.112 secs
Information:1 error
Information:0 warnings
Information:See complete output in console
答案 0 :(得分:0)
我复制了两个jar文件,一个用于Appcompat v7,另一个用于v4,用于我的app-&gt;导致错误的libs文件夹..一旦我删除了那些jar文件..它现在正常工作..