转载

Mac下Android Studio配置高德地图API过程记录与总结

集成高德地图SDK的过程总结以及发现的问题。

欢迎指正。

获取API KEY

获取SHA1

参考链接(高德开放平台): 获取Key

需要用到的命令

keytool -v -list -keystore文件路径,此处的keystore文件的路径

终端输入命令

笔者认为此方法最为简便,其他方法可参见上述官方链接。

打开系统-其他工具-终端,输入:

android```,再次输入:```keytool -v -list -keystor```
**密钥库口令:android**,并不是开机密码。

若密钥库错误,提示:

keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect

java.io.IOException: Keystore was tampered with, or password was incorrect

at java.base/sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:780)

at java.base/sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:222)

at java.base/java.security.KeyStore.load(KeyStore.java:1479)

at java.base/sun.security.tools.keytool.Main.doCommands(Main.java:1048)

at java.base/sun.security.tools.keytool.Main.run(Main.java:397)

at java.base/sun.security.tools.keytool.Main.main(Main.java:390)

Caused by: java.security.UnrecoverableKeyException: Password verification failed

at java.base/sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:778)

… 5 more

成功后如下图所示:



### Android Studio输入命令方法可能出现的问题

笔者此处没有使用这个方法,即遇到之后的方案:

这里尝试使用在Android Studio的终端中输入,终端打开路径:View-Tool Windows-Terminal

提示错误,此处当时未截图,意为找不到JDK。

原来是笔者此处未安装SDK,需要的下载链接(Oracle技术网):[Java SE Development Kit 10下载](https://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html)

keystore文件的路径

参考链接(CSDN):[获取高德地图api key](https://blog.csdn.net/m00123456789/article/details/60142613):

> 提示密钥库文件不存在。解决办法如下: 
>
>  进入.android/下。  c:  cd /Users/admin/ .android/  输入命令:
>
> keytool -genkey -v -keystore debug.keystore -alias androiddebugkey -keyalg RSA -validity 10000

此处未尝试。

### 获取PackageName

在Android Studio中获取:

> 获取build.gradle文件中的ApplicationId作为PackageName;
>
> 如果没有设置ApplicationId,请以AndroidManifest.xml配置文件的package 属性为准。

build.gradle路径:

包名/app/build.gradle

android {

compileSdkVersion 28

defaultConfig {

applicationId “product.你的包名.com.你的工程名称” //此行

minSdkVersion 2X

testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”

}

AndroidManifest.xml路径:

包名/app/src/main/AndroidManifest.xml

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="product.你的包名.com.你的工程名称">   //此行

下载与安装

下载链接(高德开放平台): Android 地图SDK 相关下载

笔者下载的是Android地图SDK 一键下载

配置:拷贝添加SDK

参考链接(高德开放平台):

由于高德地图地图SDK(入门指南第四步)处没有和定位SDK(入门指南第四步)一样添加链接:

此处直接放上地图配置SDK的配置拷贝文件集成SDK的链接:

Android Studio 配置工程

  1. 拷贝jar到libs:

将下载的文件解压,获得jar,笔者项目需要两类地图,故如下图所示:

复制路径:包名/app/libs

Mac下Android Studio配置高德地图API过程记录与总结

  1. 添加 so 库

笔者使用的是新建文件的方式,其他方法可参见上述官方链接。

方法一:

使用默认配置,不需要修改build.gradle。在 main 目录下创建文件夹 jniLibs (如果有就不需要创建了),将下载文件的 armeabi 文件夹复制到这个目录下,如果已经有这个目录,将下载的 so 库复制到这个目录即可。

将AMap3DMap.zip中的文件夹文件拷贝到新建的jnilibs文件夹中,完成如下图所示:

Mac下Android Studio配置高德地图API过程记录与总结

此处完成之后,继续配置build.gradle文件。

配置:通过Gradle集成SDK

  1. Project的build.gradle文件中配置repositories,添加maven或jcenter仓库地址

加入代码:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }//上文

    allprojects {
        repositories {
            jcenter() // 或者 mavenCentral()
        }
    }//下文
    android {
        defaultConfig {
  1. 主工程的build.gradle文件配置dependencies:添加依赖

根据项目需求添加SDK依赖。引入各个SDK功能最新版本, dependencies 配置方式如下:

表格

allprojects {
        repositories {
            jcenter() // 或者 mavenCentral(),此为上文中配置repositories
        }
    }
    android {
        defaultConfig {
            ndk {
                //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)
                abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","arm64-v8a","x86_64"
            }
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //以下为添加项目依赖代码
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.amap.api:map2d:latest.integration'    //2D地图
    //compile 'com.amap.api:3dmap:latest.integration'    //3d地图
    compile 'com.amap.api:navi-3dmap:latest.integration'//导航
    compile 'com.amap.api:search:latest.integration'   //搜索
    compile 'com.amap.api:location:latest.integration' //定位
}

此处我觉得常见问题应该放在表格的下面,最应当加粗的两条:

1、3D地图 SDK 和导航 SDK,5.0.0 版本以后全面支持多平台 so 库(armeabi、armeabi-v7a、arm64-v8a、x86、x86_64),开发者可以根据需要选择。同时还需要注意的是:如果您涉及到新旧版本更替请移除旧版本的 so 库之后替换新版本 so 库到工程中。

2、navi导航SDK 5.0.0以后版本包含了3D地图SDK,所以请不要同时引入 map3d 和 navi SDK。

3、如果build失败提示com.amap.api:XXX:X.X.X 找不到,请确认拼写及版本号是否正确,如果访问不到jcenter可以切换为maven仓库尝试一下。

4、依照上述方法引入 SDK 以后,不需要在libs文件夹下导入对应SDK的 so 和 jar 包,会有冲突。

Mac下Android Studio配置高德地图API过程记录与总结

报错信息:Cause: archive is not a ZIP archive

可能是jar文件出现了问题。

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:compileDebugJavaWithJavac'.
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
	at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
	at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
	at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
	at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
	at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
	at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
	at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
	at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
	at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
	at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
	at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
	at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
	at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
	at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
	at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
	at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
	at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
	at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
	at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
	at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
	at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.gradle.api.GradleException: Could not expand ZIP '/Users/felixxiong/AndroidStudioProjects/mypackname/app/libs/AMap_Search_V6.1.0_20180330.jar'.
	at org.gradle.api.internal.file.archive.ZipFileTree.visit(ZipFileTree.java:104)
	at org.gradle.api.internal.file.collections.FileTreeAdapter.visit(FileTreeAdapter.java:110)
	at org.gradle.api.internal.tasks.compile.incremental.jar.DefaultJarSnapshotter.createSnapshot(DefaultJarSnapshotter.java:45)
	at org.gradle.api.internal.tasks.compile.incremental.jar.CachingJarSnapshotter$1.create(CachingJarSnapshotter.java:42)
	at org.gradle.api.internal.tasks.compile.incremental.jar.CachingJarSnapshotter$1.create(CachingJarSnapshotter.java:40)
	at org.gradle.api.internal.tasks.compile.incremental.jar.DefaultJarSnapshotCache$1.create(DefaultJarSnapshotCache.java:53)
	at org.gradle.api.internal.tasks.compile.incremental.jar.DefaultJarSnapshotCache$1.create(DefaultJarSnapshotCache.java:51)
	at org.gradle.cache.internal.MinimalPersistentCache.get(MinimalPersistentCache.java:36)
	at org.gradle.api.internal.tasks.compile.incremental.jar.DefaultJarSnapshotCache.get(DefaultJarSnapshotCache.java:51)
	at org.gradle.api.internal.tasks.compile.incremental.jar.DefaultJarSnapshotCache.get(DefaultJarSnapshotCache.java:28)
	at org.gradle.api.internal.tasks.compile.incremental.jar.CachingJarSnapshotter.createSnapshot(CachingJarSnapshotter.java:40)
	at org.gradle.api.internal.tasks.compile.incremental.jar.JarClasspathSnapshotFactory.createSnapshot(JarClasspathSnapshotFactory.java:43)
	at org.gradle.api.internal.tasks.compile.incremental.jar.JarClasspathSnapshotMaker.maybeInitialize(JarClasspathSnapshotMaker.java:63)
	at org.gradle.api.internal.tasks.compile.incremental.jar.JarClasspathSnapshotMaker.getJarClasspathSnapshot(JarClasspathSnapshotMaker.java:52)
	at org.gradle.api.internal.tasks.compile.incremental.SelectiveCompiler.execute(SelectiveCompiler.java:56)
	at org.gradle.api.internal.tasks.compile.incremental.SelectiveCompiler.execute(SelectiveCompiler.java:34)
	at org.gradle.api.internal.tasks.compile.incremental.IncrementalCompilationFinalizer.execute(IncrementalCompilationFinalizer.java:39)
	at org.gradle.api.internal.tasks.compile.incremental.IncrementalCompilationFinalizer.execute(IncrementalCompilationFinalizer.java:24)
	at org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:207)
	at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:133)
	at com.android.build.gradle.tasks.factory.AndroidJavaCompile.compile(AndroidJavaCompile.java:125)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
	at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)
	at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
	at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
	at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
	... 32 more
Caused by: java.util.zip.ZipException: archive is not a ZIP archive
	at org.apache.tools.zip.ZipFile.positionAtEndOfCentralDirectoryRecord(ZipFile.java:774)
	at org.apache.tools.zip.ZipFile.positionAtCentralDirectory(ZipFile.java:710)
	at org.apache.tools.zip.ZipFile.populateFromCentralDirectory(ZipFile.java:455)
	at org.apache.tools.zip.ZipFile.<init>(ZipFile.java:215)
	at org.apache.tools.zip.ZipFile.<init>(ZipFile.java:191)
	at org.apache.tools.zip.ZipFile.<init>(ZipFile.java:151)
	at org.gradle.api.internal.file.archive.ZipFileTree.visit(ZipFileTree.java:80)
	... 67 more

其中第二条加粗,即使删除了这一行依然报错。

第四条,查看示例代码之后,

确实也 在libs包下引入jar文件以及不同的SO库架构,而不是在教程里的jniLibs文件夹下 ,是教程错了吗?

下载的实例代码的文件比对

文件结构:

Mac下Android Studio配置高德地图API过程记录与总结

和自己添加的值得注意的地方:

在 Android 地图SDK 相关下载 里面 并没有location、volley 的jar包,和这个是否有关系还需要测试。

build.gradie:

因为发布在是自己的博客,就用代码的形式。

buildscript {   //直接配置res放在开头了
    repositories {
        jcenter()
    }
}
apply plugin: 'com.android.application'

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    compile 'com.android.support:recyclerview-v7:26.0.0'
    compile 'com.android.support:support-v4:26.0.0'
}

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']  
            //使用了第二种方法:使用自定义配置,将下载文件的 armeabi 文件夹复制到 libs 目录,如果有这个目录,请将下载的 so 库复制到这个目录,然后打开build.gradle,找到 sourceSets 标签,在里面增加一项配置
        }


        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

这个问题需要有待解决。

第一时间获得动态,官方开发者社区帖子链接:

http://lbsbbs.amap.com/forum.php?mod=viewthread&tid=45652&page=1&extra=#pid189864

原文  https://felixxiong.github.io/2018/09/01/Mac下Android-Studio配置高德地图API过程记录与总结/
正文到此结束
Loading...