Android uses the aar package and androidaar package

Source: Internet
Author: User

Android uses the aar package and androidaar package

Some time ago, I needed to build an sdk project. I was bored with what packaging method I used. If I used jar to package, jar can only package class files. If I wanted to package the resource files

If there are not many interfaces, you can use reflection to reference resource files through this reflection R.

  String packageName = context.getPackageName();        Class r = null;        int id = 0;        try {            r = Class.forName(packageName + ".R");            Class[] classes = r.getClasses();            Class desireClass = null;            for (int i = 0; i < classes.length; ++i) {                if (classes[i].getName().split("\\$")[1].equals(className)) {                    desireClass = classes[i];                    break;                }            }            if (desireClass != null)                id = desireClass.getField(name).getInt(desireClass);

Now it's not so troublesome, Android studio provides us with a simpler way, You can package the class and res files at the same time, now the support v7 and gson-2.21 is aar packaging method


Packaging method:

First, we need to package the project into sdk into Android libraries B, instead of building an Android project, and then we create A new Android project A to call the prepared libraries B.

After running, studio automatically packs librarys B into an aar package. This is the sdk we want (Path: libraries B \ build \ outputs \ aar \)


Usage:

1. Copy the prepared aar package to the libs directory of the Android project to be referenced.

2. Modify gradle settings of Android studio

Repositories {flatDir {dirs 'libs'} dependencies {// compile fileTree (dir: 'libs', include :['*. jar ','*. aar ']) compile 'com. android. support: appcompat-v7: 19. + 'compile project (name ': app2', ext: 'aar') // import and execute app2.aar}

Check if there is not much app2 under external lib (including a jar and a res), it means the operation is successful.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.