4

Im working on a little interface in android and when I run it the "xxx application has stopped unexpectedly" appears. Im looking for posible errors but I don't find anything.

Anyway I would like to change the R class package name, when I refactor->rename it eclipse generates another one in the old package,even if I delete that package eclipse generates it again, I can't get rid of it!

Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
Flanagan1
  • 41
  • 1
  • 2

5 Answers5

18

To change the package name of the generated R class, change the package property of the <manifest> tag in AndroidManifest.xml

ognian
  • 11,451
  • 4
  • 35
  • 33
5

It could be possible by customizing the aapt tools command line. aapt supports the --custom-package option that specifies the package where R class will be generated.

Using ANT, this can be achieved this way:

          <exec executable="aapt" failonerror="true">
              <arg value="package" />
              <arg value="-m" />
              <arg value="-J" />
              <arg path="${gen.absolute.dir}" />
              <arg value="-M" />
              <arg path="AndroidManifest.xml" />
              <arg value="-I" />
              <arg path="${android.jar}" />
              <arg value="-A" />
              <arg path="${asset.absolute.dir}" />
              <arg value="-S" />
              <arg path="${resource.absolute.dir}" />
              <arg value="--custom-package" />
              <arg value="my.custom.package" />
          </exec>
david
  • 1,311
  • 12
  • 32
  • Thanks a lot for this tip. david, I think it will be VERY useful for me to deal with several project branches. Can you complete a bit your answer? I mean, where I need to put that XML code? – Fran Marzoa Jul 18 '12 at 11:23
  • My comment doesn't apply to recent Android SDK since an aapt ANT task has been introduced in Android tools, and it is used in Android ANT script. What I did on my side is customizing the ANT script available in /tools/ant directory, and I customized targets such as -resource-src , -package-resources. – david Jul 19 '12 at 19:26
  • Plus one just to help me find the answer to my question.. where I was puzzled about R classes being disappeared from an app. Found them in another package. Thanks – Arunkumar Sep 19 '13 at 07:18
2

I have customized an ant script so that I can change the package name for R.java when building my application. What I've did is to override the -code-gen target and modified the projectLibrariesPackageName attribute when -code-gen is executing aapt. (one alternative is to change the build build.xml that eclipse is using, I think it can be found in /tools/ant/build.xml )

    <!-- Code Generation: compile resources (aapt -> R.java), aidl, renderscript -->
  <target name="-code-gen">
        <do-only-if-manifest-hasCode
                elseText="hasCode = false. Skipping aidl/renderscript/R.java">
            <echo>...........</echo>
            <echo>Handling aidl files...</echo>
            <aidl executable="${aidl}" framework="${android.aidl}"
                    genFolder="${gen.absolute.dir}">
                <source path="${source.absolute.dir}"/>
            </aidl>

            <!-- renderscript generates resources so it must be called before aapt -->
            <echo>...........</echo>
            <echo>Handling RenderScript files...</echo>
            <renderscript executable="${renderscript}"
                    framework="${android.rs}"
                    genFolder="${gen.absolute.dir}"
                    resFolder="${resource.absolute.dir}/raw"
                    targetApi="${target.api}">
                <source path="${source.absolute.dir}"/>
            </renderscript>

            <echo>...........</echo>
            <echo>Handling Resources...</echo>
            <aapt executable="${aapt}"
                command="package"
                verbose="${verbose}"
                manifest="AndroidManifest.xml"
                androidjar="${android.jar}"
                rfolder="${gen.absolute.dir}"
                nonConstantId="${android.library}"
                projectLibrariesResName="project.libraries.res"
                projectLibrariesPackageName="package.internal"> <!-- Put custom package name here -->
                    <res path="${resource.absolute.dir}" />
            </aapt>
        </do-only-if-manifest-hasCode>
    </target>
David
  • 21
  • 1
0
  1. Look at your LogCat. It should have the stack trace of your error.
    • To see your LogCat from Eclipse, go to Window > Show View > Other > Android > LogCat.
    • To see your LogCat from the command line, go to your SDK/tools folder and run "adb logcat"
  2. It's not possible to change the R class package.
Jonas Alves
  • 1,306
  • 12
  • 17
0

Change your Perspective in Eclips from Java Perspective (Top Right Corner) to DDMS Perspective. There you will find the LogCat to View Errors in your Android Build.

Check it and then Post that errors so we can help you.

Dirk De Winnaar
  • 167
  • 2
  • 9