【问题标题】:Android need to import aar play services 8.4.0 in to eclipseAndroid需要将aar play services 8.4.0导入到eclipse中
【发布时间】:2016-07-27 09:57:59
【问题描述】:

我已使用以下代码打开像 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;

我的代码:

@覆盖 公共无效 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 格式的库(工作室可接受)。

请帮忙解决。

【问题讨论】:

    标签: android android-studio google-play-services arr google-location-services


    【解决方案1】:

    GRADLE 构建文件

    将以下内容添加到您的 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 目录下,不要导入任何“build”文件夹,它们是由 Gradle 生成的

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

    目标=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

    【讨论】:

    • 我已经完成了将 aar 转换为 android 库项目以执行以下步骤并将库项目添加到我的主要源代码中。但是,它的显示已成功添加。但是,谷歌播放服务 API 没有导入。还是一样的错误。
    • 如果可能的话,您能否将最新的 arr play services8.4.0 转换为库项目,并导入 eclipse 格式。或者,如果您想要我的源代码与库方式,我将上传并提供链接给您。如果可能,请检查并回复我,我做错了什么。
    • 试试看这里有 google 解释:developers.google.com/android/guides/setup Eclipse with ADT section
    • 谢谢您的回复。这就是我的问题:ECLIPSE WITH ADT here /extras/google/google_play_services/libproject/google-play-services_lib/.... 在 SDK 中更新 Google Play 服务(Rev​​ 31)后,现有的 google play 服务在提到的地方自动删除..如果那个地方,可用的 playservices 库项目意味着,我将直接使用。那是我的问题 playservices 位置链接:postimg.org/image/3magqljyf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多