无法编译使用Google Services Analytics对本机项目做出反应

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法编译使用Google Services Analytics对本机项目做出反应相关的知识,希望对你有一定的参考价值。

我一直在使用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:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/common/api/zzf;","sources":[{}],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/common/api/zzf;
	at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:591)
	at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:546)
	at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:528)
	at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
	at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
	at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
	at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
	at com.android.dx.command.dexer.Main.run(Main.java:277)
	at com.android.dx.command.dexer.Main.main(Main.java:245)
	at com.android.dx.command.Main.main(Main.java:106)
","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"
  }
}
答案

调整React Native SDK需要此依赖项才能读取Google Play服务广告标识符。也许您的项目包含一些已包含此部分Google Play服务库的插件,因此添加此依赖项可能会导致多次引用。

您可以尝试跳过添加此依赖项并在沙箱中跟踪会话或事件时检查调整SDK详细日志,如果您看到名为gps_adid的参数与会话/事件包一起发送,则表示您的应用已包含Google Play的一部分调整SDK需要的服务库,以便阅读Google Play服务广告标识符,在这种情况下,您无需添加:

compile 'com.google.android.gms:play-services-analytics:10.0.1'

以上是关于无法编译使用Google Services Analytics对本机项目做出反应的主要内容,如果未能解决你的问题,请参考以下文章

文件 google-services.json 丢失。没有它,Google 服务插件无法运行 ionic 3

升级到 'com.google.android.gms:play-services-ads:18.1.0' 后的 Android 编译错误

无法获取 google-services-4.3.3 - Firebase 实施

无法解决:com.google.android.support.gms:play-services-map:10.2.0

添加编译'com.google.android.gms:play-services-ads:11.6.0'ERROR

无法导入 com.google.api.services.bigquery.model.TableCell [重复]