我正试图在我的项目中使用谷歌地图方向
此处引用:http://www.akexorcist.com/2015/12/google-direction-library-for-android-en.html
但是它给了我一个错误->一个.dex文件中方法引用的数量不能超过64k
我的代码
public class Confirmation extends AppCompatActivity implements OnMapReadyCallback,DirectionCallback {
double locationLatMap;
double locationLongMap;
double destinationLatMap;
double destinationLongMap;
LatLng locationAPI=new LatLng(locationLatMap,locationLongMap);
LatLng DestinationAPI=new LatLng(destinationLatMap,destinationLongMap);
private GoogleMap mMap;
String serverKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx";
Location location=new Location("location");
Location destination=new Location("destination");
String start,end;
String cost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.confirmation_act);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//
EditText lat1=(EditText)findViewById(R.id.lat1);
EditText lat2=(EditText)findViewById(R.id.lat2);
Bundle bundle = getIntent().getExtras();
final Double d1=bundle.getDouble("d1");
final Double d2=bundle.getDouble("d2");
final Double d3=bundle.getDouble("d3");
final Double d4=bundle.getDouble("d4");
locationLatMap=d1;
locationLongMap=d2;
destinationLatMap=d3;
destinationLongMap=d4;
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(d1, d2, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);
lat1.setText("From: "+"\n"+cityName + " " + stateName + " " + countryName);
start=cityName + " " + stateName + " " + countryName;
} catch (IOException e) {
e.printStackTrace();
}
Geocoder geocoder1 = new Geocoder(this, Locale.getDefault());
List<Address> addresses1 = null;
try {
addresses1 = geocoder1.getFromLocation(d3, d4, 1);
String cityName = addresses1.get(0).getAddressLine(0);
String stateName = addresses1.get(0).getAddressLine(1);
String countryName = addresses1.get(0).getAddressLine(2);
lat2.setText("To: "+"\n"+cityName + " " + stateName + " " + countryName);
end=cityName + " " + stateName + " " + countryName;
} catch (IOException e) {
e.printStackTrace();
}
Button fareEstimate_confirm= (Button) findViewById(R.id.fareEstimate_Confirm);
fareEstimate_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
location.setLatitude(d1);
location.setLongitude(d2);
destination.setLatitude(d3);
destination.setLongitude(d4);
double distance = location.distanceTo(destination);
double base = 3;
double calc = base * distance;
NumberFormat nf = NumberFormat.getInstance(); // get instance
nf.setMaximumFractionDigits(2); // set decimal places
String s = nf.format(calc);
try {
TextView tv = (TextView) findViewById(R.id.price_confirm_tv);
tv.setText("Cost :"+"\n"+s.charAt(0)+ s.charAt(1) +s.charAt(2)+s.charAt(3)+s.charAt(4)+" EGP");
cost=tv.getText().toString();
}catch (Exception e){
}
}
});
}
public void confirmBtn_m (View view)
{
// Calculate Date&Time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd\nHH:mm");
String currentDateandTime = sdf.format(new Date());
// Calculate Cost
location.setLatitude(locationLatMap);
location.setLongitude(locationLongMap);
destination.setLatitude(destinationLatMap);
destination.setLongitude(destinationLongMap);
double distance = location.distanceTo(destination);
double base = 2;
double calc = base * distance;
NumberFormat nf = NumberFormat.getInstance(); // get instance
nf.setMaximumFractionDigits(2); // set decimal places
String s = nf.format(calc);
try {
cost=s.charAt(0) + "" + s.charAt(1) +" EGP";
}catch (Exception e){
}
// Creating New Trip
ParseGeoPoint location=new ParseGeoPoint(locationLatMap,locationLongMap);
final ParseObject trip= new ParseObject("Trip");
trip.put("source",start);
trip.put("destinations",end);
trip.put("cost",cost);
trip.put("date",currentDateandTime);
trip.put("status","Requesting");
trip.put("location",location);
//trip.put("sLat",locationLatMap);
//trip.put("sLong",locationLongMap);
trip.put("eLat",destinationLatMap);
trip.put("eLong",destinationLongMap);
ParseRelation<ParseUser> tripUser=trip.getRelation("user");
tripUser.add(ParseUser.getCurrentUser());
trip.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e==null)
{
Toast.makeText(Confirmation.this, "Request Successful !!\n Wait To Response ", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Confirmation.this,Requesting.class);
Bundle bundle = new Bundle();
bundle.putString("objectId",trip.getObjectId());
bundle.putDouble("d1",locationLatMap);
bundle.putDouble("d2",locationLongMap);
intent.putExtras(bundle);
startActivity(intent);
}
else {
Toast.makeText(Confirmation.this, "Requesting Failed !!", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
requestDirection();
}
public void requestDirection() {
GoogleDirection.withServerKey(serverKey)
.from(locationAPI)
.to(DestinationAPI)
.transportMode(TransportMode.DRIVING)
.execute(this);
}
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
mMap.addMarker(new MarkerOptions().position(locationAPI));
mMap.addMarker(new MarkerOptions().position(DestinationAPI));
ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();
mMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.DKGRAY));
}
@Override
public void onDirectionFailure(Throwable t) {
}
}
最佳答案
发生此错误的原因是应用程序超过了65K方法的限制。要解决此问题,必须启用多索引支持。
按如下所示创建一个应用程序类,并在manifest.xml中的application标记下注册。
public class YouApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
更新build.gradle文件
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 14 //lower than 14 doesn't support multidex
targetSdkVersion 22
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
Android官方文档Building Apps with Over 64K Methods