Change Asset Path in Android Studio - android

I'd like to change the path for where my assets live. This is because these assets will change often and are shared with other apps (i.e. iOS app) in another build folder. How can I do this?

To change the asset path you can tell your project to point to a different directory using the sourceSets command in your apps build.gradle file.
Here's how the build.gradle file looks like:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
sourceSets {
main {
assets.srcDirs = ['../../../build/android-assets']
}
}
...

Related

External reference/variable in build.gradle file

Is it possible to use some external reference or variable in build.gradle files?
I have several build.gradle files in my app source files, including the one for module app, module base, module player, etc. (it depends on the structure of your code and the names of your packages).
Inside of each of these files is the following or similar structure:
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0.001"
}
Is there any way I can code this the way that I don't have to change these values in every file? Is it possible to use some external reference or variable and that way I can edit my versionCode, versionName, etc. just on one place?
In your Project gradle
ext {
minSdkVersion = 14
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
// App dependencies
supportLibraryVersion = '26.1.0'
mockitoVersion = '1.10.19'
roomVersion = "1.0.0"
}
In your App gradle
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
.
.
}
}
dependencies {
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
}
Go to File/Project structure/app/flavors then you can get versionCode, versionName, etc then change them what you want and it effects all of your Files.
Check this Image
Yes it is.
In your project-level Gradle config (the one in the root of your project, outside any module folders), you can define variables under the buildscript block:
ext.thisVersionCode = 1
ext.thisVersionName = "1.0.001"
Then you should be able to reference them from your module-level configs:
defaultConfig {
versionCode = rootProject.ext.thisVersionCode
versionName = rootProject.ext.thisVersionName
}
You can define variables in top level build.gradle file and then reference these variables in each module's build.gradle - that way youll be changing them only once in one file.
to give you an example,this is top level file https://github.com/Ejstn/android-starter/blob/master/build.gradle
and this one is module level file: https://github.com/Ejstn/android-starter/blob/master/app/build.gradle
You can also declare whole dependency as variable like in this google's app: https://github.com/google/santa-tracker-android/blob/master/build.gradle

Location of espresso test reports into files created dynamically

Currently, all my test reports are being created and stored in the folder 'test-reports' in the project root directory. Below is the gradle file, in which I have specified the directory under 'testOptions'.
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.edk1kor.decodedemov3"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testOptions{
reportDir "$rootDir/test-reports"
}
}
I want to create a new folder, each time a test is run. Is it possible to dynamically create a folder each time? If not via gradle code, via the android source code?

Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File

I imported a project downloaded from GitHub into my Android Studio project as module.
The "Import module..." wizard worked fine, but when the Adroid Studio tried to rebuild the project, it returned me this error:
Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File
The error is related to this line in the "build.gradle" file of the imported module:
compileSdkVersion rootProject.compileSdkVersion
I tried to add "ext" section in the project "build.gradle" like this:
ext {
compileSdkVersion 26
}
But in this way I receive a new error:
Gradle DSL method not found: 'compileSdkVersion()' Possible causes: ...
In your top-level file use:
ext {
compileSdkVersion = 26
}
In your module/build.gradle file use:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
...
}
Another way:
Your build.gradle in top-level module
ext {
minSdk = 21
targetSdk = 29
compileSdk = 29
buildTools = '29.0.3'
}
Your build.gradle in app module
android {
def buildConfig = rootProject.extensions.getByName("ext")
compileSdkVersion buildConfig.compileSdk
buildToolsVersion buildConfig.buildTools
defaultConfig {
minSdkVersion buildConfig.minSdk
targetSdkVersion buildConfig.targetSdk
}
// ...
}
In build.gradle you need to write compilesdkversion under android tag as in this example:
android {
..
compileSdkVersion 26 // 26 is an example
..}
By the way. You can build that module as library then import it into your project as .aar file.
Change your .gradle android part to this
android {
compileSdkVersion 26
defaultConfig {
applicationId "your App id"
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

Importing OpenCV Sample to Android Studio

I am trying to import a OpenCV sample I got from http://ee368.stanford.edu/Android/OpenCV/ to Android Studio on a windows machine.
However, I'm getting the following error
* Project CVCamera MSER:C:\AndroidDevelopment\CVCamera_MSER\project.properties:
Library reference ..\..\android-jni could not be found
Path is C:\AndroidDevelopment\CVCamera_MSER\..\..\android-jni which resolves to C:\android-jni
Any ideas?
EDIT - more info:
I'm using the experimental gradle plugin
`classpath 'com.android.tools.build:gradle-experimental:0.6.0-alpha7'
And my gradle file is as follows:
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig.with {
applicationId "co.vispera.moka"
minSdkVersion.apiLevel 17
targetSdkVersion.apiLevel 23
versionCode 1
versionName "1.0"
}
}
android.ndk {
moduleName = "mymodule"
// ldLibs.addAll(['log'])
// cppFlags.add("-std=c++11")
// cppFlags.add("-fexceptions")
// stl = '
}
}
1.Maybe your sample's default API version codes are too low.
If you're using Android Studio, try changing build.grade Version codes to newer ones.
example:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
}
}
2.If same thing happens, try copying OpenCV libs folder to your project.
Copy OpenCV libs folder,(OpenCVPath/sdk/native/libs)
and paste it inside your app's 'main' folder.
Don't forget to rename it to 'jniLibs'.

sub folder for layout in android studio

I developing an android project for my university. My layout folder is too big, and i decide to create sub folder in my layout folder.
I read this answer and question but this is not work for me.
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "example.google.com.widgetforlockscreen"
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/layouts_category2',
'src/main/res/layouts',
'src/main/res'
]
}
}
}
but nothing create for me.
Gradle won't create any directories for you. You have to create the following directory structure,
- res/layouts
- layout
- layout1.xml
- layouts_category2
- layout
- layout2.xml
Like this, we can use Gradle's ability to merge multiple resource folders. Note that it's not an Android feature.

Categories

Resources