How to create a custom subfolder within assets in an APK? - android

I have an external project as a git submodule inside my Android project. That external project contains assets that I am copying over to the APK when building the android project. Here is how it is done in my module-level build.gradle file:
android {
...
sourceSets {
main {
assets {
srcDirs 'path/to/my/submodule/assets',
'path/to/my/submodule/otherAssets'
}
}
}
}
How can I tell gradle to copy both of these assets folders under the same subfolder within assets? So, in the final APK I would like to have the following structure for my assets:
└── assets
└── externalAssets
├── assets
└── otherAssets

Related

Add dynamic feature module in a Eclipse Style Android project

I am trying to add dynamic feature module in my project. It was previously an Eclipse project so the structure is different from the Android Studio structure. The main application is in the root directory, not an independent module.
The project structure as follow:
/Project Root
Project Root Files
+Module1
+Dynamic Module
I want to add a dynamic feature module in the project, so I need to add the root project as the dependency of the dynamic module. Is there a way that I can do this? In the dynamic module build.gradle file, I tried ':Root' and ':', both did not work. Gradle said it could not resolve the root project.
Even I faced the above issue and was able to resolve it by referring the base module in the dependency module with the below approach.
dependencies {
implementation project(':')
}
If the base module is in the root of the project, one should refer the base module in the dependency module with the ":" symbol.
Using a project structure that gradle can deal with is the important bit here.
You can migrate the project Root to a different folder.
By convention that has been app.
You then can refer to it from dependent projects as :app.
The project structure would be then something like this:
.
├── build.gradle
├── app
│ ├── build.gradle
│ └── src
├── moduleA
│ ├── build.gradle
│ └── src
├── moduleB
│ ├── build.gradle
│ └── src

Android test raw resource

I have the following folder structure in Android Studio:
├── androidTest
│   ├── java
│   └── res
│   └── raw
│   └── test_file
└── main
├── java
└── res
└── raw
   └── app_file
I'm trying to access the test_file resource which exists in the raw folder of the androidTest elements. Here's the code inside a Robotium test case that inherits from ActivityInstrumentationTestCase2:
InputStream is = this.getInstrumentation()
.getContext()
.getResources()
.openRawResource(R.raw.test_file);
Android Studio throws a reference error since the resource cannot be found. The exact error is "Cannot resolve symbol test_file".
How can I reference this resource form a test case, which exists on the androidTest resources bundle?
By default your androidTest project will include your app's R class, but androidTest's resources will be generated into a separate file. Make sure you import the R class from your test project:
import com.your.package.test.R;
[..]
getInstrumentation().getContext().getResources().openRawResource(R.raw.test_file);
You can also directly reference the test project's R class:
getInstrumentation().getContext().getResources().openRawResource(com.your.package.test.R.raw.test_file);
I had the androidTest resources in the right spot (src/androidTest/res) and I still couldn't access them via <normal.package>.test.R. I spent a lot of time googling trying to figure out what was going on..
I FINALLY stumbled onto the answer. If you're building a buildType where you specified an applicationIdSuffix, your files are at <applicationId><applicationIdSuffix>.test.R !!!!
i.e.
applicationId "com.example.my.app"
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
if you have androidTest resources in the right directory, then you can only access them via com.example.my.app.debug.test.R !!!!
See Android Unit Tests Requiring Context. For instrumentation test use InstrumentationRegistry.getInstrumentation().getTargetContext() (in Kotlin: getInstrumentation().targetContext). Then you can access resources. You won't need to import R file.
As the others have stated, androidTest resource ids are generated in <applicationId>.test.R by default. Since applicationId can be modified by different build types and flavors, this leads to different R files location for each of them. This can be changed by assigning explicit value to testApplicationId in the defaultConfig of the app's android configuration in build.gradle. It can be useful if there are more than one build types/flavors that alter the appId, but the tests can be run for any of them.
build.gradle (app):
android {
defaultConfig {
applicationId "com.example.hello"
testApplicationId = "com.example.hello.test"
}
}
Test files:
import com.example.hello.test.R

How to build multiple APKs from a single source project

I wanna get multiple APKs from a single source project.
Just the application's title, icon, and package name are different with the others.
The project is on gradle(1.12), as below.
.
└── my_project
├── build.gradle
├── settings.gradle
└── module
├── build.gradle
└── src
How can I do that?
You can use productFlavors for that, and the under the promo and full folders (for example) create strings file (promo/res/values/strings.xml) with the update title value, same approach goes for the icon.
productFlavors {
promo {
packageName "com.woony.promo"
versionCode 1
versionName "v1.0.0_promo"
}
full {
packageName "com.woony"
versionCode 1
versionName "v1.0.0"
}
}
The updated project structure should be like the following
.
└── my_project
├── build.gradle
├── settings.gradle
└── module
├── build.gradle
└── src
├── main
├── promo
└── full
And to generate the release apks just call the following once (just make sure you added signingConfigs and linked it in your release buildTypes)
gradle assembleRelease
All the common files like java code, manifest and base resources are under "src/main"
Move your applications specific folders into "src/projects"
declare two productFlavor (one per applications) specifying applicationId and versionName
create two sourceSet (one per project) indicating the specific res, java, etc.. folders (i just needed res folder)
Full project's structure
Now, use Build Variants (bottom-left of Android Studio) to select the application to run

