2018年3月2日 星期五

Google SST Android SDK 引入錯誤 (Could not resolve project)

嘗試把 Google SST 的官方範例 (https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech/Speech) APP 加入現有專案導致發生錯誤之解決方案。

引導專案


引入 Google SST Speech 這個專案到現有專案時,要做兩件事,才能正確引導。


Gradle Project 修正 Dependencies


匯入專案後,會馬上缺少 Project 中要的 Protobuf ,因此要在 Project 的 build.gradle 增加,以及原本專案使用的 com.novoda:bintray-release:0.4.0 需要升級 0.5.0 (2018年),設定如下:


// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.novoda:bintray-release:0.5.0'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


匯入的專案更改應用程式為類別庫


由於匯入的專案如果是應用程式,可能在引用 gradle 的 compile 的時候會出錯,所以要修正該匯入模組的 build.gradle ,把一開始 apply plugin 的設定值 com.android.application 改為 library ,以及把 applicationId 拿掉,所有設定如下:

/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

apply plugin: 'com.android.library' //把 com.android.application 改為 library
apply plugin: 'com.google.protobuf'

ext {
    supportLibraryVersion = '25.4.0'
    grpcVersion = '1.4.0'
}

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'

    flavorDimensions "dev"

    defaultConfig {
        //applicationId "com.google.cloud.android.speech" 移除 applicationId
        targetSdkVersion 25
        versionCode 1
        versionName '1.0'
    }

    signingConfigs {
        release {
            storeFile file(project.properties.storeFile)
            storePassword project.properties.storePassword
            keyAlias project.properties.keyAlias
            keyPassword project.properties.keyPassword
        }
    }

    productFlavors {
        dev {
            // Minimum version with platform multi-dex support
            minSdkVersion 21
            targetSdkVersion 25
            multiDexEnabled true
        }
        /*prod {
            // Minimum version that can run gRPC (TLS extension)
            minSdkVersion 16
            targetSdkVersion 25
            multiDexEnabled true
        }*/
    }

    buildTypes {
        debug {
            minifyEnabled false
            multiDexEnabled true
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }

    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
        resolutionStrategy.force "com.android.support:support-annotations:$supportLibraryVersion"
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.3.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Support libraries
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"

    // gRPC
    compile "io.grpc:grpc-okhttp:$grpcVersion"
    compile "io.grpc:grpc-protobuf-lite:$grpcVersion"
    compile "io.grpc:grpc-stub:$grpcVersion"
    compile 'javax.annotation:javax.annotation-api:1.2'
    protobuf 'com.google.protobuf:protobuf-java:3.3.1'

    // OAuth2 for Google API
    compile('com.google.auth:google-auth-library-oauth2-http:0.7.0') {
        exclude module: 'httpclient'
    }

    //Tests
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

task copySecretKey(type: Copy) {
    def File secretKey = file "$System.env.GOOGLE_APPLICATION_CREDENTIALS"
    from secretKey.getParent()
    include secretKey.getName()
    into 'src/main/res/raw'
    rename secretKey.getName(), "credential.json"
}
preBuild.dependsOn(copySecretKey)


匯入 gradle.properties


因為匯入後,可能會出現 path 是空的這樣的錯誤,因此需要從原有的專案中,把它的 gradle.properties 檔案的參數複製到現在專案的 gradle.properties 裡面,設定是:

# Sign key settings
storeFile=../../../../../.android/debug.keystore
storePassword=android
keyAlias=androiddebugkey
keyPassword=android


Compile 專案的方式


專案中需要用到 com.google.cloud.speech....之類的 api,所以需要在你使用的應用程式的 Module Gradle 設定檔( build.Gradle: app ),在下面的 dependencies 加入:

dependencies {
...
compile project(path: GoolgeSpeechSDK)
...
}


解決 Could not resolve project 


直接引用 compile project(path: GoolgeSpeechSDK) 可能會導致出錯:


錯誤的意思是你沒有選擇 Gradle 的設定,這時候,可以用讓專案選擇設定檔的方式來完成,有以下的語法可以實現選擇:

implementation project(path: ':GoogleSpeechSDK', configuration: 'default')

或是

annotationProcessor project(':GoogleSpeechSDK')


或是

compile project(path: ':gcpsst', configuration: 'default')

如果最後都沒辦法解決,那請直接到該模組的 build.gradle ,把 productFlavors 設定下其中的 prod 做註解:


Reference:
https://stackoverflow.com/questions/44105127/android-studio-3-0-flavor-dimension-issue
https://github.com/GoogleCloudPlatform/android-docs-samples/issues/54
http://blog.csdn.net/losingcarryjie/article/details/78945597
https://stackoverflow.com/questions/32021074/in-android-studio-how-can-i-change-an-app-project-to-a-library
http://blog.csdn.net/u010665691/article/details/44355323
http://blog.csdn.net/liranke/article/details/72866423
http://blog.csdn.net/kerwinJu/article/details/53053853
https://www.jianshu.com/p/af1eee86f24e
https://stackoverflow.com/questions/34421193/could-not-determine-the-dependencies-of-task-appcrashlyticsstoredeobsdebug-i
https://juejin.im/entry/59f6755a51882567aa5a9faf
https://stackoverflow.com/questions/45679847/android-studio-3-0-compile-issue-cannot-choose-between-configurations
http://www.voidcn.com/article/p-knsmzuea-bqp.html
https://stackoverflow.com/questions/32021074/in-android-studio-how-can-i-change-an-app-project-to-a-library

沒有留言:

張貼留言

© Mac Taylor, 歡迎自由轉貼。
Background Email Pattern by Toby Elliott
Since 2014