Unity调用安卓相册选择图片,采用gradle打包,踩坑记录

疫情离职在家没事自己写了个应用,需要用到安卓的打开相册选择图片功能,折腾了3天终于搞定了,以下是记录期间遇到的坑。
参考链接:https://www.cnblogs.com/shawnlen/p/11812954.html
https://www.jianshu.com/p/86b275da600e
最后完成功能,主要是参考了以上两个大佬的方案。
我先按着第二个链接操作的,打出包后,Unity和安卓的交互完全没问题,信息都可以显示。

/*
          * Toast显示unity发送过来的内容
          * @param content           消息的内容
          * @return                  调用是否成功
          */
         public boolean showToast(String content){
             Toast.makeText(getActivity(),content,Toast.LENGTH_SHORT).show();
             //这里是主动调用Unity中的方法,该方法之后unity部分会讲到
             callUnity("Main Camera","FromAndroid", "hello unity i'm android");
             return true;
         }

showToast是接受到Unity的消息后,调用安卓原生方法,但是我不知道安卓原生打开相册,选择图片的方法是啥,也没找到所以放弃了这种方法,不过这个帖子的打包流程对我之后出jar包提供了帮助,如果有哪个大佬知道 Toast.makeText(getActivity(),content,Toast.LENGTH_SHORT).show();这块对应打开相册的方法,希望评论告知一下,实在没查到(也有原因是这功能弄的比较烦躁,没太仔细查,没事我在查查去)。
然后是第一个链接的方法,原作者已经写的很详细了,一步一步跟着来就行了,不过多赘述了,我主要说下打包和打包后闪退或者选择不了图片的问题(处理期间都想砸电脑了,还好最后解决了)。

task makejar(type: Copy){

    delete 'libs/test.jar'

    from('build/intermediates/packaged-classes/release/')

    into('libs')

    include('classes.jar')

    rename('classes.jar','FarmGetAndroidPhoto.jar')
}

makejar.dependsOn(build)

在Terminal里面输入命令 gradlew makejar 等一会就会提示build successful 这就是包打好了。

我这里是没有成功出来的。
首先提一下目录的问题,可能是Android Studio版本的问题,用上边from(‘build/intermediates/packaged-classes/release/’)目录我是出不来包的,自己查找测试了一下,我工程里是用from(‘build/intermediates/aar_libs_directory/release/libs’)这个目录才出来的包,上边代码片的意思其实很简单,就是把之前的jar包删除,然后把from目录下的classes.jar文件复制到into目录,再重命名为rename的名字。所以最后其实出来的jar包都是叫classes.jar,我一开始发现没有,就打包完成后在整个工程搜索classes.jar看生成时间没错,就按着这个目录修改了,不确定之后Android Studio升级会不会再变,就把解决过程写出来了。
代码片在这引用了,我就先把后边的问题放到前边说了。

跳过目录问题,正常进行流程会到这一步,看到提示以下信息:
Task :PhotoModule:compileDebugUnitTestJavaWithJavac FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:PhotoModule:compileDebugUnitTestJavaWithJavac’.
    Could not find tools.jar. Please check that D:\AndroidConfig\jre contains a valid JDK installation.

解决这个问题,把jdk目录下bin文件夹下的tools.jar拷贝到jre对应目录就可以了Unity调用安卓相册选择图片,采用gradle打包,踩坑记录_第1张图片
Unity调用安卓相册选择图片,采用gradle打包,踩坑记录_第2张图片我也不知道为啥会提示这个,jre都是按照jdk的时候按照提示正常安装的,而且我还发现jdk安装完成后,目录里还会有个jre文件夹,我把这个删除了,没看到啥影响。应该是安卓jdk的时候默认在jdk目录安装了jre,不懂有啥意义,知道的大佬可以帮忙解释下,我对安卓的这些环境还不是很了解。

上边这个问题解决了,在运行gradlew makejar会报以下错误

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:PhotoModule:testDebugUnitTest’.
    Process ‘Gradle Test Executor 2’ finished with non-zero exit value 1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org
    查了半天,有给解决方案的,试了没效果,这log信息也没看出啥,感觉Android Studio的log就会告诉我一个消息“请查看日志信息”,别的啥帮助也没有。
    然后我自己瞎研究,看log,PhotoModule:testDebugUnitTest’,冒号前边是我的工程名字,后边看着是个单元测试,但我怎么也不懂,打个jar包要啥单元测试,于是我就试着看能不能找到执行这个单元测试的代码。果然在工程目录/java/包名下,找到了ExampleUnitTest这个类。

public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() {
        assertEquals(4, 2 + 2);
    }
}

看到有这么段代码,也不懂啥意思,在我看来就是意义不明,反正我是急于出包,抱着试试的态度就把ExampleUnitTest方法里的代码注释了,再打包就可以了。如果有谁知道这是干啥的,希望帮忙解释下,我也是没办法了,本身不喜欢这种看不懂就注释的操作。
到这里总算是出jar包了,高兴的不行,可累死老子了,结果后边才是恶梦。

出来包放到Unity里,进行打包,会报以下错误。
查了半天也没查出个所以然,我就总觉得是Gradle版本没对应上,就各种查,改配置文件,sdk升级,jdk重装,折腾了半天。最后发现第一个链接的作者有说这个问题把jar包里的buildconfig.class,就可以了,但是解压后没有发现这个东西,不仅如此连在Android Studio里我自己创建的类都没有找到,我觉得再怎么打包总不可能把类名也改了把,毕竟Unity和Android交互再我看来还是用的字符串对应名字调用的类和方法。

