【问题标题】:Getting errors while using Google Cloud Translation Client Libraries for Java (Android)使用 Google Cloud Translation Client Libraries for Java (Android) 时出错
【发布时间】:2023-04-07 00:39:01
【问题描述】:

我正在尝试使用 Google 的 Cloud Translation API 制作一个谷歌翻译应用程序。当我尝试将 compile 'com.google.cloud:google-cloud-translate:1.14.0' 错误编译为 APK project.properties 中复制的重复文件

我的android代码如下(java代码没有问题)

import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;

public class QuickstartSample {
  public static void main(String... args) throws Exception {

    Translate translate = TranslateOptions.getDefaultInstance().getService();


    String text = "Hello, world!";


    Translation translation =
        translate.translate(
            text,
            TranslateOption.sourceLanguage("en"),
            TranslateOption.targetLanguage("ru"));


    System.out.printf("Text: %s%n", text);
    System.out.printf("Translation: %s%n", translation.getTranslatedText());
  }
}

我的 build.gradle 如下(由于下面给出的 compile 语句而出现问题)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.googletranslation"
        minSdkVersion 22
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    **compile 'com.google.cloud:google-cloud-translate:1.14.0'**
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
}

我得到如下错误:

  • 错误:与项目 ':app' 中的依赖项 'com.google.code.findbugs:jsr305' 冲突。应用程序 (3.0.0) 和测试应用程序 (2.0.1) 的已解决版本不同。详情请见http://g.co/androidstudio/app-test-app-conflict

  • 警告:警告:依赖 org.apache.httpcomponents:httpclient:4.0.1 在发布时被忽略,因为它可能与 Android 提供的内部版本冲突。 如有问题,请用jarjar重新打包以更改类包

  • 警告:警告:依赖 org.json:json:20160810 在发布时会被忽略,因为它可能与 Android 提供的内部版本冲突。 如有问题,请用jarjar重新打包以更改类包

  • 我已经尝试了以下链接中的所有方法,但没有效果。错误仍在增加

  • Error:Conflict with dependency 'com.google.code.findbugs:jsr305'

【问题讨论】:

  • 帮助我使用 Google Translation API 制作翻译应用

标签: gradle build.gradle google-translate


【解决方案1】:

必须包括这些:

android {
packagingOptions {
    exclude 'project.properties'
    exclude  'META-INF/INDEX.LIST'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/notice'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license'
    exclude 'META-INF/license.txt'
}


    defaultConfig {
    javaCompileOptions {
        annotationProcessorOptions {
            includeCompileClasspath false
        }
    }}

}

【讨论】: