關於aar的一些坑

1. gradle工程如果有遠程依賴,則生成aar後,默認不會包含這些遠程依賴。所以如果主工程引用了若干aar,應該檢查aar對應的原工程是否有遠程依賴,如果有的話,都應該在主工程中添加。否則編譯會報NoClassDefineException。以github上的usb carema驅動爲例,引用其aar的同時應包含對應遠程依賴:

dependencies {
	compile fileTree(include: ['*.jar'], dir: 'libs')
	compile 'com.android.support:support-v4:26.+'

        compile(name: 'libuvccamera-release', ext: 'aar')
        compile(name: 'usbCameraCommon-release', ext: 'aar')
        compile "com.android.support:support-annotations:${supportLibVersion}"
        compile("com.serenegiant:common:${commonLibVersion}") {
            exclude module: 'support-v4'
        }
}

repositories {
    jcenter()
    flatDir {
        dirs('libs')
    }
}

2. 在子工程中引用了若干aar,在編譯時,主工程報錯:找不到對應的aar。解決的辦法如下:

第一種,在主工程的libs下添加對應的aar。比如子工程中引用了lib-release.aar,在主工程中的libs目錄也放一份,並配置gradle。

dependencies {
	compile fileTree(include: ['*.jar'], dir: 'libs')
	compile 'com.android.support:support-v4:26.+'
        compile(name: 'lib-release', ext: 'aar')
}

repositories {
    jcenter()
    flatDir {
        dirs('libs')
    }
}

第二種,在主工程build.gradle中配置子工程放置aar的路徑。xxx爲子工程的名字。

repositories {
    jcenter()
    flatDir {
        ...
        dirs project(':xxx').file('libs')
    }
}

第三種,在最外層Project下的build.gradle中配置:

allprojects {
    repositories {
        jcenter()
        flatDir {
            ...
            dirs project(':xxx').file('libs')
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章