${applicationId}.wxapi.wxentryActivity. This operation is very painful in multi-package names or componentized projects. You can use the CustomPackage library to decouple this operation

The role of CustomPackage, for example

This class, for example, has the full class name aaA.wxentryActivity

package aaa;
 
import pokercc.android.custompakcage.CustomPackage;

@CustomPackage("bbb")
public class WxEntryActivity  {}Copy the code

A bbb.WxEntryActivity class is generated at compile time

package bbb;
/* * create by CustomPackageProcessor don't modify!! * /
public class WxEntryActivity  extends aaa.WxEntryActivity{}Copy the code

${applicationId}.wxapi.wxentryActivity = ${applicationId}.wxapi.wxentryActivity

How does it work?

1. WxEntryActivity is on the App Module

  1. Introduce gradle dependencies
def custom_package_version = "0.1.2"
implementation "pokercc.android.custompackage:annotations:$custom_package_version"
annotationProcessor "pokercc.android.custompackage:compiler:$custom_package_version"
Copy the code
  1. Add a CustomPackage annotation to the class
@CustomPackage(BuildConfig.APPLICATION_ID+".wxapi.WXEntryActivity")
public class WxEntryActivity extends Activity {}Copy the code
  1. The activity declaration in androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pokercc.android.custompackage.share_lib">

    <application>
        <activity android:name="${$applicationId}.wxapi.WXEntryActivity" />
    </application>
</manifest>

Copy the code

Well, you can have a look.

2. WxEntryActivity is in the Library Module

The annotation generator is used, so this Module, instead of being called an AAR, is compiled with the main project

Buildconfig. APPLICATION_ID is in the Library Module, not the package name you want. It is the Library AppID, not the App Module appID. How do you solve it?

A. Define a variable on root build.gradle

// in root build.gradle
buildscript {
    ext {
        application_id="xxx"}... }... ```groovy// In the Library Module, pass
buildConfigField 'String'.'WECHAT_PACKAGE_NAME_PREFIX'."\"$rootproject.ext.application_id\""
manifestPlaceholders['WECHAT_PACKAGE_NAME_PREFIX'] = rootproject.ext.application_id
Builcconfig.wechat_package_name_prefix = builcconfig.wechat_package_name_prefix
Copy the code

B. Define a variable directly to the Library Module, passing the package name

  • In app Module build.gradle
Share_lib is the name of the sharing module
findProject(":share_lib").ext.WECHAT_PACKAGE_NAME_PREFIX = appId
Copy the code
  • In the Library Module build.gradle, check the parameters
try {
    println "WECHAT_PACKAGE_NAME_PREFIX:$WECHAT_PACKAGE_NAME_PREFIX"
} catch (Exception e) {
    throw new RuntimeException("WECHAT_PACKAGE_NAME_PREFIX must be set to compile successfully, such as before referencing the library findProject('$project.name').ext.WECHAT_PACKAGE_NAME_PREFIX='xx' ", e)
}
Copy the code

I prefer the second way, because the declaration is more explicit, please refer to demo for details

Principle:

A subclass is generated by AnnotationProcessor specifying the package name. So the source class, which can only be modified by public, cannot be modified by final, and cannot be an inner class

Portal github.com/pokercc/Cus…

Have brothers message found more simple solution, using the activity – alias, portal blog.csdn.net/happyjie198…