简体   繁体   中英

How to set package id in kapt

I rewrite our android app with feature module, in feature module, there are some layout files, for example. layout/feature_entry.xml, when I log the value of R.layout.feature_entry, I got -2130575360(0x81020000). why the value is negative, how to fix it.

one consequence of negative id is that DynamicNavHostFragment won't work, it will crash app.

crash is due to negative id, it's like:

Caused by: java.lang.IllegalStateException: FragmentContainerView must have an android:id to add Fragment androidx.navigation.dynamicfeatures.fragment.DynamicNavHostFragment with tag layout/fragment_android_map_0 at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:157) at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:51) at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:239) at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1061) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961) at android.view.LayoutInflater.inflate(LayoutInflater.java:659) at android.view.LayoutInflater.inflate(LayoutInflater.java:534) at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126) at androidx.databinding.ViewDataBinding.inflateInternal(ViewDataBinding.java:1368)

in FragmentContainerView.java:

if (name != null && existingFragment == null) {
            if (id <= 0) {
                final String tagMessage = tag != null
                        ? " with tag " + tag
                        : "";
                throw new IllegalStateException("FragmentContainerView must have an android:id to "
                        + "add Fragment " + name + tagMessage);
            }

so if you got negative view id, it will throw IllegalStateException.

at the first beginning, I have no idea why this happens. but these id is compiled by aapt, and the format of these integer id is something like PPNNXXXX, PP is package id, so I have 1 app, 2 feature module, the package id are 0x79, 0x80, 0x81. so the question here is how to make these package id 0x79. what setting will enable/disable different package id.

update: separate package id is ok, the problem is that it shouldn't be 0x8x that's negative number. so how to stop invalid package id like 0x8x from being generated.

update: if using aapt2, by using the following method can set package id:

aaptOptions { 
additionalParameters '--allow-reserved-package-id','--package-id','0x70'
}

It doesn't work I think because I am using kapt. tried

kapt {
        arguments {
            arg("allow-reserved-package-id", "true")
            arg("package-id", "0x20")
        }
    }

it doesn't work.

the fix is simple, just make minSdkVersion lower than 26. you will get package id lower than 0x80. but why's that is still unknown.

update: before Android 8(API level 26), resource's package id that greater than 0x7f is not supported. so if we define minSdVersion as 25, due to compatibility, package id that greater than 0x7f won't be assigned.

for minSdVersion that's greater than 25, package id that's greater than 0x7f will be used, however, androidx.fragment.app.FragmentContainerView won't support package id that is greater than 0x7f. most likely, this is a bug of androidx.fragment.app.FragmentContainerView(as of 1.4.0-alpha05).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM