Reputation: 129
I'm trying to use Google Map Direction on my Project
referred here :http://www.akexorcist.com/2015/12/google-direction-library-for-android-en.html
but it gives me this Error -> The number of method references in a .dex file cannot exceed 64K
my code
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) {
}
}
Upvotes: 2
Views: 133
Reputation: 5741
This error occurs because your application crosses the limit of 65k method. To solve this problem, you must enable multidex support. Create an application class as follows and register in manifest.xml under appliation tag.
public class YouApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
Update your build.gradle file
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 official document Building Apps with Over 64K Methods
Upvotes: 2