Improve Android compatibility and Maven publication configuration
This commit is contained in:
1 parent
96c1d26a57
commit
e9d9281333
16 files changed
+226
-143
No files matched your search
@@ -0,0 +1,34 @@
|
||||
# Android 兼容性说明
|
||||
|
||||
本次保留 minSdk 15、compileSdk/targetSdk 36 和现有发布坐标;Android 17/API 37 的宿主接入要求如下,尚未宣称通过 API 37 真机验证。
|
||||
|
||||
## 存储
|
||||
|
||||
- 内部 files/cache 目录无需存储权限。API 19+ 的 getExternalFilesDir 也无需存储权限,外部卷可能不可用。
|
||||
- 公共下载目录在现代系统上优先使用现有 MediaStore/SAF Uri 下载接口。SAF 必须由宿主获取读写授权;需要跨重启恢复时保存系统实际授予的持久权限。授权撤销后必须重新选择。
|
||||
- READ_MEDIA_IMAGES/VIDEO/AUDIO 只授权相应媒体读取,不授予任意文件写权限。不能用任意一种媒体权限证明所有下载/上传路径可访问。
|
||||
- 旧系统共享目录仍需宿主按路径和操作申请 READ/WRITE_EXTERNAL_STORAGE。移除统一权限门槛不代表绕过系统访问限制;路径检查与实际 I/O 仍会失败。
|
||||
- MANAGE_EXTERNAL_STORAGE 不作为一般下载接入前提;已有示例中的全文件访问仅用于其旧路径演示,应按产品需求改用私有目录或 Uri。
|
||||
|
||||
## 网络和后台
|
||||
|
||||
- 宿主声明 INTERNET;网络监测需要 ACCESS_NETWORK_STATE。API 24+ 监听默认网络,API 21–23 使用不限定传输类型的回调,更旧版本实时查询。
|
||||
- 网络存在不保证服务器可达;局域网连接无需通过公网 VALIDATED 检查,连接失败仍由协议层处理。
|
||||
- targetSdk 37+ 在 Android 17 访问局域网 HTTP/FTP/SFTP 服务时,宿主需按业务声明并在前台请求 ACCESS_LOCAL_NETWORK;只访问公网时无需申请。库不弹权限框、不代替宿主管理授权。
|
||||
- 库的线程任务不保证进程退出后的后台持续运行。宿主如需长时间用户可见传输,应按目标系统的前台服务类型、启动限制和时限设计生命周期;不要仅提高 targetSdk 后假设后台任务持续运行。
|
||||
- HTTPS 优先;如确需明文 HTTP,由宿主对具体域名配置 Network Security Config。
|
||||
|
||||
## 待设备验证矩阵
|
||||
|
||||
| 系统 | 场景 | 预期 |
|
||||
| --- | --- | --- |
|
||||
| API 15–18 | 私有目录、共享目录拒绝权限 | 私有下载可用;共享访问明确失败 |
|
||||
| API 19–28 | 应用外部目录,不申请存储权限 | 下载与恢复可用 |
|
||||
| API 29–36 | MediaStore/SAF、权限撤销、私有目录 | 正常写入;撤销后明确失败或重新授权 |
|
||||
| API 21+ | Wi-Fi/蜂窝/以太网/VPN、多网络切换 | 不因传输类型漏判,断网可恢复 |
|
||||
| API 37,宿主 target 37 | 局域网授权允许/拒绝,公网 | 授权后局域网可访问;拒绝不影响公网 |
|
||||
|
||||
官方依据:
|
||||
- https://developer.android.com/training/data-storage/app-specific
|
||||
- https://developer.android.com/develop/connectivity/network-ops/reading-network-state
|
||||
- https://developer.android.com/privacy-and-security/local-network-permission
|
||||
@@ -35,6 +35,12 @@ android {
|
||||
|
||||
}
|
||||
|
||||
android {
|
||||
publishing {
|
||||
singleVariant('release')
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
|
||||
@@ -17,7 +17,6 @@ package com.arialyy.aria.core.common.controller;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
@@ -162,74 +161,16 @@ public abstract class FeatureController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查权限,需要的权限有
|
||||
* {@link Manifest.permission#INTERNET}
|
||||
* 存储权限会根据 Android 版本与下载模式(分区存储 Uri / 普通文件路径)进行适配:
|
||||
* <ul>
|
||||
* <li>分区存储 Uri 模式(MediaStore / SAF):跨 Android 版本均无需申请存储权限</li>
|
||||
* <li>Android 9 及以下:{@link Manifest.permission#WRITE_EXTERNAL_STORAGE} +
|
||||
* {@link Manifest.permission#READ_EXTERNAL_STORAGE}</li>
|
||||
* <li>Android 10(Q):同上,仍可请求 legacy 存储</li>
|
||||
* <li>Android 11/12(R/S):{@link Manifest.permission#WRITE_EXTERNAL_STORAGE} 已废弃,
|
||||
* 仅需 {@link Manifest.permission#READ_EXTERNAL_STORAGE}</li>
|
||||
* <li>Android 13+(TIRAMISU):READ/WRITE_EXTERNAL_STORAGE 不再强制,使用细分媒体权限
|
||||
* {@link Manifest.permission#READ_MEDIA_IMAGES} / {@link Manifest.permission#READ_MEDIA_VIDEO} /
|
||||
* {@link Manifest.permission#READ_MEDIA_AUDIO},或兼容旧 target 的 READ_EXTERNAL_STORAGE</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return {@code false} 缺少权限
|
||||
* 这里只检查网络基础权限。存储访问能力取决于具体路径、文件所有权和 Uri 授权,
|
||||
* 不能通过任意 READ_MEDIA_* 权限或系统版本统一判断。
|
||||
* 私有目录不需要存储权限;共享文件由实体检查和实际 I/O 验证访问能力。
|
||||
*/
|
||||
private boolean checkPermission() {
|
||||
if (!hasPermission(Manifest.permission.INTERNET)) {
|
||||
ALog.e(TAG, "启动失败,缺少权限:Manifest.permission.INTERNET");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 分区存储 Uri 模式(MediaStore / SAF)写入 Uri,跨 Android 版本均无需存储权限
|
||||
if (isUriTask()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int sdk = Build.VERSION.SDK_INT;
|
||||
if (sdk >= Build.VERSION_CODES.TIRAMISU) {
|
||||
// Android 13+:READ/WRITE_EXTERNAL_STORAGE 已不再强制,使用细分媒体权限;
|
||||
// 同时声明 READ_EXTERNAL_STORAGE 在 13+ 仍会被授予(兼容旧 target),二者满足其一即可
|
||||
if (!hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
&& !hasPermission(Manifest.permission.READ_MEDIA_IMAGES)
|
||||
&& !hasPermission(Manifest.permission.READ_MEDIA_VIDEO)
|
||||
&& !hasPermission(Manifest.permission.READ_MEDIA_AUDIO)) {
|
||||
ALog.e(TAG, "启动失败,缺少权限:Android 13+ 需要 Manifest.permission.READ_MEDIA_* "
|
||||
+ "或 Manifest.permission.READ_EXTERNAL_STORAGE");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else if (sdk >= Build.VERSION_CODES.R) {
|
||||
// Android 11/12:WRITE_EXTERNAL_STORAGE 已废弃且不再授予共享存储写权限,读仍可通过
|
||||
// READ_EXTERNAL_STORAGE 兼容获取
|
||||
if (!hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
||||
ALog.e(TAG, "启动失败,缺少权限:Manifest.permission.READ_EXTERNAL_STORAGE");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
// Android 10 及以下:需要读写外部存储权限
|
||||
if (!hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||
ALog.e(TAG, "启动失败,缺少权限:Manifest.permission.WRITE_EXTERNAL_STORAGE");
|
||||
return false;
|
||||
}
|
||||
if (!hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
||||
ALog.e(TAG, "启动失败,缺少权限:Manifest.permission.READ_EXTERNAL_STORAGE");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为分区存储 Uri 任务,Uri 模式下通过 MediaStore / SAF 写入,无需存储权限
|
||||
*/
|
||||
private boolean isUriTask() {
|
||||
return mTaskWrapper instanceof DTaskWrapper && ((DTaskWrapper) mTaskWrapper).isUriMode();
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean hasPermission(String permission) {
|
||||
|
||||
@@ -35,6 +35,10 @@ android {
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
singleVariant('release')
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -44,6 +44,12 @@ dependencies {
|
||||
}
|
||||
|
||||
//apply from: 'bintray-release.gradle'
|
||||
android {
|
||||
publishing {
|
||||
singleVariant('release')
|
||||
}
|
||||
}
|
||||
|
||||
ext {
|
||||
PUBLISH_ARTIFACT_ID = 'http'
|
||||
}
|
||||
|
||||
@@ -46,6 +46,12 @@ dependencies {
|
||||
}
|
||||
|
||||
//apply from: 'bintray-release.gradle'
|
||||
android {
|
||||
publishing {
|
||||
singleVariant('release')
|
||||
}
|
||||
}
|
||||
|
||||
ext {
|
||||
PUBLISH_ARTIFACT_ID = 'm3u8'
|
||||
}
|
||||
|
||||
@@ -45,6 +45,12 @@ dependencies {
|
||||
}
|
||||
|
||||
//apply from: 'bintray-release.gradle'
|
||||
android {
|
||||
publishing {
|
||||
singleVariant('release')
|
||||
}
|
||||
}
|
||||
|
||||
ext {
|
||||
PUBLISH_ARTIFACT_ID = 'public'
|
||||
}
|
||||
|
||||
@@ -59,8 +59,9 @@ public class AriaConfig {
|
||||
/**
|
||||
* 是否已经联网,true 已经联网
|
||||
*/
|
||||
private static boolean isConnectedNet = true;
|
||||
private static volatile boolean isConnectedNet = true;
|
||||
private Handler mAriaHandler;
|
||||
private volatile boolean networkCallbackRegistered;
|
||||
|
||||
private AriaConfig(Context context) {
|
||||
APP = context.getApplicationContext();
|
||||
@@ -134,56 +135,58 @@ public class AriaConfig {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
||||
NetworkRequest request = builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
||||
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
|
||||
.build();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
cm.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback() {
|
||||
ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() {
|
||||
private final java.util.Set<Network> networks = new java.util.HashSet<>();
|
||||
|
||||
@Override public void onLost(Network network) {
|
||||
super.onLost(network);
|
||||
isConnectedNet = isNetworkAvailable();
|
||||
ALog.d(TAG, "onLost, isConnectNet = " + isConnectedNet);
|
||||
}
|
||||
|
||||
@Override public void onAvailable(Network network) {
|
||||
super.onAvailable(network);
|
||||
isConnectedNet = true;
|
||||
ALog.d(TAG, "onAvailable, isConnectNet = true");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNetworkAvailable() {
|
||||
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
|
||||
ConnectivityManager connectivityManager =
|
||||
(ConnectivityManager) getAPP().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
if (connectivityManager == null) {
|
||||
|
||||
return false;
|
||||
} else {
|
||||
// 获取NetworkInfo对象
|
||||
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
|
||||
|
||||
if (networkInfo != null && networkInfo.length > 0) {
|
||||
for (NetworkInfo info : networkInfo) {
|
||||
// 判断当前网络状态是否为连接状态
|
||||
if (info.getState() == NetworkInfo.State.CONNECTED) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@Override public synchronized void onAvailable(Network network) {
|
||||
networks.add(network);
|
||||
isConnectedNet = true;
|
||||
}
|
||||
|
||||
@Override public synchronized void onLost(Network network) {
|
||||
networks.remove(network);
|
||||
isConnectedNet = !networks.isEmpty();
|
||||
}
|
||||
};
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
cm.registerDefaultNetworkCallback(callback);
|
||||
} else {
|
||||
// 不限制传输类型,支持以太网和 VPN;允许局域网传输,不要求公网验证。
|
||||
NetworkRequest request = new NetworkRequest.Builder()
|
||||
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
|
||||
.build();
|
||||
cm.registerNetworkCallback(request, callback);
|
||||
}
|
||||
networkCallbackRegistered = true;
|
||||
} catch (SecurityException | IllegalArgumentException e) {
|
||||
ALog.w(TAG, "无法注册网络监听:" + e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public boolean isNetworkAvailable() {
|
||||
ConnectivityManager cm =
|
||||
(ConnectivityManager) getAPP().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (cm == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
return cm.getActiveNetwork() != null;
|
||||
}
|
||||
NetworkInfo info = cm.getActiveNetworkInfo();
|
||||
return info != null && info.isConnected();
|
||||
} catch (SecurityException e) {
|
||||
// 宿主移除 ACCESS_NETWORK_STATE 时不能因此阻止网络 I/O。
|
||||
ALog.w(TAG, "无法查询网络状态:" + e.getMessage());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnectedNet() {
|
||||
return isConnectedNet;
|
||||
// 旧系统、关闭监听或注册失败时,查询实时状态。
|
||||
return !networkCallbackRegistered ? isNetworkAvailable() : isConnectedNet;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -259,7 +259,7 @@ class DelegateFind extends AbsDelegate {
|
||||
|
||||
try {
|
||||
while (cursor.moveToNext()) {
|
||||
int pRowId = cursor.getInt(cursor.getColumnIndex(PARENT_COLUMN_ALIAS.concat("rowid")));
|
||||
int pRowId = cursor.getInt(cursor.getColumnIndexOrThrow(PARENT_COLUMN_ALIAS.concat("rowid")));
|
||||
if (childs.get(pRowId) == null) {
|
||||
childs.put(pRowId, new ArrayList<DbEntity>());
|
||||
parents.put(pRowId, createParent(pRowId, parentClazz, pColumn, cursor));
|
||||
@@ -331,7 +331,7 @@ class DelegateFind extends AbsDelegate {
|
||||
Cursor cursor)
|
||||
throws InstantiationException, IllegalAccessException {
|
||||
T child = childClazz.newInstance();
|
||||
child.rowID = cursor.getInt(cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat("rowid")));
|
||||
child.rowID = cursor.getInt(cursor.getColumnIndexOrThrow(CHILD_COLUMN_ALIAS.concat("rowid")));
|
||||
for (Field field : cColumn) {
|
||||
field.setAccessible(true);
|
||||
int columnIndex = cursor.getColumnIndex(CHILD_COLUMN_ALIAS.concat(field.getName()));
|
||||
@@ -515,7 +515,7 @@ class DelegateFind extends AbsDelegate {
|
||||
}
|
||||
//当设置了主键,而且主键的类型为integer时,查询RowID等于主键
|
||||
entity.rowID = cursor.getInt(
|
||||
cursor.getColumnIndex(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName));
|
||||
cursor.getColumnIndexOrThrow(TextUtils.isEmpty(primaryName) ? "rowid" : primaryName));
|
||||
//mDataCache.put(getCacheKey(entity), entity);
|
||||
entitys.add(entity);
|
||||
}
|
||||
@@ -587,7 +587,7 @@ class DelegateFind extends AbsDelegate {
|
||||
int[] ids = new int[cursor.getCount()];
|
||||
int i = 0;
|
||||
while (cursor.moveToNext()) {
|
||||
ids[i] = cursor.getInt(cursor.getColumnIndex("rowid"));
|
||||
ids[i] = cursor.getInt(cursor.getColumnIndexOrThrow("rowid"));
|
||||
i++;
|
||||
}
|
||||
cursor.close();
|
||||
|
||||
@@ -212,7 +212,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
List<String> oldTabColumns = new ArrayList<>();
|
||||
|
||||
while (columnC.moveToNext()) {
|
||||
String columnName = columnC.getString(columnC.getColumnIndex("name"));
|
||||
String columnName = columnC.getString(columnC.getColumnIndexOrThrow("name"));
|
||||
oldTabColumns.add(columnName);
|
||||
}
|
||||
columnC.close();
|
||||
@@ -397,8 +397,8 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
c = db.rawQuery(uSql, null);
|
||||
while (c.moveToNext()) {
|
||||
int type;
|
||||
String filePath = c.getString(c.getColumnIndex("filePath"));
|
||||
String url = c.getString(c.getColumnIndex("url"));
|
||||
String filePath = c.getString(c.getColumnIndexOrThrow("filePath"));
|
||||
String url = c.getString(c.getColumnIndexOrThrow("url"));
|
||||
if (url.startsWith("ftp") || url.startsWith("sftp")) {
|
||||
type = ITaskWrapper.D_FTP;
|
||||
} else {
|
||||
|
||||
@@ -21,7 +21,6 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
@@ -344,10 +343,8 @@ public class CommonUtil {
|
||||
if (file != null) {
|
||||
dir = file.getPath() + "/";
|
||||
} else {
|
||||
dir = Environment.getExternalStorageDirectory().getPath()
|
||||
+ "/Android/data/"
|
||||
+ context.getPackageName()
|
||||
+ "/files/";
|
||||
// Android 11+ 不允许手工创建外部应用目录,外部卷不可用时使用内部目录。
|
||||
dir = context.getFilesDir().getPath() + "/";
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
@@ -647,21 +647,14 @@ public class FileUtil {
|
||||
IllegalAccessException {
|
||||
List<String> pathList = new ArrayList<>();
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
StorageManager manager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
|
||||
List<StorageVolume> volumes = manager.getStorageVolumes();
|
||||
|
||||
Class<?> volumeClass = StorageVolume.class;
|
||||
Method getPath = volumeClass.getDeclaredMethod("getPath");
|
||||
//Method isRemovable = volumeClass.getDeclaredMethod("isRemovable");
|
||||
getPath.setAccessible(true);
|
||||
//isRemovable.setAccessible(true);
|
||||
|
||||
for (StorageVolume volume : volumes) {
|
||||
String state = volume.getState();
|
||||
|
||||
if (state.equals(Environment.MEDIA_MOUNTED)) {
|
||||
pathList.add((String) getPath.invoke(volume));
|
||||
if (manager != null) {
|
||||
for (StorageVolume volume : manager.getStorageVolumes()) {
|
||||
File directory = volume.getDirectory();
|
||||
if (Environment.MEDIA_MOUNTED.equals(volume.getState()) && directory != null) {
|
||||
pathList.add(directory.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -103,10 +103,23 @@ public class NetUtils {
|
||||
return netWorkType;
|
||||
}
|
||||
|
||||
// 可选权限由宿主声明;下方已检查授权并捕获运行时撤销。
|
||||
@android.annotation.SuppressLint("MissingPermission")
|
||||
private static boolean isFastMobileNetwork(Context context) {
|
||||
TelephonyManager telephonyManager =
|
||||
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
switch (telephonyManager.getNetworkType()) {
|
||||
if (telephonyManager == null || context.checkCallingOrSelfPermission(
|
||||
android.Manifest.permission.READ_PHONE_STATE)
|
||||
!= android.content.pm.PackageManager.PERMISSION_GRANTED) {
|
||||
return false;
|
||||
}
|
||||
int networkType;
|
||||
try {
|
||||
networkType = telephonyManager.getNetworkType();
|
||||
} catch (SecurityException e) {
|
||||
return false;
|
||||
}
|
||||
switch (networkType) {
|
||||
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
||||
return false; // ~ 50-100 kbps
|
||||
case TelephonyManager.NETWORK_TYPE_CDMA:
|
||||
|
||||
@@ -201,7 +201,8 @@ public class UriFileUtil {
|
||||
* 在系统 Download 目录插入一条 MediaStore 记录(供只有文件名、没有原始路径的旧任务兜底重建使用)
|
||||
*/
|
||||
private static Uri createDownloadUri(Context context, String displayName) {
|
||||
if (context == null || TextUtils.isEmpty(displayName)) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q
|
||||
|| context == null || TextUtils.isEmpty(displayName)) {
|
||||
return null;
|
||||
}
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
@@ -47,6 +47,12 @@ dependencies {
|
||||
}
|
||||
|
||||
//apply from: 'bintray-release.gradle'
|
||||
android {
|
||||
publishing {
|
||||
singleVariant('release')
|
||||
}
|
||||
}
|
||||
|
||||
ext {
|
||||
PUBLISH_ARTIFACT_ID = 'sftp'
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import org.gradle.api.credentials.HttpHeaderCredentials
|
||||
import org.gradle.api.publish.tasks.GenerateModuleMetadata
|
||||
import org.gradle.authentication.http.HttpHeaderAuthentication
|
||||
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'signing'
|
||||
|
||||
@@ -6,6 +10,33 @@ ext {
|
||||
PUBLISH_VERSION = rootProject.ext.mavenVersion
|
||||
}
|
||||
|
||||
group = PUBLISH_GROUP_ID
|
||||
version = PUBLISH_VERSION
|
||||
|
||||
// 项目依赖在 POM 中的坐标需与已发布构件的 artifactId 对应
|
||||
def publishedArtifactIds = [
|
||||
'Aria' : 'core',
|
||||
'AriaAnnotations': 'annotations',
|
||||
'AriaCompiler' : 'compiler',
|
||||
'PublicComponent': 'public',
|
||||
'HttpComponent' : 'http',
|
||||
'M3U8Component' : 'm3u8',
|
||||
'SFtpComponent' : 'sftp',
|
||||
'FtpComponent' : 'ftp'
|
||||
]
|
||||
|
||||
def giteaToken = project.findProperty('giteaToken') ?: System.getenv('GITEA_TOKEN')
|
||||
|
||||
// Gitea 不支持覆盖版本,也不校验签名;本地或显式指定仓库地址时仍走原流程
|
||||
def giteaPublish = !isLocal() &&
|
||||
!project.hasProperty('RELEASE_REPOSITORY_URL') &&
|
||||
!project.hasProperty('SNAPSHOT_REPOSITORY_URL')
|
||||
|
||||
// 部分模块名与发布 artifactId 不一致,禁用 .module 元数据避免消费方按项目名解析
|
||||
tasks.withType(GenerateModuleMetadata).configureEach {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
|
||||
//编译groovy代码时采用 UTF-8
|
||||
tasks.withType(GroovyCompile) {
|
||||
@@ -55,13 +86,13 @@ static def localMavenRepo() {
|
||||
def getReleaseRepositoryUrl() {
|
||||
return isLocal() ? localMavenRepo()
|
||||
: hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
|
||||
: 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
|
||||
: 'https://gitea.ttstd.com/api/packages/maven/maven'
|
||||
}
|
||||
|
||||
def getSnapshotRepositoryUrl() {
|
||||
return isLocal() ? localMavenRepo()
|
||||
: hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
|
||||
: 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
|
||||
: 'https://gitea.ttstd.com/api/packages/maven/maven'
|
||||
}
|
||||
|
||||
def isLocal() {
|
||||
@@ -71,6 +102,8 @@ def isLocal() {
|
||||
afterEvaluate { project ->
|
||||
if (isLocal()) {
|
||||
println("上传到本地仓库")
|
||||
} else if (giteaPublish) {
|
||||
println("上传到 Gitea 私有仓库")
|
||||
} else {
|
||||
println("上传到中央仓库")
|
||||
}
|
||||
@@ -162,7 +195,10 @@ afterEvaluate { project ->
|
||||
archiveBaseName = "${JAR_PREFIX}${project.name}-cache"
|
||||
}
|
||||
|
||||
artifact androidLibraryJar
|
||||
artifact(androidLibraryJar) {
|
||||
// 作为附属构件,避免与主 AAR 争抢无 classifier 主构件导致 packaging 变 pom
|
||||
classifier = 'classes'
|
||||
}
|
||||
artifact androidSourcesJar
|
||||
artifact androidJavadocsJar
|
||||
|
||||
@@ -211,26 +247,57 @@ afterEvaluate { project ->
|
||||
//分支地址:
|
||||
url = rootProject.ext.mavenCentralTreeURL
|
||||
}
|
||||
withXml {
|
||||
asNode().dependencies?.dependency?.each { dep ->
|
||||
def projectName = dep.artifactId?.text()
|
||||
if (projectName && publishedArtifactIds.containsKey(projectName)) {
|
||||
if (dep.groupId) {
|
||||
dep.groupId[0].value = PUBLISH_GROUP_ID
|
||||
} else {
|
||||
dep.appendNode('groupId', PUBLISH_GROUP_ID)
|
||||
}
|
||||
dep.artifactId[0].value = publishedArtifactIds[projectName]
|
||||
if (dep.version) {
|
||||
dep.version[0].value = PUBLISH_VERSION
|
||||
} else {
|
||||
dep.appendNode('version', PUBLISH_VERSION)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name = "mavencentral"
|
||||
name = giteaPublish ? "gitea" : "mavencentral"
|
||||
|
||||
def releasesRepoUrl = getReleaseRepositoryUrl()
|
||||
def snapshotsRepoUrl = getSnapshotRepositoryUrl()
|
||||
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
|
||||
|
||||
credentials {
|
||||
username ossrhUsername
|
||||
password ossrhPassword
|
||||
if (giteaPublish) {
|
||||
if (!giteaToken) {
|
||||
throw new GradleException("缺少 giteaToken:请写入 ~/.gradle/gradle.properties 或设置环境变量 GITEA_TOKEN")
|
||||
}
|
||||
credentials(HttpHeaderCredentials) {
|
||||
name = "Authorization"
|
||||
value = "token ${giteaToken}"
|
||||
}
|
||||
authentication {
|
||||
header(HttpHeaderAuthentication)
|
||||
}
|
||||
} else if (!isLocal()) {
|
||||
credentials {
|
||||
username ossrhUsername
|
||||
password ossrhPassword
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
signing {
|
||||
required { !isLocal() && project.findProperty("signing.keyId") }
|
||||
required { !isLocal() && !giteaPublish && project.findProperty("signing.keyId") }
|
||||
sign publishing.publications
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user