首先Android Studio版本:3.2.0
1、註冊百度賬號並企業認證
2、創建應用生成API Key和Secret Key
3、下載對應的SDK(下載SDK的時候需要新建授權)
因爲下載的時候需要選擇授權文件(授權文件包含包名和簽名文件的MD5)
如下即可下載SDK
我們以基礎版和人臉通行示例工程爲例
下載下來並解壓然後導入工程:
剛導入會彈出一個提示:
我這邊選擇update,然後gradle-wrapper.properties文件就變成了4.6的版本:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
並報錯了:
Could not find com.android.tools.build:aapt2:3.2.0-4818971.
Searched in the following locations:
file:/F:/Android/sdk/extras/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
file:/F:/Android/sdk/extras/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
file:/F:/Android/sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
file:/F:/Android/sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
file:/F:/Android/sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
file:/F:/Android/sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
https://jcenter.bintray.com/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
https://jcenter.bintray.com/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
Required by:
project :facesdk
因爲升級了版本,新版本需要在最上級的build.gralde增加谷歌庫 解決問題
(最簡單的方法,重新創建一個項目,copy過來最靠譜)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
再次build發現出現了這些錯誤:
這些錯誤可以忽略,不影響我們運行,下面我們開始運行項目,又報錯了:
什麼?R不存在?什麼鬼
再認真看看我們的包名和build.gradle中的applicationId發現
applicationId "com.xx.facedemo"
//我的manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xx.facedemo">
再對比下工程文件中導入的包:
怎麼回事?兩邊竟然不一樣。
我們就找到問題了,R文件需要重新導入纔可以。
那我們每個java文件依次重新導入一下R後是這樣的:
import com.xx.facedemo.R;
所有的java類都重新導入後,沒問題了,不報錯了。
溫馨提示(一)修改Config.java中key
這個類中需要填寫百度申請的key:
/*
* Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
*/
package com.baidu.aip.fl;
public class Config {
// 爲了apiKey,secretKey爲您調用百度人臉在線接口的,如註冊,識別等。
// 爲了的安全,建議放在您的服務端,端把人臉傳給服務器,在服務端端進行人臉註冊、識別放在示例裏面是爲了您快速看到效果
public static String apiKey = 替換爲你的apiKey(ak);
public static String secretKey = 替換爲你的secretKey(sk);
public static String licenseID = "facedemos-face-android";
public static String licenseFileName = "替換爲你的licenseFileName";
/**
* groupId,標識一組用戶(由數字、字母、下劃線組成),長度限制128B,可以自行定義,只要註冊和識別都是同一個組。
* 詳情見 http://ai.baidu.com/docs#/Face-API/top
* <p>
* 人臉識別 接口 https://aip.baidubce.com/rest/2.0/face/v2/identify
* 人臉註冊 接口 https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/add
*/
public static String groupID = 替換爲groupID;
}
溫馨提示(二)修改build.gradle中的簽名參數
/*
* Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
*/
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.zhiao.facedemo"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
signingConfigs {
def alias = "您的keyAlias"
def password = "您的keyPassword"
def filePath = "您的storeFile" //簽名文件路徑 如:../faceprint.jks
debug {
keyAlias alias
keyPassword password
storeFile file(filePath)
storePassword(password)
}
release {
keyAlias alias
keyPassword password
storeFile file(filePath)
storePassword(password)
}
}
}
repositories {
flatDir {
dirs 'libs', project(':facesdk').file('libs')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(":facesdk")
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'cat.ereza:customactivityoncrash:1.5.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
}
都配置好了以後就會看到這個提示了,哈哈。