在Android Studio中集成Realm时出错

枕上诗书闲处好,门前风景雨来佳。这篇文章主要讲述在Android Studio中集成Realm时出错相关的知识,希望能为你提供帮助。
我正在尝试集成Realm 4.3.1,但我得到了奇怪的错误,我无法解决。我提到了Realm Documentation,我正在使用

android Studio 3.0 Canary 4,
Gradle版本4.1
Android插件版本3.0.0-alpha4
这是一个错误
Error:Cause: java.lang.NullPointerException at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:98) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94) at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255) at java.nio.file.Paths.get(Paths.java:84) at com.android.build.gradle.internal.scope.BuildOutput.getOutputPath(BuildOutput.java:222) at com.android.build.gradle.internal.scope.BuildOutputs.lambda$load$2(BuildOutputs.java:243) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) at com.android.build.gradle.internal.scope.BuildOutputs.load(BuildOutputs.java:245) at com.android.build.gradle.internal.scope.BuildOutputs.load(BuildOutputs.java:184) at com.android.build.gradle.internal.scope.BuildOutputs.load(BuildOutputs.java:140) at com.android.build.gradle.internal.ide.BuildOutputsSupplier.lambda$get$1(BuildOutputsSupplier.java:54) at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:397) at com.android.build.gradle.internal.ide.BuildOutputsSupplier.get(BuildOutputsSupplier.java:49) at com.android.build.gradle.internal.ide.BuildOutputsSupplier.get(BuildOutputsSupplier.java:34) at com.android.build.gradle.internal.ide.AndroidArtifactImpl.getOutputs(AndroidArtifactImpl.java:137) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.tooling.internal.adapter.ProtocolToModelAdapter$ReflectionMethodInvoker.invoke(ProtocolToModelAdapter.java:592) at org.gradle.tooling.internal.adapter.ProtocolToModelAdapter$AdaptingMethodInvoker.invoke(ProtocolToModelAdapter.java:397) at org.gradle.tooling.internal.adapter.ProtocolToModelAdapter$PropertyCachingMethodInvoker.invoke(ProtocolToModelAdapter.java:625) at org.gradle.tooling.internal.adapter.ProtocolToModelAdapter$SafeMethodInvoker.invoke(ProtocolToModelAdapter.java:647) at org.gradle.tooling.internal.adapter.ProtocolToModelAdapter$SupportedPropertyInvoker.invoke(ProtocolToModelAdapter.java:670) at org.gradle.tooling.internal.adapter.ProtocolToModelAdapter$InvocationHandlerImpl.invoke(ProtocolToModelAdapter.java:356) at com.sun.proxy.$Proxy187.getOutputs(Unknown Source) at com.android.tools.idea.gradle.project.model.ide.android.IdeAndroidArtifactImpl.< init> (IdeAndroidArtifactImpl.java:56) at com.android.tools.idea.gradle.project.model.ide.android.IdeVariantImpl.lambda$new$0(IdeVariantImpl.java:59) at com.android.tools.idea.gradle.project.model.ide.android.ModelCache.lambda$computeIfAbsent$0(ModelCache.java:31) at java.util.HashMap.computeIfAbsent(HashMap.java:1126) at com.android.tools.idea.gradle.project.model.ide.android.ModelCache.computeIfAbsent(ModelCache.java:31) at com.android.tools.idea.gradle.project.model.ide.android.IdeVariantImpl.< init> (IdeVariantImpl.java:58) at com.android.tools.idea.gradle.project.model.ide.android.IdeAndroidProjectImpl.lambda$new$4(IdeAndroidProjectImpl.java:87) at com.android.tools.idea.gradle.project.model.ide.android.ModelCache.lambda$computeIfAbsent$0(ModelCache.java:31) at java.util.HashMap.computeIfAbsent(HashMap.java:1126) at com.android.tools.idea.gradle.project.model.ide.android.ModelCache.computeIfAbsent(ModelCache.java:31) at com.android.tools.idea.gradle.project.model.ide.android.IdeModel.copy(IdeModel.java:74) at com.android.tools.idea.gradle.project.model.ide.android.IdeAndroidProjectImpl.< init> (IdeAndroidProjectImpl.java:86) at com.android.tools.idea.gradle.project.model.ide.android.IdeAndroidProjectImpl.< init> (IdeAndroidProjectImpl.java:67) at com.android.tools.idea.gradle.project.model.AndroidModuleModel.< init> (AndroidModuleModel.java:128) at com.android.tools.idea.gradle.project.sync.idea.AndroidGradleProjectResolver.populateModuleContentRoots(AndroidGradleProjectResolver.java:211) at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.doResolveProjectInfo(GradleProjectResolver.java:366) at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.access$200(GradleProjectResolver.java:79) at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver$ProjectConnectionDataNodeFunction.fun(GradleProjectResolver.java:902) at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver$ProjectConnectionDataNodeFunction.fun(GradleProjectResolver.java:886) at org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper.execute(GradleExecutionHelper.java:218) at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.resolveProjectInfo(GradleProjectResolver.java:139) at org.jetbrains.plugins.gradle.service.project.GradleProjectResolver.resolveProjectInfo(GradleProjectResolver.java:79) at com.intellij.openapi.externalSystem.service.remote.RemoteExternalSystemProjectResolverImpl.lambda$resolveProjectInfo$0(RemoteExternalSystemProjectResolverImpl.java:37) at com.intellij.openapi.externalSystem.service.remote.AbstractRemoteExternalSystemService.execute(AbstractRemoteExternalSystemService.java:59) at com.intellij.openapi.externalSystem.service.remote.RemoteExternalSystemProjectResolverImpl.resolveProjectInfo(RemoteExternalSystemProjectResolverImpl.java:37) at com.intellij.openapi.externalSystem.service.remote.wrapper.ExternalSystemProjectResolverWrapper.resolveProjectInfo(ExternalSystemProjectResolverWrapper.java:45) at com.intellij.openapi.externalSystem.service.internal.ExternalSystemResolveProjectTask.doExecute(ExternalSystemResolveProjectTask.java:66) at com.intellij.openapi.externalSystem.service.internal.AbstractExternalSystemTask.execute(AbstractExternalSystemTask.java:139) at com.intellij.openapi.externalSystem.service.internal.AbstractExternalSystemTask.execute(AbstractExternalSystemTask.java:125) at com.intellij.openapi.externalSystem.util.ExternalSystemUtil$3.execute(ExternalSystemUtil.java:388) at com.intellij.openapi.externalSystem.util.ExternalSystemUtil$5.run(ExternalSystemUtil.java:445) at com.intellij.openapi.progress.impl.CoreProgressManager$TaskRunnable.run(CoreProgressManager.java:726) at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:176) at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:556) at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:501) at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:66) at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:163) at com.intellij.openapi.progress.impl.ProgressManagerImpl$1.run(ProgressManagerImpl.java:137) at com.intellij.openapi.application.impl.ApplicationImpl$2.run(ApplicationImpl.java:334) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

