打包aar并发布到远程_Gradle实战:发布aar包到maven仓库

这篇博客介绍了在Gradle中如何打包aar并发布到远程maven仓库,同时解决了一个问题:无法设置debug编译类型。文章提供了通过反射获取应用编译类型的代码示例,并针对4.4以下系统版本可能出现的空指针错误提出了完整解决方案。
摘要由CSDN通过智能技术生成

问题三:无法设置debug编译类型

在lib工程中无论怎么设置编译类型,最后生成的aar包中始终都是release版本,该问题见google反馈。既然不可设置编译类型,我们可以在aar包代码中通过反射来获取应用的编译类型:

private Object getBuildConfigValue(Context context, String fieldName) {

try {

Class> clazz = Class.forName(context.getPackageName() + ".BuildConfig");

Field field = clazz.getField(fieldName);

return field.get(null);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

return null;

}

//使用

String buildType = getBuildConfigValue(ctx,"BUILD_TYPE").toString();

if (!TextUtils.isEmpty(buildType) && buildType.equals("debug")) { // debug

...

} else { // release

...

}

但是,这里面还有一个坑,系统版本在4.4以下的设备中,该方法无法获得包名,会抛空指针错误。以下我们给出完整的解决方案:

public class BuildConfigProvider {

private static Context sContext;

private static String packageName;

public static String getBuildType() {

String buildType = (String) getBuildConfigValue("BUILD_TYPE");

if ("debug".equals(buildType)) {

buildType = "debug";

}

if ("release".equals(buildType)) {

buildType = "release";

}

return buildType;

}

public static final boolean isDebug() {

return BuildConfig.DEBUG;

}

/**

* 通过反射获取ApplicationContext

*

* @return

*/

private static Context getContext() {

if (sContext == null) {

try {

final Class> activityThreadClass = Class.forName("android.app.ActivityThread");

final Method currentActivityThread = activityThreadClass.getDeclaredMethod("currentActivityThread");

final Object activityThread = currentActivityThread.invoke(null);

final Method getApplication = activityThreadClass.getDeclaredMethod("getApplication");

final Application application = (Application) getApplication.invoke(activityThread);

sContext = application.getApplicationContext();

} catch (Exception e) {

e.printStackTrace();

}

}

return sContext;

}

/**

* 通过反射获取包名

*

* @return

*/

private static String getPackageName() {

if (packageName == null) {

try {

final Class> activityThreadClass = Class.forName("android.app.ActivityThread");

final Method currentPackageName = activityThreadClass.getDeclaredMethod("currentPackageName");

packageName = (String) currentPackageName.invoke(null);

} catch (Exception e) {

packageName = getContext().getPackageName();

}

}

return packageName;

}

public static Object getBuildConfigValue(String fieldName) {

try {

Class> clazz = Class.forName(packageName + ".BuildConfig");

Field field = clazz.getField(fieldName);

return field.get(null);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (IndexOutOfBoundsException e) {

e.printStackTrace();

}

return "";

}

}

当然,有人可能会说,既然可以通过反射得到ApplicationContext,就没必要再去反射获得包名了,这里只是提供不同的解决方案以作参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发布本地 AAR 包,您需要完成以下步骤: 1. 在您的项目根目录中的 `build.gradle` 文件中添加 Maven 插件: ``` plugins { id 'maven-publish' } ``` 2. 配置发布信息。在 `build.gradle` 文件中添加以下内容: ``` group = 'com.example' // 填写您想要发布的包的 Group ID version = '1.0.0' // 填写您想要发布的包的版本号 publishing { publications { MyPublication(MavenPublication) { artifact file('path/to/your/aar/file') // 填写您想要发布AAR 包的路径 groupId group version version artifactId 'your-library-name' // 填写您想要发布的包的 Artifact ID pom.withXml { // 填写您想要发布的包的信息 def dependenciesNode = asNode().appendNode('dependencies') def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', 'com.example') dependencyNode.appendNode('artifactId', 'example-library') dependencyNode.appendNode('version', '1.0.0') } } } repositories { mavenLocal() //配置本地 maven 仓库 } } ``` 注意:在 `pom.withXml` 中,您需要填写您想要发布的包的信息,例如依赖项等。 3. 运行 `./gradlew publish` 命令,Gradle 将会发布您的 AAR 包到本地 Maven 仓库中。如果您需要发布到其他 Maven 仓库,请将 `mavenLocal()` 替换为其他 Maven 仓库 URL。 4. 如果您的 AAR 包依赖其他库,您需要将这些库发布到本地仓库或其他 Maven 仓库中,否则其他开发者在使用您的 AAR 包时会出现依赖错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值