0

我已经使用以下代码来打开像 OLA 应用程序这样的 gps。下面的代码在带有播放服务 8.4.0 的 Android Studio 中成功运行。但是,我想在 Eclipse 中使用 ADT 23 运行它。

我在 sdk 中搜索了最新的 google playservices。它不可用。然后,最后,我找到了最新的 aar8.4.0 文件。在我将 AAR 转换为普通的 Eclipse 库项目并导入到 Eclipse 并刷新并作为库添加到我的主项目中之后。

仍然收到此错误。主要源代码活动不支持以下 API。

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

我的代码:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 设置内容视图(R.layout.main);设置请求();} //------------------------------------------------ -------------------------------------

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 中完美运行。并看到输出,例如直接使用对话框是/否本身打开 gps,而无需转到设置页面。

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

请帮助获得解决方案。

4

1 回答 1

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 构建

运行 gradle clean build (或在 Eclipse 中右键单击 build.gradle Rus As -> Gradle build )

您应该在 libs 目录中找到所有依赖项和分解的 AAR。这就是 Eclipse 所需要的。

在 ECLIPSE 中导入

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

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

  1. 在项目的 libs 目录下导入这些文件夹,不要导入任何“构建”文件夹,它们是由 Gradle 生成的

  2. 确保对您添加的所有 AAR 项目执行项目 -> 清理。在您的工作区中,检查每个 AAR 展开的项目是否在 project.properties 中具有以下内容:

target=android- android.library=true 3. 现在在您的主 Android 项目中,您可以使用 ADT 添加库引用,或者您可以编辑 project.properties 文件并添加

android.libraries.reference.1=libs/someExplodedAAR/
  1. 现在您可以右键单击您的主要 Android 项目并运行为 -> Android 应用程序。

信息来自:

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

于 2016-07-27T10:04:28.470 回答