0

I have used following code to get turn on gps like OLA app. this below code run successfully in Android Studio with play services 8.4.0 . But, I want to run this in Eclipse with ADT 23.

i have searched in latest google playservices in sdk. its not available. then, finally, i found latest aar8.4.0 file. after i have converted AAR to normal Eclipse Library project and import into eclipse and refresh and add as a library in to my main project.

Still am getting this error..Following API's not supported in main source code activity.

import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; import com.google.android.gms.location.LocationRequest;

my code:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); settingsrequest(); } //------------------------------------------------------------------------------------

public void settingsrequest()
{
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);

    builder.setAlwaysShow(true); //this is the key ingredient

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {

    // Check for the integer request code originally supplied to startResolutionForResult().
        case REQUEST_CHECK_SETTINGS:

            switch (resultCode) {

            case Activity.RESULT_OK:

                //startLocationUpdates();

                GetLocation() ;

                break;

            case Activity.RESULT_CANCELED:

            settingsrequest();//keep asking if imp or do whatever

            break;
            }

            break;
    }
}

//---------------------------------------------------------------------

Note: This code in Perfectly working in Android Studio 2.1. and saw output like directly turn on gps with dialog yes/no itself, without go to settings page.

But, I want the same output in Eclipse. I am also found the problem, where is? problem is for Google Play services only.Now google API providing library for AAR format only( studio acceptable).

Please help to get solution.

harikrishnan
  • 1,985
  • 4
  • 32
  • 63

1 Answers1

0

GRADLE BUILD FILE

Add the following to the end of your Android projects build.gradle :

task copyJarDependencies(type: Copy) {
    description = 'Used for Eclipse. Copies all dependencies to the libs directory. If there are any AAR files it will extract the classes.jar and rename it the same as the AAR file but with a .jar on the end.'
    libDir = new File(project.projectDir, '/libs')
    println libDir
    println 'Adding dependencies from compile configuration'
    configurations.compile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
    println 'Adding dependencies from releaseCompile configuration'
    configurations.releaseCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
    println 'Adding dependencies from debugCompile configuration'
    configurations.debugCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
    println 'Adding dependencies from instrumentTestCompile configuration'
    configurations.instrumentTestCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
    println 'Extracting dependencies from compile configuration'
    configurations.compile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
    println 'Extracting dependencies from releaseCompile configuration'
    configurations.releaseCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
    println 'Extracting dependencies from debugCompile configuration'
    configurations.debugCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
    println 'Extracting AAR dependencies from instrumentTestCompile configuration'
    configurations.instrumentTestCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
}

void moveJarIntoLibs(File file){
    println 'Added jar ' + file
        copy{
            from file
            into 'libs'
        }
}

void moveAndRenameAar(File file){
    println 'Added aar ' + file
    def baseFilename = file.name.lastIndexOf('.').with {it != -1 ? file.name[0..<it] : file.name}

    // directory excluding the classes.jar
    copy{
        from zipTree(file)
        exclude 'classes.jar'
        into 'libs/'+baseFilename
    }

    // Copies the classes.jar into the libs directory of the expoded AAR.
    // In Eclipse you can then import this exploded ar as an Android project
    // and then reference not only the classes but also the android resources :D 
    copy{
        from zipTree(file)
        include 'classes.jar'
        into 'libs/' + baseFilename +'/libs'
        rename { String fileName ->
            fileName.replace('classes.jar', baseFilename + '.jar')
        }
    }
}

BUILDING WITH GRADLE

Run gradle clean build ( or within Eclipse right-click build.gradle Rus As -> Gradle build )

You should find all dependencies and exploded AARs in your libs directory. This is all Eclipse should need.

IMPORTING IN ECLIPSE

Now this is where the real benefit begins. After you've generated the libs directory from the gradle step above you'll notice there are folders in there too. Those new folders are the exploded AAR dependencies from your build.gradle file.

Now the cool part is that when you import your existing Android project into Eclipse it will also detect the exploded AAR folders as projects it can import too!

  1. Import those folders under your project's libs directory, don't import any 'build' folders, they're generated by Gradle

  2. Ensure you perform a Project -> Clean on all AAR projects you've added. In your workspace check that each AAR exploded project has the following in the project.properties :

target=android- android.library=true 3. Now in your main Android project you can just add the library references with either ADT or you can just edit the project.properties file and add

android.libraries.reference.1=libs/someExplodedAAR/
  1. Now you can right-click on your main Android project and Run as -> Android Application.

Info from:

http://www.nodeclipse.org/projects/gradle/android/aar-for-Eclipse

Miguel Benitez
  • 2,322
  • 10
  • 22
  • i have done for convert aar to android library project for following steps and add library project in to my main source code. but, its showing successfully added. But, google play-services API not import. still same error. – harikrishnan Jul 27 '16 at 10:13
  • if possible can u give convert latest arr play services8.4.0 as a library project with import into eclipse format. or if you want my source code with library means, i will upload and give link to you. if possible pls check and revert me back, what i have done mistake. – harikrishnan Jul 27 '16 at 10:14
  • Try doing it has google explain here: https://developers.google.com/android/guides/setup Eclipse with ADT section – Miguel Benitez Jul 27 '16 at 10:20
  • thank you reply. thats my problem here:ECLIPSE WITH ADT here /extras/google/google_play_services/libproject/google-play-services_lib/.... after updated Google Play Services(Rev 31) in SDK, that existing google play services is deleted automatically on that mentioned place.. if that place, available playservices library project means, i will use directly. that is my problem playservices location Link: https://postimg.org/image/3magqljyf/ – harikrishnan Jul 27 '16 at 11:12