天天看點

android studio 使用.so .jar .aar檔案

參考> http://www.androidchina.net/2467.html

android studio 使用.so .jar .aar檔案

1.使用.jar

(1)利用工具添加jar

将jar拷貝至工程的libs下(若不存在則建立)

android studio 使用.so .jar .aar檔案

open module setting>>Dependencies>>+>>File Dependence>> 添加libs下的.jar檔案

android studio 使用.so .jar .aar檔案

就成功添加了。

(2)手動添加jar

打開module下的build.gradle,在dependencies裡面添加依賴檔案

第一種方法:添加單個檔案

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile files('libs/first.jar') //compile files('jar包的路徑')
}
           

第二種方法:添加目錄下符合要求的檔案

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile fileTree(include: ['*.jar'], dir: 'libs')//添加libs目錄下所有的.jar包
}
           

該方法在需要導入多jar包的工程中使用更廣

2.使用.aar

上文提到的compile fileTree不支援aar,是以需要按照以下步驟操作:

1.将aar放置于libs

2.打開module的build.gradle,添加

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

3.在dependencies中添加

3.使用.so

添加時有個注意點就是添加的路徑要設定正确,Android Studio 預設的so檔案路徑是app/src/main/jniLibs/armeabi

可以在main下面建立一個jniLibs目錄,然後将檔案全部複制進去便可以運作程式了

android studio 使用.so .jar .aar檔案

如果你想統一管理各種第三方檔案,例如放在libs中

android studio 使用.so .jar .aar檔案

需要在gradle的android {}中添加以下代碼來更改預設路徑

sourceSets {
        main {
            jniLibs.srcDir "libs"
        }
    }
           

編譯或運作時若提示找不到庫,clean project再運作

繼續閱讀