有時候開發者會需要用到其他廠商所撰寫的API,就有機會使用匯入 aar 檔案的技巧
本範例是以 Google Pay 串接 Tap Pay 時,需要先置入 Tap Pay 的 API 為題。
首先到 Tap Pay 下載 aar 檔案,請點選 「 tpdirect.aar 」 ,連結
再來進入到 Android Studio 選擇「Project FIle」模式,將 aar 檔案置入 app/libs 的資料夾內(參考下圖)
置入完檔案,緊接著就是要透過程式碼,請android studio載入檔案內容:
Manifest 中增加 'tools:replace="android:allowBackup" ,說明請看補充資料。
Gradle (Project) 中增加檔案路徑。
Gradle (Apps) 中需要讀取資料的檔案名稱及格式
最後按下 sync 重新整理即可完整檔案載入。
-
<application android:allowBackup="true" tools:replace="android:allowBackup" <!-- 關鍵的一行 --> android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity">
allprojects { repositories { google() jcenter() flatDir { dirs 'libs' } //增加此段落 } }
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation(name: 'tpdirect', ext: 'aar') //關鍵的段落,要把檔案名稱與格式撰寫清楚 implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
補充資料:
什麼是allowBackUp
https://blog.csdn.net/kite30/article/details/45922475
https://blog.csdn.net/qq_16131393/article/details/51556050
文章標籤
全站熱搜