How to create the best Android app project structure with Android Studio and Gradle?

I'm trying to switch to Android Studio and Gradle, but I have quite some issues with both integration into Studio and build with Gradle.
I've got an app that relies on several libraries.
I'd like to use Android Studio, and Gradle build system.
I'm using git
Most of my libraries are directly git cloned from their github location
Currently, what I have is:
Main Project
├── GH Lib 1
│ ├── <some stuff from the lib>
│ └── library
│ ├── AndroidManifest.xml
│ ├── res
│ └── src
├── GH Lib 2
│ └── <same structure as the lib 1>
├── GH Lib 3
│ └── <same structure as the lib 1>
│── GH Lib 4
│ └── <same structure as the lib 1>
└── My App folder
└── AndroidManifest.xml
└── res
└── src
└── libs
Each of the 'GH Lib X' directory is the result of a git clone from GitHub (for example: ActionBarSherlock).
'My app folder' contains directly res, src, AndroidManifest.xml, libs (with jars), etc.
1st question
I would like to understand how I can integrate all of this in Studio, with Gradle. Currently each lib is a module, and contains a build.gradle file. My App contains also a build.gradle file, however I can't reference dependencies from other folders, because they are in the parent folder, and this AFAIK can't be done with Gradle.
Would this structure be better?
My App Folder
│── AndroidManifest.xml
│── res
│── src
│── libs
└── dependencies
│── GH Lib 1
│── GH Lib 2
│── GH Lib 3
│── GH Lib 4
└── My App folder
My second question related to this is integration with git. Currently all libs are git submodules, is it a good idea?
You should look at the multiproject example for the layout attached in the doc.
http://tools.android.com/tech-docs/new-build-system
http://docs.google.com/viewer?a=v&pid=sites&srcid=YW5kcm9pZC5jb218dG9vbHN8Z3g6NDYzNTVjMjNmM2YwMjhhNA
Essentially you want a top level settings.gradle that tie all the pieces together. Ideally they should be in one single git repo to make your life easier. However you probably can use symlink to tie them into a common build repo that contain your top level settings.gradle.
This structure will work just fine. I have a similar structure and everything OK in Ubuntu 13.04 and Android Studio 130.729444.
You should provide settings.gradle file in root project with references (basically it's a relative path) to each module which should be built.
include ':my-app', ':gh-lib-1:library', ':gh-lib-2:library'
Root build.gradle file should contain tasks/configuration which will be common for all projects. More about multi-project setup can be found here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup
Right now your source directories location does not conform to the default Android Studio setup. You can either move your src, res directories or setup sourceSets configuration in your build.gradle. It should be done for each project. More about project structure: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Configuring-the-Structure
After these steps you may try to import your project to Android Studio by selecting root build.gradle file in the 'Import project' dialog.
It's possible that at first you will be unable to build the project in IDE due to Task 'assemble' not found in root project error. This is a bug in Android Studio. Fortunately, there is a workaround for this - just include task assemble{} in build.gradle files for all 'root' projects.

For Android project how do I get maven to pickup all libraries in lib directory including .so files?

I have project that compiles and links fine when using Eclipse IDE. This project uses some external jar files and also uses some of JNI ( .so ) libraries.
They are all located in the libs/ directory of my project. How should my pom.xml file look like in order for all the jar files in libs/ folder and the shared objects are included in the APK. My libs directory looks something like this:
libs/
├── mydevicelib.jar
├── armeabi
│   ├── libdevice.so
├── armeabi-v7a
│   ├── libdevice.so
│   ├── libcmiris.so
├── libcommon.jar
I am using maven 3.0.5
These jars should be included as dependencies in the maven POM.
Rather than include them in a local lib folder they should be deployed to a maven repository.
For any artifacts that are available through maven central repository just include the dependency directly from there.
For custom artifacts, the best way to do this is to set up your own maven repository (http://archiva.apache.org/index.cgi or http://www.sonatype.org/nexus/) and deploy the artifacts to that. Then you can include them as dependencies in the pom.
Alternatively, if this is overkill for your project, you can set up a local project repository and deploy the artifacts to that.
I havent actually done this with JNI libraries, though this link looks like it may contain useful info for doing a similar thing there: http://code.google.com/p/maven-android-plugin/wiki/NativeLibsAsDependencies. Looks like you just need to add the
<type>so</type>
element to the dependency.

Categories

Resources