我正在关注这篇文章:
问题:
1.该代码无法在release
apk上运行,并会因以下异常而崩溃。翻译按钮仅适用于调试应用。客户端库中也提到“ Cloud java client libraries do not currently support Android
”
implementation('com.google.cloud:google-cloud-translate:1.12.0') {
exclude group: 'org.apache.httpcomponents'
exclude group: 'com.google.auto.value'
exclude group: 'org.json', module: 'json'
}
annotationProcessor 'com.google.cloud:google-cloud-translate:1.12.0'
例外:
致命异常:com.google.cloud.translate.TranslateException:获取服务帐户的访问令牌时出错: 在com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate + 61(HttpTranslateRpc.java:61) 在com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate + 144(HttpTranslateRpc.java:144) 在com.google.cloud.translate.TranslateImpl $ 4.call + 113(TranslateImpl.java:113) 在com.google.cloud.translate.TranslateImpl $ 4.call + 110(TranslateImpl.java:110) com.google.api.gax.retrying.DirectRetryingExecutor.submit + 89(DirectRetryingExecutor.java:89) 在com.google.cloud.RetryHelper.run + 74(RetryHelper.java:74) 在com.google.cloud.RetryHelper.runWithRetries + 51(RetryHelper.java:51) 在com.google.cloud.translate.TranslateImpl.translate + 110(TranslateImpl.java:110) 在com.google.cloud.translate.TranslateImpl.translate + 124(TranslateImpl.java:124) 在company.Test.TranslationDemoActivity.translate + 51(TranslationDemoActivity.kt:51) 在company.Test.TranslationDemoActivity.access $ translate + 14(TranslationDemoActivity.kt:14) 在company.Test.TranslationDemoActivity $ onCreate $ 1.onClick + 28(TranslationDemoActivity.kt:28) 在android.view.View.performClick + 6663(View.java:6663) 在android.view.View.performClickInternal + 6635(View.java:6635) 在android.view.View.access $ 3100 + 794(View.java:794) 在android.view.View $ PerformClick.run + 26199(View.java:26199) 在android.os.Handler.handleCallback + 907(Handler.java:907) 在android.os.Handler.dispatchMessage + 105(Handler.java:105) 在android.os.Looper.loop + 216(Looper.java:216) 在android.app.ActivityThread.main + 7625(ActivityThread.java:7625) 在java.lang.reflect.Method.invoke(Method.java) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run + 524(RuntimeInit.java:524) 在com.android.internal.os.ZygoteInit.main + 987(ZygoteInit.java:987)
问题2:集成Google翻译SDK的官方方法是什么?本教程建议将credentials.json
放在raw
文件夹中,这在您反编译apk时可见!是否建议采用这种方式进行集成?
来源:
private var originalText: String? = null
var translate: Translate? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_translation_demo)
translateButton.setOnClickListener {
if (HelperMethods.isConnectedToInternet(this)) {
getTranslateService()
translate()
}
}
}
private fun getTranslateService() {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
try {
resources.openRawResource(R.raw.cred).use { `is` ->
val myCredentials = GoogleCredentials.fromStream(`is`)
val translateOptions = TranslateOptions.newBuilder().setCredentials(myCredentials).build()
translate = translateOptions.service
}
} catch (ioe: IOException) {
}
}
private fun translate() {
originalText = inputToTranslate!!.text.toString()
val translation = translate!!.translate(
originalText,
Translate.TranslateOption.targetLanguage("hi")
)
translatedTv!!.text = translation.translatedText
}