无法使用 Google Services Analytics 编译反应原生项目
Posted
技术标签:
【中文标题】无法使用 Google Services Analytics 编译反应原生项目【英文标题】:Can't compile react native project with Google Services Analytics 【发布时间】:2017-11-25 09:21:26 【问题描述】:我一直在使用react native并按照说明添加了adjust sdk,但无法在android端编译项目。 我遇到的问题是关于 build.gradle 中的这一行
compile 'com.google.android.gms:play-services-analytics:10.0.1'
如果该行不在那里,那么它编译没有问题,但是有了它,它就不能处理这个错误(上下文的前几行):
:app:transformClassesWithDexForRelease
Running dex as a separate process.
To run dex in process, the Gradle daemon needs a larger heap.
It currently has 1024 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to at least 1536 MB.
To do this set org.gradle.jvmargs=-Xmx1536M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
AGPBI: "kind":"error","text":"Error converting bytecode to dex:\nCause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/common/api/zzf;","sources":[],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:\ncom.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/common/api/zzf;\n\tat com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:591)\n\tat com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:546)\n\tat com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:528)\n\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)\n\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:188)\n\tat com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)\n\tat com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)\n\tat com.android.dx.command.dexer.Main.run(Main.java:277)\n\tat com.android.dx.command.dexer.Main.main(Main.java:245)\n\tat com.android.dx.command.Main.main(Main.java:106)\n","tool":"Dex"
:app:transformClassesWithDexForRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformClassesWithDexForRelease'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 36.108 secs
核心异常似乎是:
Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/common/api/zzf;
我使用这个库的唯一原因是因为这些说明: https://github.com/adjust/react_native_sdk#google-play-services
任何帮助将不胜感激!
仅供参考:我尝试了很多解决方案,例如在图书馆尝试force=true
,但随机谷歌搜索却没有运气
其余的构建 gradle 和 package.json 用于上下文
build.grade
apply plugin: "com.android.application"
import com.android.build.OutputFile
/**
* The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
* and bundleReleaseJsAndAssets).
* These basically call `react-native bundle` with the correct arguments during the Android build
* cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
* bundle directly from the development server. Below you can see all the possible configurations
* and their defaults. If you decide to add a configuration block, make sure to add it before the
* `apply from: "../../node_modules/react-native/react.gradle"` line.
*
* project.ext.react = [
* // the name of the generated asset file containing your JS bundle
* bundleAssetName: "index.android.bundle",
*
* // the entry file for bundle generation
* entryFile: "index.android.js",
*
* // whether to bundle JS and assets in debug mode
* bundleInDebug: false,
*
* // whether to bundle JS and assets in release mode
* bundleInRelease: true,
*
* // whether to bundle JS and assets in another build variant (if configured).
* // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
* // The configuration property can be in the following formats
* // 'bundleIn$productFlavor$buildType'
* // 'bundleIn$buildType'
* // bundleInFreeDebug: true,
* // bundleInPaidRelease: true,
* // bundleInBeta: true,
*
* // the root of your project, i.e. where "package.json" lives
* root: "../../",
*
* // where to put the JS bundle asset in debug mode
* jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
*
* // where to put the JS bundle asset in release mode
* jsBundleDirRelease: "$buildDir/intermediates/assets/release",
*
* // where to put drawable resources / React Native assets, e.g. the ones you use via
* // require('./image.png')), in debug mode
* resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
*
* // where to put drawable resources / React Native assets, e.g. the ones you use via
* // require('./image.png')), in release mode
* resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
*
* // by default the gradle tasks are skipped if none of the JS files or assets change; this means
* // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
* // date; if you have any other folders that you want to ignore for performance reasons (gradle
* // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
* // for example, you might want to remove it from here.
* inputExcludes: ["android/**", "ios/**"],
*
* // override which node gets called and with what additional arguments
* nodeExecutableAndArgs: ["node"]
*
* // supply additional arguments to the packager
* extraPackagerArgs: []
* ]
*/
apply from: "../../node_modules/react-native/react.gradle"
/**
* Set this to true to create two separate APKs instead of one:
* - An APK that only works on ARM devices
* - An APK that only works on x86 devices
* The advantage is the size of the APK is reduced by about 4MB.
* Upload all the APKs to the Play Store and people will download
* the correct one based on the CPU architecture of their device.
*/
def enableSeparateBuildPerCPUArchitecture = false
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false
android
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig
applicationId "example.app"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
ndk
abiFilters "armeabi-v7a", "x86"
//multiDexEnabled true
//dexOptions
// preDexLibraries = false
//
signingConfigs
release
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
splits
abi
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86"
buildTypes
release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
// applicationVariants are e.g. debug, release
applicationVariants.all variant ->
variant.outputs.each output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "x86":2]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
repositories
mavenCentral()
maven url "https://jitpack.io"
dependencies
compile project(':react-native-adjust')
compile project(':react-native-version-number')
compile project(':react-native-fcm')
compile project(':react-native-fbsdk')
compile project(':react-native-dialogs')
compile project(':react-native-image-picker')
compile project(':react-native-camera')
compile project(':realm')
compile project(':react-native-vector-icons')
compile 'com.google.firebase:firebase-core:10.0.1' //this decides your firebase SDK version
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules
compile 'com.google.android.gms:play-services-analytics:10.0.1'
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy)
from configurations.compile
into 'libs'
apply plugin: 'com.google.gms.google-services'
和 package.json:
"name": "example",
"version": "0.5.0",
"private": true,
"scripts":
"start": "node node_modules/react-native/local-cli/cli.js start",
"android": "react-native run-android",
"rev": "adb reverse tcp:8081 tcp:8081",
"ios": "react-native run-ios --simulator='iPhone 7'",
"release:android": "cd android && ./gradlew assembleRelease && cd ..",
"cdapk": "cd android/app/build/outputs/apk",
"lint": "eslint .",
"test": "jest"
,
"dependencies":
"base-64": "^0.1.0",
"chroma-js": "^1.2.1",
"eslint-plugin-react": "^6.3.0",
"lodash": "^4.16.4",
"moment": "^2.16.0",
"react": "~15.4.0-rc.4",
"react-native": "0.38.0",
"react-native-adjust": "^4.11.3",
"react-native-animatable": "^0.6.1",
"react-native-camera": "^0.4.1",
"react-native-dialogs": "0.0.19",
"react-native-fbsdk": "^0.4.0",
"react-native-fcm": "^2.5.5",
"react-native-image-picker": "^0.23.0",
"react-native-keyboard-aware-scroll-view": "^0.2.8",
"react-native-mimetype": "0.0.7",
"react-native-modal-picker": "0.0.16",
"react-native-navbar": "^1.5.4",
"react-native-popup-menu": "^0.7.5",
"react-native-router-flux": "^3.35.0",
"react-native-scrollable-tab-view": "0.6.0",
"react-native-search-bar": "^2.16.0",
"react-native-swiper": "^1.5.4",
"react-native-tabs": "^1.0.9",
"react-native-vector-icons": "^3.0.0",
"react-native-version-number": "^0.1.2",
"react-redux": "^4.4.5",
"realm": "^0.14.3",
"redux": "^3.6.0",
"redux-logger": "^2.6.1",
"redux-saga": "^0.12.1",
"z-schema": "^3.18.0"
,
"devDependencies":
"babel-jest": "17.0.2",
"babel-preset-react-native": "1.9.0",
"eslint": "^3.6.1",
"eslint-plugin-react": "^6.3.0",
"jest": "17.0.3",
"jest-react-native": "17.0.3",
"react-test-renderer": "~15.4.0-rc.4",
"redux-devtools": "^3.3.1",
"remote-redux-devtools": "^0.5.1"
,
"jest":
"preset": "react-native"
【问题讨论】:
【参考方案1】:Adjust React Native SDK 需要此依赖项才能读取 Google Play 服务广告标识符。也许您的项目包含一些插件,这些插件已经包含了 Google Play 服务库的这一部分,因此您自己添加此依赖项可能会导致对它的多次引用。
您可以尝试跳过添加此依赖项并在沙盒中跟踪会话或事件时检查 Adjust SDK 详细日志,如果您看到名为 gps_adid
的参数与会话/事件包一起发送,则表示您的应用已经包含 Adjust SDK 读取 Google Play 服务广告标识符所需的部分 Google Play 服务库,在这种情况下,您无需添加:
compile 'com.google.android.gms:play-services-analytics:10.0.1'
【讨论】:
以上是关于无法使用 Google Services Analytics 编译反应原生项目的主要内容,如果未能解决你的问题,请参考以下文章
无法编译使用Google Services Analytics对本机项目做出反应
无法获取 google-services-4.3.3 - Firebase 实施
无法解决:com.google.android.support.gms:play-services-map:10.2.0
无法导入 com.google.api.services.bigquery.model.TableCell [重复]
Android Studio 2.3.3:即使使用 Maven 也无法解决:com.google.android.gms:play-services-ads:11.8.0
无法解决:com.google.android.support.gms:play-services-map:10.2.0