【在Android Studio中集成Realm时出错】应用程序gradle
apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.###.###" minSdkVersion 15 targetSdkVersion 25 versionCode 17 versionName "1.17"}buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }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.android.support:appcompat-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.android.support:design:25.3.1' //compile 'com.github.chrisbanes:PhotoView:2.0.0' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.okhttp3:logging-interceptor:3.8.0' compile 'com.squareup.okhttp3:okhttp:3.8.0' compile 'id.zelory:compressor:2.1.0'compile 'com.github.bumptech.glide:glide:3.8.0' compile 'testfairy:testfairy-android-sdk:1.+@aar'compile 'io.realm:realm-android:4.3.1'} apply plugin: 'realm-android'

Project Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha4' classpath 'io.realm:realm-gradle-plugin:4.3.1' } }allprojects { repositories { maven { url 'https://maven.google.com' } maven { url "https://jitpack.io" } jcenter() } }task clean(type: Delete) { delete rootProject.buildDir }

答案从您的应用程序级别compile 'io.realm:realm-android:4.3.1'文件中删除build.gradle并在apply plugin: 'realm-android'下方应用apply plugin: 'com.android.application'。您的应用程序级别build.geadle将如下所示
apply plugin: 'com.android.application' apply plugin: 'realm-android'android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.###.###" minSdkVersion 15 targetSdkVersion 25 versionCode 17 versionName "1.17"}buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }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.android.support:appcompat-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.android.support:design:25.3.1' //compile 'com.github.chrisbanes:PhotoView:2.0.0' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.okhttp3:logging-interceptor:3.8.0' compile 'com.squareup.okhttp3:okhttp:3.8.0' compile 'id.zelory:compressor:2.1.0'compile 'com.github.bumptech.glide:glide:3.8.0' compile 'testfairy:testfairy-android-sdk:1.+@aar' }

另一答案使用领域4.3,您需要在gradle-wrapper.properties中使用gradle 4.1:
distributionUrl = HTTPS://services.gradle.org/distributions/gradle-4.1-all.zip
我必须将我的Android Studio更新到3.0.1才能拥有它。这就是我无法导入领域的原因。 ^^

    推荐阅读