hqt
hqt

Reputation: 30284

Android: Error when many package use R.java

I have two package, for example, packageA and packageB. in packageA I has a class named classA that take resource from R.java: example:

TextView textView = (TextView) findViewById(R.id.textView);

same with package B. I has a class named classB that take resource from R.java

EditText editText = (EditText) findViewById(R.id.EditText);

When I want android to run classB, I must change Android Manifest file, change default package from com.packageA to com.packageB, so classA will be notice error at above line. The same when I reverse.

So, How can I fixed this problem: use multi packages and many of them use Resource data. Please help me.

thanks :)

Upvotes: 0

Views: 612

Answers (3)

Mikaël Mayer
Mikaël Mayer

Reputation: 10711

I had the same problem because I was forking a project to another with different resources, and because I was using scala (2.8.3) and treeshaker, the plug-ins would not allow me to properly use android library projects.

I solved the problem by adding a custom build step with ant which creates a duplicate of R.java in the package I wanted.

I added a file named custom_rules.xml to the root of my project MyProjectExtended with the following content :

<?xml version="1.0" encoding="UTF-8"?>
  <project name="MyProjectExtended" default="debug">
  <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
  <property name="aapt" location="C:\Program Files (x86)\Android\android-sdk\platform-tools\aapt${exe}" />
  <property name="android.jar" location="C:\Program Files (x86)\Android\android-sdk\platforms\android-10\android.jar" />

  <!-- Generates the R.java file for this project's resources. -->
  <target name="debug">
     <echo>Generating R.java / Manifest.java from the resources...</echo>
 <exec executable="${aapt}" failonerror="true">
   <arg value="package" />
   <arg line="" />
   <arg value="-m" />
   <arg value="-J" />
   <arg path="gen" />
   <arg value="-M" />
   <arg path="AndroidManifest.xml" />
   <arg value="-S" />
   <arg path="res" />
   <arg value="-I" />
   <arg path="${android.jar}" />
   <arg value="--custom-package" />
   <arg value="com.MyProjectExtendedPackage" />
     </exec>  
  </target>
</project>

Then I added the custom build step:

  • Right-click on the project, properties
  • On the "Builder", click on "New ..."
  • Choose "Ant Builder", continue.
  • For the BuildFile ${workspace_loc:/MyProjectExtended/custom_rules.xml}
  • Base directory : ${workspace_loc:/MyProjectExtended}

and if the console pop-up is annoying, you can disable it in the tab named 'Build Options'

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82563

If the package specified in your manifest is com.packageA, then in the files of packageA, you don't need to do anything. In the files of packageB, use import com.packageA.R; without changing the package name in the manifest. That way, your packageB can access the R.java file.

Upvotes: 3

Rashmi.B
Rashmi.B

Reputation: 1787

You do not need to mention all your packages in the manifest file. Just declare class A package and run the program. But make sure you import the package of class B in your java class where you are calling it. The manifest file by default declares the first package that you mention when you create a new android project.

Let me know if this solves your issue

Upvotes: 1

Related Questions