CommandInvokationFailure: Gradle build failed. 
D:/AndroidConfig/jdk\bin\java.exe -classpath "D:\Unity2018.4.6\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-5.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

stderr[

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':checkReleaseDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class bitter.jnibridge.JNIBridge found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class bitter.jnibridge.JNIBridge$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.Camera2Wrapper found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleARCoreApi found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrApi found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrProxy found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrProxy$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrProxy$2 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrProxy$3 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrProxy$4 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrProxy$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrVideo found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.GoogleVrVideo$GoogleVrVideoCallbacks found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.NativeLoader found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.ReflectionHelper found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.ReflectionHelper$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.ReflectionHelper$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$10 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$11 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$12 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$13 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$14 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$15 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$16 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$17 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$18 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$19 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$2 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$20 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$21 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$22 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$23 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$3 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$4 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$5 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$6 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$7 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$8 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$9 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$b found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$c found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$d found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$e found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$e$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$e$2 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayer$f found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityPlayerActivity found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityWebRequest found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityWebRequest$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.UnityWebRequest$2 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$2 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$3 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$4 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$5 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.a$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.b found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.b$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.b$b found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.c found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.c$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.d found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.e found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.f found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.g found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.h found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.i found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.j found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.k found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.k$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.k$2 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.k$3 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.l found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.l$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.l$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.m found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.n found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.o found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.o$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.p found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.p$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.p$b found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$1$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$1$1$1 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$2 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$3 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$4 found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class com.unity3d.player.q$a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class org.fmod.FMODAudioDevice found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  Duplicate class org.fmod.a found in modules FarmGetAndroidPhoto.jar (FarmGetAndroidPhoto.jar) and unity-classes.jar (unity-classes.jar)
  
  Go to the documentation to learn how to Fix dependency resolution errors.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
]
stdout[

> Configure project :
Observed package id 'build-tools;30.0.0-rc2' in inconsistent location 'D:\AndroidConfig\android-sdk_r24.4.1-windows\android-sdk-windows\build-tools\30.0.0-preview' (Expected 'D:\AndroidConfig\android-sdk_r24.4.1-windows\android-sdk-windows\build-tools\30.0.0-rc2')
Observed package id 'tools' in inconsistent location 'D:\AndroidConfig\android-sdk_r24.4.1-windows\android-sdk-windows\tempToolsDir' (Expected 'D:\AndroidConfig\android-sdk_r24.4.1-windows\android-sdk-w

这个错误卡了我好久,属于彻底蒙圈无从下手的状态。然后突然灵光一闪,想到按第二个链接我是可以打出包的,而且交互是通的,虽然一个是jar包一个是aar包,但是unity调用安卓方法的函数是一样,于是就抱着试试态度,用Android Studio中的Build/Make Module‘工程名’,选项试了一下,发现可以出来aar包。
然后放入Unity中打包,还是会有错误,我这里出现的是两个错误:
1.是提示classes.jar重复的错误,log信息忘记保存了,这个网上能查到,解决方案就是把aar/bin目录下的classes.jar删除就好了。
Unity调用安卓相册选择图片,采用gradle打包,踩坑记录_第3张图片
2.是"D:\Unity2018.4.6\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-5.1.1.jar" org.gradle.launcher.GradleMain “-Dorg.gradle.jvmargs=-Xmx4096m” "assembleRelease"这个,不过这次已经可以在aar包里找到buildconfig.class这个文件了,把它删除掉就好了(删除时直接修改文件名为rar双击打开删除就行,如果是解压删除再压缩,我测试的unity发包时会提示格式错误,压缩软件的压缩方式和打包aar的压缩方式还是有区别的)。
Unity调用安卓相册选择图片,采用gradle打包,踩坑记录_第4张图片删除bin目录下的classes.jar后,外层还有一个classes.jar,Buildconfig.class文件就在这里。
如果有谁知道怎么能不删除Buildconfig.class解决这个问题,或者Buildconfig.class起的作用,希望告知下,我现在对原理其实不太清楚,纯粹是试出来的(-debug和-release的都可以使用,自己测试过了)。
以上两步弄完,再打包就出来了,我打包选择的都是Gradle选项。
终于出包了,激动啊,赶紧到模拟器跑一下,OK没问题,高兴!
放手机上试一下,吼吼吼,手机上打开相册可以,选择图片没效果,白高兴一场,连上monitor.exe看下log,发现是在选择图片之后,用IO流读取的时候,提示没有权限,郁闷啊,我明明在AndroidManifest里设置了权限的,又是一通查啊(我的手机是小米cc9pro,显示MIUI版本是11.0.9 Android版本是9)。
发现有人说安卓新版本的需要动态请求权限, 然后就试一下呗,试的时候我发现了这个API Permission.RequestUserPermission,看着就是获取授权用的,预感要行,然后又看了下Permission,发现ExternalStorageWrite和ExternalStorageRead不正是我加到AndroidManifest里的选项嘛。
Unity调用安卓相册选择图片,采用gradle打包,踩坑记录_第5张图片
因为想搞清楚原因,特意试了一下。Unity调用安卓相册选择图片,采用gradle打包,踩坑记录_第6张图片我在打开相册前调用了Permission.HasUserAuthorizedPermission()返回的果真的false,证明在AndroidManifest没有用。
于是我在Start里用Permission.RequestUserPermission()申请了下权限,再打包放到手机上测试,果然可以了。
本身是一个记录贴,所以写的可能啰嗦了些,希望可以帮到别人吧,更希望有大佬能解答一下我遇到的疑问,感激不尽!

你可能感兴趣的:(unity,安卓,android)