简体   繁体   English

如何从Main访问lib中的aar文件

[英]How to access aar file, in libs, from Main

I spent the last three days trying to read an .aar in Android Studio and I just can't figure it out. 我花了最后三天的时间尝试在Android Studio中阅读.aar,但我无法弄清楚。 My project view looks like this: 我的项目视图如下所示:

--- AnApplication

----------------------.gradle

-----------------------.idea

----------------------- app

-------------------------------- build

-------------------------------- libs

---------------------------------------- cube5-debug.aar

(...)

I want to access a class inside cube5-debug.aar from my main activity. 我想从我的主要活动中访问cube5-debug.aar中的类。

I know the class is there in the .aar file. 我知道该类在.aar文件中。

I don't know the precise import line I must write at the start of my main. 我不知道在主程序开始时必须写的确切导入行。 I have tried import com.company_cube.cube5 (the package of the project where I generated my .aar) and many other variants. 我尝试了导入com.company_cube.cube5(生成我的.aar的项目包)和许多其他变体。

I don't know if Android Studio is recognizing the file as a library. 我不知道 Android Studio是否将文件识别为库。 However, the files sync with the gradle successfully. 但是,文件与gradle成功同步。

(For context, the project where I generated the .aar was made by Unity, by exporting a Unity project into an Android one, and then generating the .aar from that one; the goal was to use this .aar file as a library in a second Android Studio project - the one I am talking about in this post) (对于上下文,我生成.aar的项目是Unity制作的,方法是将Unity项目导出到Android项目中,然后从该项目中生成.aar;目标是将该.aar文件用作第二个Android Studio项目-我在本文中谈论的那个项目)

I am patiently waiting for any help you can give me. 我正在耐心等待您可以提供的任何帮助。 Thanks 谢谢

EDIT: To import the .aar, I followed the images of this small post: Importing .aar in android Studio 编辑:要导入.aar,我关注了这个小文章的图像: 在android studio中导入.aar

And solved the error the post reports by putting the .aar in the libs folder like Daniel Nugent said. 并通过将.aar放到libs文件夹中来解决帖子报告的错误,如Daniel Nugent所说。 However, when I try to write, in the Main: 但是,当我尝试编写时,在Main中:

  • Intent intent = new Intent(getApplicationContext(),UnityPlayerActivity.class); Intent intent = new Intent(getApplicationContext(),UnityPlayerActivity.class);

AS doesn't recognize UnitPlayerActivity. AS无法识别UnitPlayerActivity。 This class is inside the .aar file supposedly. 此类位于.aar文件中。 Before making the aar in my first AS project, it was here: 在我的第一个AS项目中制作aar之前,它在这里:

cube5 -> libs -> unity-classes.jar -> com.unity3d.player -> UnityPlayerActivity cube5- > -> unity-classes.jar- > com.unity3d.player- > UnityPlayerActivity

When I open the .aar with Winrar, and open unity-classes.jar with a text editor, I see a almost everything as encoded symbols. 当我使用Winrar打开.aar并使用文本编辑器打开unity-classes.jar时,几乎看到的所有内容都是编码符号。

I'm not sure it is normal. 我不确定这是否正常。 Any idea? 任何想法?

EDIT 2: I think I know where the error is: when I import my .aar, I get an IDE fatal error. 编辑2:我想我知道错误在哪里:导入.aar时,出现IDE致命错误。 I don't know how to solve it, so if you can, please check the post I made about it: IDE fatal error on importing .aar 我不知道如何解决它,所以如果可以的话,请检查我所做的文章: 导入.aar时发生IDE致命错误

Add .aar file to your app by doing: 通过执行以下操作将.aar文件添加到您的应用中:

in project build.gradle 在项目build.gradle

allprojects {
   repositories {
      flatDir {
        dirs 'libs'
      }
   }
}

in app build.gradle 在应用程序build.gradle

dependencies {
       compile(name:'cube5-debug', ext:'aar')
}

For android Studio 2.3.3 you can use 对于Android Studio 2.3.3,您可以使用

repositories {
    flatDir {
        dirs 'libs' //this way we can find the .aar file in libs folder
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile(name: 'cube5-debug', ext: 'aar')
}

in the app level build.gradle . app级别build.gradle I did't check it for AS 3.0. 我没有检查AS 3.0。

So I managed to solve it. 所以我设法解决了。 The .aar file I talked about was exported with AS, after I imported an Unity project into AS. 我将Unity项目导入AS后,我讨论的.aar文件已随AS导出。 I did this in order to use the Unity program inside AS. 我这样做是为了在AS中使用Unity程序。 However, this gives error for some versions of Unity and AS. 但是,这为某些版本的Unity和AS提供了错误。 I installed Unity 5.5.0f3 and an older version of AS, and everything worked fine. 我安装了Unity 5.5.0f3和较旧版本的AS,一切正常。

Note1: if you aim to install that Unity version, you will be prompted with a requirement of some windows 10 tool, which you must get (I cannot remember the name). 注意1:如果您打算安装该Unity版本,系统将提示您某些Windows 10工具的要求,您必须获得该工具(我不记得它的名称)。 I did it by downloading windows 10 install and installing only that tool, which comes with the package. 我是通过下载Windows 10安装程序并仅安装软件包随附的该工具来实现的。

Note2: I made the program from Unity work inside AS but only because it was a simple program. 注意2:我使Unity中的程序可以在AS内运行,但这仅仅是因为它是一个简单的程序。 I later wanted to do an Augmented Reality App in Unity, and use it in AS, but it cannot be done apparently. 后来我想在Unity中制作一个增强现实应用程序,并在AS中使用它,但是显然无法完成。 Evertyting works fine until I import the .aar into AS, and a class in that .aar library gets unresolved ("Video player”). This should have to do with the AR camera class, which AS does not recognize. I ended up creating a AR app with Unity, another app in AS, and from the AS app, calling the Unity app with two lines of code, whenever necessary. To return to the AS app, now in background, I simple click the back button, which is recognized by the Unity app (if you make code in Unity for it) and makes it destroy itself, returning to the AS app. 直到我将.aar导入到AS中,并且无法解析该.aar库中的类(“视频播放器”),Evertyting都可以正常工作。这应该与AS无法识别的AR相机类有关。我最终创建了一个带有Unity的AR应用程序,另一个是AS中的应用程序,并在需要时从AS应用程序中用两行代码调用Unity应用程序。要返回AS应用程序(现在处于后台),我只需单击后退按钮,即被Unity应用识别(如果您在Unity中为其编写代码),并使其自身销毁,返回AS应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM