0

我已经使用下面的代码来打开gps,比如OLA应用。以下代码在Android Studio中使用播放服务8.4.0成功运行。 但是,我想在Eclipse中使用ADT 23运行此操作。Android需要导入aar play服务8.4.0到eclipse

我已经在sdk中搜索了最新的google playservices。它不可用。然后,最后,我发现最新的aar8.4.0文件。在我已经将AAR转换为正常的Ec​​lipse库项目并导入到Eclipse并刷新并添加为我的主项目库之后。

我仍然得到这个错误。以下API在主要源代码活动中不受支持。

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;

我的代码:

@覆盖 公共无效的onCreate(捆绑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; 
    } 
} 

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

注意:此代码完美适用于Android Studio 2.1。并看到输出像直接打开全球定位系统与对话框是/否本身,没有去设置页面。

但是,我想要在Eclipse中相同的输出。我也发现问题在哪里? 问题仅适用于Google Play服务。现在Google API仅为AAR格式提供库(工作室可接受)。

请帮忙解决。

回答

0

摇篮构建文件

以下内容添加到您的Android项目结束的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') 
     } 
    } 
} 

与摇篮

运行gradle这个干净建造建筑物(或在Eclipse中右键单击build.gradle Rus As - > Gradle build)

您应该在libs目录中找到所有依赖关系和爆炸的AAR。这是Eclipse所需要的。

IMPORTING在Eclipse

现在,这是真正的好处开始的地方。从上面的gradle步骤生成libs目录后,您会注意到其中还有文件夹。这些新文件夹是来自build.gradle文件的分解AAR依赖关系。

现在很酷的部分是,当您将现有的Android项目导入到Eclipse中时,它也将检测到爆炸的AAR文件夹作为它可以导入的项目!

  1. 导入您的项目的libs目录下的这些文件夹,不导入任何“建设”的文件夹,它们是由摇篮

    产生
  2. 确保您执行项目 - 在所有AAR的项目,你>清洁补充说。在工作区中检查每个AAR爆炸项目在project.properties如下:

目标= android- android.library =真 3.现在你主要的Android项目,你可以只添加库> Android应用程序 - 无论是与ADT或者你可以编辑project.properties文件,并添加

android.libraries.reference.1=libs/someExplodedAAR/ 
  • 现在,在您的主要Android项目,你可以用鼠标右键点击并运行作为参考。从
  • 信息:

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

    +0

    我有转换AAR进行到Android库项目下面的步骤,并在我的主要源代码添加库项目。但是,它的展示成功添加了。但是,google play-services API不能导入。还是一样的错误。 – harikrishnan

    +0

    如果可能的话,你可以把最新的arr play services8.4.0作为一个库项目导入到eclipse格式。或者如果你想要我的源代码库的手段,我会上传并给你链接。如果可能的话请检查并恢复我,我犯了什么错误。 – harikrishnan

    +0

    尝试做到这一点有谷歌解释在这里:https://developers.google.com/android/guides/setup Eclipse与ADT部分 –