0

I spent the last three days trying to read an .aar in Android Studio and I just can't figure it out. 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.

I know the class is there in the .aar file.

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.

I don't know if Android Studio is recognizing the file as a library. However, the files sync with the gradle successfully.

(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)

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

And solved the error the post reports by putting the .aar in the libs folder like Daniel Nugent said. However, when I try to write, in the Main:

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

AS doesn't recognize UnitPlayerActivity. This class is inside the .aar file supposedly. Before making the aar in my first AS project, it was here:

cube5->libs->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.

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. 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

3 Answers3

1

For android Studio 2.3.3 you can use

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. I did't check it for AS 3.0.

isabsent
  • 3,683
  • 3
  • 25
  • 46
  • 1
    Thanks for your help. I already have those lines in that file, and that is why I can sync the graddle with no errors. However, I don't know how to call something inside my aar file from my Main. Inside the .aar there is a folder called "libs" which has a unity-classes.jar file. I want to call a class that is in there. Any suggestion on how to do it from Main? – Gustavo Caetano Dec 01 '17 at 03:39
0

Add .aar file to your app by doing:

in project build.gradle

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

in app build.gradle

dependencies {
       compile(name:'cube5-debug', ext:'aar')
}
Jisu Hong
  • 724
  • 1
  • 7
  • 22
  • 1
    Thanks for your reply. I had the lines in app build.gradle but not in project build.gradle. I just added them but it doesn't seem to make a difference... I just don't know how to call the functions from Main. For instance, inside my aar file there is a folder called "libs" which has a unity-classes.jar file. I want to call one class from that file. I just don't know how to do it – Gustavo Caetano Dec 01 '17 at 03:35
  • If you've imported it right, when you call the class, Android Studio should automatically know that you are importing a class from the aar. You can check this by calling a class function auto-completing. – Jisu Hong Dec 01 '17 at 03:54
  • Just try to call `aar` classes from your MainActivity and you will see that AS gives your hints on them and auto imports needed classes if you agree with hints. – isabsent Dec 01 '17 at 04:25
  • I'm trying but AS doesn't give my any suggestions. I explained how I have imported the .aar, what can I see inside it with Winrar and what line I want to write in Main, in an EDIT. If you could see it I would much appreciate it. – Gustavo Caetano Dec 01 '17 at 15:45
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. I did this in order to use the Unity program inside AS. However, this gives error for some versions of Unity and AS. I installed Unity 5.5.0f3 and an older version of AS, and everything worked fine.

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). I did it by downloading windows 10 install and installing only that tool, which comes with the package.

Note2: I made the program from Unity work inside AS but only because it was a simple program. I later wanted to do an Augmented Reality App in Unity, and use it in AS, but it cannot be done apparently. 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.