首页 > 解决方案 > 导入为 AAR 文件时,Android 播放服务不起作用

问题描述

我正在尝试在 android studio 中编写一个简单的 GPS 插件
为此我有依赖项,play-services-maps并且play-services-location
首先模块按预期编译
但由于我需要在 Unity 中使用这个插件,我必须以某种方式在我的插件中包含这些 gradle 依赖项。(无论我做什么,我都无法让 Unity 自行导入这些依赖项)
所以我从 maven 下载了这些存储库作为 .aar 文件并添加到 libs 文件夹中。并替换了这些行:

implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'  

用这些:

implementation files('libs/play-services-maps-17.0.0.aar')  
implementation files('libs/play-services-location-17.0.0.aar')  

现在,当我尝试制作我的模块时,我得到了错误:

locationRequest = LocationRequest.create();
                                 ^
  class file for com.google.android.gms.common.internal.ReflectedParcelable not found  

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gpsmodule">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>  

这是我的课:

package com.example.gpsmodule;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Location;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;

public class GPSWrapper {
    private FusedLocationProviderClient fusedLocationProviderClient;
    private LocationRequest locationRequest;
    private LocationCallback locationCallback;
    private Activity activity;

    public GPSWrapper(Activity ac) {
        activity = ac;
        getGPS();
    }

    public void getGPS() {

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(activity);

        locationRequest = LocationRequest.create();
        locationRequest.setInterval(0);
        locationRequest.setFastestInterval(0);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                Location location = locationResult.getLastLocation();
                Log.d("GPSWrapper", "fresh location received");
            }
        };

        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
    }
}

这是我的毕业生:

plugins {
    id 'com.android.library'
}

android {
    compileSdkVersion 30

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    //implementation 'com.google.android.gms:play-services-maps:17.0.0' // this actually work
    //implementation 'com.google.android.gms:play-services-location:17.0.0'//this actually work
    implementation files('libs/play-services-maps-17.0.0.aar') // this line break the Make
    implementation files('libs/play-services-location-17.0.0.aar')// this line break the Make
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'


    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

知道问题是什么吗?

标签: android-studiounity3dgradlegoogle-play-servicesandroid-gps

解决方案


所以答案来自一个意想不到的方向,
显然我不需要在插件 JAR 文件中包含我的插件依赖的库,
而不是在插件本身中打包库 - 我可以简单地gradleTemplate.properties在 UNITY 中编辑文件。
这样 UNITY 将在编译时下载并打包它们。
为此,请转到文件 -> 构建设置 -> 播放器设置 -> 发布设置
检查“自定义主 Gradle 模板”以启用在 UNITY 中编辑 gradle 文件。
然后,在 UNITY 编辑器项目选项卡中,转到 Assets->Plugins->Android
右键单击gradleTemplate.properties​​并选择show in explorer
现在您可以使用任何文本编辑器打开文件,然后像在 Android Studio 中一样添加依赖项


推荐阅读